大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本
最近,又临近博主所负责的一个大型项目的发版了。之前有提到过,该项目涉及到30-40台服务器的发版。且项目客户规定发版需在晚上10-11点左右开始进行,这里博主不得不说每次发布最后都是眼花缭乱。
博主最近想了下,还是决定做些自动化发版的脚本;首先,在批量关闭服务器、批量重启服务器、批量延迟重启服务器、以及批量nginx重启、kill、reload方面都可以做成自动化脚本。当然,还有资源备份、代码备份等等。
自动化脚本来发版后,将一键关闭、重启所有tomcat,一键实现所有Nginx的downline、online、kill,一键备份代码(包括定时任务代码、普通服务器代码、接口服务器代码、消息服务器代码等),一键资源备份,一键数据库备份。当然,在执行自动化脚本之前,服务器必须先配置ssh免密登录。
通过上面的自动化脚本后,我们的整个发版过程就非常简单了:
(1)通过增量打包工具patch-generator-desk实现本次版本增量代码打包,具体参照patch-generator-desk打包软件

(2)使用xftp上传覆盖代码,由于打包出来是可以直接上传实现替换,故此步骤非常简单

(3)一键执行代码备份脚本
(4)执行n_downline_ssh.sh脚本实现Nginx请求重写跳转
(5)执行一键关闭所有tomcat服务器脚本t_kill.sh
(6)执行资源文件以及数据库脚本一键备份的脚本
(7)执行一键启动所有tomcat服务器脚本t_start.sh/t_restart_delay.sh
(8)执行n_go_online_ssh.sh脚本实现Nginx重写加载非跳转配置实现上线
(9)校验本次发版内容完整性
(10)发版结束........
以下是自动化脚本内容:
代码备份相关脚本
t_ump_code_backup.sh(代码备份)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:30:00
#function instruction: this shell use to backup tomcat ump code ,use romote ssh command
fileNameStr=date <span class="hljs-string">'+%Y_%m_%d_%H_%M_%S'</span>
SERVERS=(10.x.x.1x3 10.x.x.10x 10.x.x.105 10.x.x.1x0)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======start backup tomcat ump code $SERVER $dat ===========
ssh root@$SERVER "nohup tar --exclude /opt/wcc/apache-tomcat-7.0.57/webapps/ump/attached --exclude /opt/wcc/apache-tomcat-7.0.57/webapps/ump/download --exclude /opt/wcc/apache-tomcat-7.0.57/webapps/ump/uploads -zcvf /opt/backup/ump${fileNameStr}.tar.gz /opt/wcc/apache-tomcat-7.0.57/webapps/ump 1>/opt/backup/umpcodebackup.log 2>/dev/null & ; exit;"
echo start backup tomcat ump code $SERVER success $dat=
done
echo 自动化脚本任务执行完成
数据库备份相关脚本
t_ump_database_backup.sh(数据备份)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:30:00
#function instruction: this shell use to backup ump database ,use romote ssh command
SERVERS=(10.x.x.1xx)
echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo ========start backup ump database $SERVER $dat ===========
ssh -Tq root@$SERVER < /opt/wcc_auto_sh/t_remote_dbbackup.sh
echo ========start backup ump database $SERVER success $dat===========
done
echo ==========自动化脚本任务执行完成==========
t_remote_dbbackup.sh(代码备份脚本调用的本地脚本到远程服务器执行)
#/bin/bash
fileNameStr=`date '+%Y%m%d_%H%M%S'`
nohup mysqldump -uroot -p111111 chery_wcc > /opt/cherry/dbbackup/chery_wcc${fileNameStr}.sql 2>/dev/null &
tomcat相关脚本
t_kill.sh(tomcat批量Kill脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 12:30:00
#function instruction: this shell use to kill tomcat downline,use remote ssh command
SERVERS=(10.x.x.1x5 10.x.x.1x6 10.x.x.1x5)
#SERVERS=(10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x5 10.x.x.1x6 10.x.x.1x0 10.x.x.1x1 10.x.x.1x2 10.x.x.193 10.x.x.1x4 10.x.x.1x5 10.x.x.1x6)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======kill tomcat $SERVER $dat ===========
ssh root@$SERVER "ps -ef | grep '/opt/wcc/apache-tomcat-7.0.57|/opt/wcc/t1|/opt/wcc/apache-tomcat-7.0._export' | grep -v grep | awk '{print $2}'| xargs kill;exit;"
echo kill tomcat $SERVER success $dat=
done
echo 自动化脚本任务执行完成
t_restart.sh(批量重启tomcat脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 12:30:00
#function instruction: this shell use to restart multiple tomcat go online,use remote ssh command
SERVERS=(10.x.x.1x5 10.x.x.1x6 10.x.x.1x5)
#SERVERS=(10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x5 10.x.x.1x6 10.x.x.1x0 10.x.x.1x1 10.x.x.1x2 10.x.x.193 10.x.x.1x4 10.x.x.1x5 10.x.x.1x6)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======kill tomcat $SERVER $dat ===========
ssh root@$SERVER "ps -ef | grep '/opt/wcc/apache-tomcat-7.0.57|/opt/wcc/t1|/opt/wcc/apache-tomcat-7.0._export' | grep -v grep | awk '{print $2}'| xargs kill;exit;"
echo kill tomcat $SERVER success $dat=
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======start tomcat $SERVER $dat ===========
if [ $SERVER = 10.x.x.1x5 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh ;/opt/wcc/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.1x6 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/t1/bin/startup.sh;exit;"
fi
echo start tomcat $SERVER success $dat=
done
echo 自动化脚本任务执行完成
t_restart_delay_new.sh(批量延时重启tomcat脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 12:30:00
#function instruction: this shell use to restart multiple tomcat go online,use remote ssh command
interval_time=10s
if [ "$1" ]; then
echo =成功设置延迟启动时间设置为:$1s=========
interval_time=$1
else
echo =默认设置延迟启动时间设置为:${interval_time}s=========
fi
SERVERS=(10.x.x.1x5 10.x.x.1x6 10.x.x.1x5)
#SERVERS=(10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x5 10.x.x.1x6 10.x.x.1x0 10.x.x.1x1 10.x.x.1x2 10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x6)
i=1
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======kill tomcat $SERVER $dat ===========
ssh root@$SERVER "ps -ef | grep '/opt/wcc/apache-tomcat-7.0.57|/opt/wcc/t1|/opt/wcc/apache-tomcat-7.0._export' | grep -v grep | awk '{print $2}'| xargs kill;exit;"
echo kill tomcat $SERVER success $dat=
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======start tomcat $SERVER $dat ===========
if [ $SERVER = 10.x.x.1x5 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh ;/opt/wcc/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.1x6 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/t1/bin/startup.sh;exit;"
fi
echo start tomcat $SERVER success $dat=
if [[ "$i" -lt ${#SERVERS[@]} ]]; then
echo ==sleep ${interval_time}=
sleep ${interval_time}
fi
let "i++"
done
echo 自动化脚本任务执行完成
t_start.sh(批量启动tomcat脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 12:30:00
#function instruction: this shell use to start tomcat go online,use romote ssh command
SERVERS=(10.x.x.1x5 10.x.x.1x6 10.x.x.1x5)
#SERVERS=(10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x5 10.x.x.1x6 10.x.x.1x0 10.x.x.1x1 10.x.x.1x2 10.x.x.1x3 10.x.x.1x4 10.x.x.1x5 10.x.x.1x6)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======start tomcat $SERVER $dat ===========
if [ $SERVER = 10.x.x.1x5 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh ;/opt/wcc/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.1x6 ]; then
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/wcc/apache-tomcat-7.0.57/bin/startup.sh;/opt/wcc/t1/bin/startup.sh;exit;"
fi
echo start tomcat $SERVER success $dat=
done
echo 自动化脚本任务执行完成
nginx相关脚本
n_downline_ssh.sh(nginx批量下线脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx downline,use remote ssh command
SERVERS=(10.x.x.x0x)
#SERVERS=(10.x.x.1xx 10.x.x.x0x 10.x.x.x8x)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo 执行$SERVER nginx下线任务 $dat==
ssh -Tq root@$SERVER < /opt/wcc_auto_sh/n_remote_downline.sh
echo 执行$SERVER nginx下线任务完成,成功下线 $dat==
done
n_go_online_ssh.sh(nginx批量上线脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx go online,use remote ssh command
SERVERS=(10.x.x.1xx)
#SERVERS=(10.x.x.x0x 10.x.x.xx2 10.x.x.1xx)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo 执行$SERVER nginx上线任务 $dat
ssh -Tq root@$SERVER < /opt/wcc_auto_sh/n_remote_online.sh
echo 执行$SERVER nginx上线任务完成,成功上线 $dat
done
n_kill_ssh.sh(nginx批量Kill脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx kill,use remote ssh command
SERVERS=(10.x.x.x0x)
#SERVERS=(10.x.x.1xx 10.x.x.x0x 10.x.x.1xx)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo 执行$SERVER nginx kill任务 $dat==
ssh root@$SERVER "ps -ef | grep 'nginx' | grep -v grep | awk '{print $2}'| xargs kill;exit;"
echo 执行$SERVER nginx kill任务完成,成功kill $dat==
done
n_start_ssh.sh(nginx批量start脚本)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx kill,use remote ssh command
SERVERS=(10.x.x.x0x)
#SERVERS=(10.x.x.1x1 10.x.x.xx2 10.x.x.1xx)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo 执行$SERVER nginx start任务 $dat==
ssh root@$SERVER "/usr/local/nginx/sbin/nginx ; exit;"
echo 执行$SERVER nginx start任务完成,成功start $dat==
done
n_remote_downline.sh(n_downline_ssh.sh调用的本地脚本到远程执行)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx downline,use remote ssh command
c1=netstat -antp |grep -v grep |grep nginx |wc -l
echo $c1
cp /usr/local/nginx/conf/nginx_downline.conf /usr/local/nginx/conf/nginx.conf
if [ $c1 -eq 0 ]; then
/usr/local/nginx/sbin/nginx
else
/usr/local/nginx/sbin/nginx -s reload
fi
n_remote_online.sh(n_go_online_ssh.sh调用的本地脚本到远程执行)
#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-07-7-31 21:40:00
#function instruction: this shell use to multiple nginx go online,use remote ssh command
c1=netstat -antp |grep -v grep |grep nginx |wc -l
echo $c1
rm -rf /usr/local/nginx/conf/nginx.conf;
cp /usr/local/nginx/conf/nginx_go_online.conf /usr/local/nginx/conf/nginx.conf;
if [ $c1 = 0 ]; then
/usr/local/nginx/sbin/nginx
else
/usr/local/nginx/sbin/nginx -s reload
fi
exit
n_restart_ssh.sh(nginx重启脚本)
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-08-01 21:30:00
#function instruction: this shell use to restart multiple nginx go online,use remote ssh command
SERVERS=(10.x.x.xxx 10.x.x.x02 10.x.x.xxx)
echo 总共${#SERVERS[@]}台服务器
for SERVER in ${SERVERS[@]}
do
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======kill nginx $SERVER $dat ===========
ssh root@$SERVER "ps -ef | grep nginx | grep -v grep | awk '{print $2}'| xargs kill;exit;"
echo kill nginx $SERVER success $dat=
dat=date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>
echo ======start nginx $SERVER $dat ===========
ssh root@$SERVER "/usr/local/nginx/sbin/nginx ; exit;"
echo start nginx $SERVER success $dat=
done
echo 自动化脚本任务执行完成
n_restart_delay_ssh.sh(nginx延迟重启脚本)
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-08-01 21:30:00
#function instruction: this shell use to restart multiple nginx go online,use remote ssh command
interval_time=60s
if [ "$1" ]; then
echo ===========成功设置延迟启动时间设置为:$1s===================
interval_time=$1
else
echo ===========默认设置延迟启动时间设置为:${interval_time}s===================
fi
i=1
SERVERS=(1x.x.x.xx1 10.x.x.x0x)
#SERVERS=(1x.x.x.xxx 10.x.x.x0x 1x.x.x.x8x)
echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo ========kill nginx $SERVER $dat ===========
ssh root@$SERVER "ps -ef | grep nginx | grep -v grep | awk '{print \$2}'| xargs kill;exit;"
echo ========kill nginx $SERVER success $dat===========
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo ========start nginx $SERVER $dat ===========
ssh root@$SERVER "/usr/local/nginx/sbin/nginx ; exit;"
echo ========start nginx $SERVER success $dat===========
if [[ "$i" -lt ${#SERVERS[@]} ]]; then
echo ==sleep ${interval_time}=
sleep ${interval_time}
fi
let "i++"
done
echo 自动化脚本任务执行完成
最后总结,由于博主其它数据资源备份脚本还没写好,今天先写到这里,其它脚本后续将补充到此篇文章。以上是博主本次文章的全部内容,如果大家觉得博主的文章还不错,请点赞;如果您对博主其它服务器技术或者博主本人感兴趣,请关注博主博客,并且欢迎随时跟博主沟通交流。
大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本的更多相关文章
- nginx 服务脚本编写模板
编写nginx服务脚本:脚本内容如下: [root@www ~]# cat /etc/init.d/nginx #!/bin/bash # nginx Startup script for the N ...
- Gitlab+Jenkins学习之路(八)之发布maven项目及按版本发布
一.什么是Maven maven是一个项目管理和综合工具.Maven提供给开发人员构建一个完整的生命周期框架. 开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期 ...
- 团队项目作业-Beta版本发布
团队成员: 学号 姓名 201731062234 薛磊 201731062230 李林 201731062231 燕泓达 201731062232 陈东 201731062229 沈瑞琦 201731 ...
- 软工造梦厂团队项目(Alpha版本发布2)
课程 (https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience) 作业要求 https://www.cnblogs.com/h ...
- supervisor开机自动启动脚本+redis+MySQL+tomcat+nginx进程自动重启配置
[root@mongodb-host supervisord]# cat mongo.conf [program:mongo]command=/usr/local/mongodb/bin/mongod ...
- CakeDC(cakephp company)Git workflow--适合于较大团队大型项目开发
CakeDC Git workflow是一个项目开发和版本发布的工作流,在这个工作流程中开发和版本发布周期是基于几个关键阶段(key phases): Development: 所有活跃的开发活动都由 ...
- ABP大型项目实战(2) - 调试与排错 - 日志 - 查看审计日志
这是<ABP大型项目实战>系列文章的一篇. 项目发布到生产环境后难免会有错误. 那么如何进行调试和排错呢? 我看到俱乐部里有人是直接登陆生产服务器把数据库下载到开发机器进行调试排错 ...
- 【 Linux 】lvs-dr模型实现HA,后端Nginx、PHP、MySQL分离 搭建wordpress站点
要求: 1. wordpress程序通过nfs共享给各个realserver 2. 后端realserver中的nginx和php分离 网络结构图: 环境说明: OS:centos6 ...
- 比Ansible更吊的自动化运维工具,自动化统一安装部署自动化部署udeploy 1.0 版本发布
新增功能: 逻辑与业务分离,完美实现逻辑与业务分离,业务实现统一shell脚本开发,由框架统一调用. 并发多线程部署,不管多少台服务器,多少个服务,同时发起线程进行更新.部署.启动. 提高list规则 ...
随机推荐
- 【习题 6-5 UVA-1600】Patrol Robot
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路 bfs一下就ok [代码] /* 1.Shoud it use l ...
- MyBatis学习总结(14)——Mybatis使用技巧总结
1. 区分 #{} 和 ${}的不同应用场景 1)#{} 会生成预编译SQL,会正确的处理数据的类型,而${}仅仅是文本替换. 对于SQL: select * from student where x ...
- Qt学习 之 Socket通信
近期写大作业用到Qt的Socket部分.网上关于这部分的资料都太过复杂,如今总结一下一些简单的应用.有机会能够给大家讲讲用Socket传送文件的代码. 这里主要解说怎样实现TCP和UDP的简单通信. ...
- [Angular] Progress HTTP Events with 'HttpRequest'
New use case that is supported by the HTTP client is Progress events. To receive these events, we cr ...
- 关于VUE的安装和一些简单属性
安装vue 安装前初始化package.json 主要用来描述自己的项目,记录安装过得文件有哪些,在当前文件夹下生产json 安装vue --save(-S)代表项目依赖 --save-dev(-D) ...
- Flask项目之手机端租房网站的实战开发(一)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶项目介绍 产品:关于手机移动端的租房网站 角色:在这个产品中用户包括房东与房客 功能:房东可以在这个平台发布自己的房屋,房客可 ...
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- C# 进制转换 在什么情况下使用16进制,字节数组,字符串
C# 进制转换 Admin2013年9月18日 名人名言:从工作里爱了生命,就是通彻了生命最深的秘密.——纪伯伦 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制Con ...
- 9.3 Binder系统_驱动情景分析_服务获取过程
4. 服务获取过程 test_client客户端: (1)在用户态先构造name=“hello”的数据(服务的名字是hello),调用ioctl发送数据给service_manager(handle= ...
- JVM学习:方法重载的优先级
重载:方法名一致,参数长度或者类型不一致. 先放总结,下面为例子 参数具有继承.实现关系,优先考虑子类: 在不考虑对基本类型自动装拆箱(auto-boxing,auto-unboxing),以及可变长 ...
