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

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

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

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

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

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

457b163822dbe63e273a63da8b717b4efd2.jpg

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

7d6c0c1ef590c3842bd942597e6fd4e57e3.jpg

(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 '+%Y_%m_%d_%H_%M_%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%Y-%m-%d %H:%M:%S'`
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 '+%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===========
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 ==========自动化脚本任务执行完成==========

        最后总结,由于博主其它数据资源备份脚本还没写好,今天先写到这里,其它脚本后续将补充到此篇文章。以上是博主本次文章的全部内容,如果大家觉得博主的文章还不错,请点赞;如果您对博主其它服务器技术或者博主本人感兴趣,请关注博主博客,并且欢迎随时跟博主沟通交流。

 

转载于:https://www.cnblogs.com/jpfss/p/9717069.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值