大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本

时间:2022-08-29 13:05:44

最近,又临近博主所负责的一个大型项目的发版了。之前有提到过,该项目涉及到30-40台服务器的发版。且项目客户规定发版需在晚上10-11点左右开始进行,这里博主不得不说每次发布最后都是眼花缭乱。

博主最近想了下,还是决定做些自动化发版的脚本;首先,在批量关闭服务器、批量重启服务器、批量延迟重启服务器、以及批量nginx重启、kill、reload方面都可以做成自动化脚本。当然,还有资源备份、代码备份等等。

自动化脚本来发版后,将一键关闭、重启所有tomcat,一键实现所有Nginx的downline、online、kill,一键备份代码(包括定时任务代码、普通服务器代码、接口服务器代码、消息服务器代码等),一键资源备份,一键数据库备份。当然,在执行自动化脚本之前,服务器必须先配置ssh免密登录。

通过上面的自动化脚本后,我们的整个发版过程就非常简单了:

(1)通过增量打包工具patch-generator-desk实现本次版本增量代码打包,具体参照patch-generator-desk打包软件

大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本

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

大型项目linux自动化版本发布脚本(shell)之tomcat、nginx服务脚本

(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 &amp; ; 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服务脚本的更多相关文章

  1. nginx 服务脚本编写模板

    编写nginx服务脚本:脚本内容如下: [root@www ~]# cat /etc/init.d/nginx #!/bin/bash # nginx Startup script for the N ...

  2. Gitlab&plus;Jenkins学习之路(八)之发布maven项目及按版本发布

    一.什么是Maven maven是一个项目管理和综合工具.Maven提供给开发人员构建一个完整的生命周期框架. 开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期 ...

  3. 团队项目作业-Beta版本发布

    团队成员: 学号 姓名 201731062234 薛磊 201731062230 李林 201731062231 燕泓达 201731062232 陈东 201731062229 沈瑞琦 201731 ...

  4. 软工造梦厂团队项目(Alpha版本发布2)

    课程 (https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience) 作业要求 https://www.cnblogs.com/h ...

  5. supervisor开机自动启动脚本&plus;redis&plus;MySQL&plus;tomcat&plus;nginx进程自动重启配置

    [root@mongodb-host supervisord]# cat mongo.conf [program:mongo]command=/usr/local/mongodb/bin/mongod ...

  6. CakeDC&lpar;cakephp company&rpar;Git workflow--适合于较大团队大型项目开发

    CakeDC Git workflow是一个项目开发和版本发布的工作流,在这个工作流程中开发和版本发布周期是基于几个关键阶段(key phases): Development: 所有活跃的开发活动都由 ...

  7. ABP大型项目实战&lpar;2&rpar; - 调试与排错 - 日志 - 查看审计日志

    这是<ABP大型项目实战>系列文章的一篇.   项目发布到生产环境后难免会有错误. 那么如何进行调试和排错呢?   我看到俱乐部里有人是直接登陆生产服务器把数据库下载到开发机器进行调试排错 ...

  8. 【 Linux 】lvs-dr模型实现HA,后端Nginx、PHP、MySQL分离 搭建wordpress站点

    要求:    1. wordpress程序通过nfs共享给各个realserver    2. 后端realserver中的nginx和php分离 网络结构图: 环境说明:    OS:centos6 ...

  9. 比Ansible更吊的自动化运维工具,自动化统一安装部署自动化部署udeploy 1&period;0 版本发布

    新增功能: 逻辑与业务分离,完美实现逻辑与业务分离,业务实现统一shell脚本开发,由框架统一调用. 并发多线程部署,不管多少台服务器,多少个服务,同时发起线程进行更新.部署.启动. 提高list规则 ...

随机推荐

  1. QuickFIX&sol;J常见问题汇总

    最近在搞QuickFIX/J,网上的资料不算很多,遇到一些简单的问题都需要google一阵才能找到解决方法,因此做点记录: 错误:Rejecting invalid message: quickfix ...

  2. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. Java向PostgreSQL发送prepared statement 与 libpq 向PostgreSQL发送prepared statement之比较:

    Java 代码,在数据库端,并没有当成 prepared statetment 被处理. C代码通过libpq 访问数据库端,被当成了 prepared statement 处理.也许是因Postgr ...

  4. 使用modelsim仿真DDR3时编译出错的解决方法

    Modelsim 10.1c release note sates as : Product Changes in 10.1c Release 10.1b introduced a new error ...

  5. TCP协议下Socket的基础编程类型

    套接字的基本操作有: 创建(socket).命名(bind).侦听(listen).连接(accept).关闭(shutdown).发送(send).接受(recv). 下面逐个分析: 一.创建(so ...

  6. python网上开发执行环境

    http://www.tutorialspoint.com/execute_python_online.php

  7. Touch Handling in Cocos2D 3&period;x&lpar;二&rpar;

    接受触摸 在Cocos2d 3.0中每一个CCNode和每一个CCNode的子类都可以接收触摸.你只需要开启一个选项.让我们在定制的初始化器里完成它.替换MainScene.m中init方法的代码: ...

  8. javascript实现异步编程的4种方法

    1.回调函数. 2.事件监听 .  思路:采用事件驱动模式.任务的执行不取决于代码的顺序,而取决于某个事件是否发生 3.观察者模式 (发布/订阅模式)   代码如下: jQuery.subscribe ...

  9. web安全CSRF和XSS

    web端的安全攻击有CSRF和XSS两种,将通过以下三个方面介绍这两种安全攻击: 1.基本概念和缩写 2.攻击原理 3.防御措施 CSRF  1.基本概念和缩写 CSRF(Cross-site req ...

  10. java 坑

    时间戳 unix的时间戳和java中的是不同的.unix为10位,java的13位.需要进行转换. long timestamps = 1509783992L; long javaTimstamps ...