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

孤者浪人 提交于 2020-02-28 15:53:20

        最近,又临近博主所负责的一个大型项目的发版了。之前有提到过,该项目涉及到50-60台服务器的发版。且项目客户规定发版需在晚上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_xmp_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.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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/www/apache-tomcat-7.0.57/webapps/xmp/attached --exclude  /opt/www/apache-tomcat-7.0.57/webapps/xmp/download --exclude /opt/www/apache-tomcat-7.0.57/webapps/xmp/uploads  -zcvf /opt/backup/xmp${fileNameStr}.tar.gz /opt/www/apache-tomcat-7.0.57/webapps/xmp1>/opt/backup/xmpcodebackup.log 2>/dev/null \& ; exit;"
echo  ========start backup tomcat ump code  $SERVER success $dat===========
done
echo ==========自动化脚本任务执行完成==========

代码发布相关脚本( /opt/codepush/2018-11-02-05-42-01为发布文件

sh  t_code_push.sh  /opt/codepush/2018-11-02-05-42-01


#/bin/bash
#version: 1.0.0
#author: aaron
#email: 592235961@qq.com
#create time: 2018-09-29 14:49:00
#function instruction: this shell use to code publish multiple tomcat go online,use remote scp command

SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
tpath1=/opt/www/apache-tomcat-7.0.57_msg
tpath2=/opt/www/apache-tomcat-7.0.57
tpath3=/opt/www/apache-tomcat-7.0._export
tpath4=/opt/www/t1

if [ ! -d "$1" ];then
echo "$1文件夹不存在"
exit 1
else
echo "本服务器文件夹存在即将进入发布流程"
fi
echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo  ========start code publish $SERVER $dat ===========
scp -r $1/* root@$SERVER:$tpath2/webapps/xmp/
if [ $SERVER = 10.x.x.xxx ]; then
scp -r $1/* root@$SERVER:$tpath1/webapps/xmp/
elif [ $SERVER = 10.x.x.xxx ]; then
scp -r $1/* root@$SERVER:$tpath3/webapps/xmp/
else
scp -r $1/* root@$SERVER:$tpath4/webapps/xmp/
fi
echo  ========start code publish $SERVER success $dat===========
done
echo ==========自动化脚本任务执行完成==========

版本回退相关脚本(此脚本需自己根据实际情况改动,/opt/coderollback/2018-03-06-23-14-15.tar.gz为之前脚本备份的代码)

sh t_code_rollback.sh /opt/coderollback/2018-03-06-23-14-15.tar.gz

#/bin/bash
cfile=$1
SERVERS=(10.x.x.xxx 10.x.x.xxx)
tfile=/opt/www/apache-tomcat-7.0.57
tpath1=/opt/www/apache-tomcat-7.0.57_msg
tpath2=/opt/www/apache-tomcat-7.0.57
tpath3=/opt/www/apache-tomcat-7.0._export
tpath4=/opt/www/t1
bkpath=/opt/rollback/
bkfilepath=${bkpath}code_roll_$(basename $cfile)
if [ ! -f "$1" ];then
echo "$1代码备份压缩文件不存在"
exit 1
else
echo "本服务器代码备份压缩文件存在即将进入版本回退流程"
fi

echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
  dat=`date '+%Y-%m-%d %H:%M:%S'`
  echo  ========start code rollback tomcat $SERVER $dat ===========
  if [ $SERVER = 10.x.x.xxx  ]; then
     echo '该服务器不支持'
  else
     ssh root@$SERVER "[ -d $bkpath ] && echo ok || mkdir -p $bkpath;exit;"
     scp -r $1 root@$SERVER:$bkfilepath
     ssh root@$SERVER "cd $tfile/webapps/xmp;ls  | grep -v 'download'|grep -v 'uploads' |grep -v 'attached'| xargs  rm -rf;tar -zxvf $bkfilepath -C / ;exit;"
  fi
  echo  ========start code rollback tomcat $SERVER success $dat===========
done

数据库备份相关脚本

t_xmp_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  xmp 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/www_auto_sh/t_remote_dbbackup.sh 
echo  ========start backup xmp database  $SERVER success $dat===========
done
echo ==========自动化脚本任务执行完成==========

t_remote_dbbackup.sh(代码备份脚本调用的本地脚本到远程服务器执行)

#/bin/bash
fileNameStr=`date '+%Y%m%d_%H%M%S'`
nohup mysqldump -uxoox -p111111 xhexx_www > /opt/xherxx/dbbackup/xhexx_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.xxx 10.x.x.xxx 10.x.x.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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/www/apache-tomcat-7.0.57\|/opt/www/t1\|/opt/www/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.xxx 10.x.x.xxx 10.x.x.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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/www/apache-tomcat-7.0.57\|/opt/www/t1\|/opt/www/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.xxx ]; then
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh ;/opt/www/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.xxx ]; then
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/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.xxx 10.x.x.xxx 10.x.x.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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/www/apache-tomcat-7.0.57\|/opt/www/t1\|/opt/www/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/www/apache-tomcat-7.0.57/bin/startup.sh ;/opt/www/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.1x6 ]; then
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/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.xxx 10.x.x.xxx 10.x.x.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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/www/apache-tomcat-7.0.57/bin/startup.sh ;/opt/www/apache-tomcat-7.0.57_msg/bin/startup.sh;exit;"
elif [ $SERVER = 10.x.x.1x6 ]; then
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/apache-tomcat-7.0._export/bin/startup.sh;exit;"
else
ssh root@$SERVER "/opt/www/apache-tomcat-7.0.57/bin/startup.sh;/opt/www/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.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo ============执行$SERVER nginx下线任务 $dat==============
ssh -Tq root@$SERVER < /opt/www_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.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
echo ==========总共${#SERVERS[@]}台服务器==========
for SERVER in ${SERVERS[@]}
do
dat=`date '+%Y-%m-%d %H:%M:%S'`
echo ============执行$SERVER nginx上线任务 $dat============
ssh -Tq root@$SERVER < /opt/www_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.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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.xxx)
#SERVERS=(10.x.x.xxx 10.x.x.xxx 10.x.x.xxx)
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.xxx 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.xxx 10.x.x.xxx)
#SERVERS=(1x.x.x.xxx 10.x.x.xxx 1x.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===========

if [[ "$i" -lt ${#SERVERS[@]} ]]; then
     echo  ========sleep ${interval_time}=======
     sleep ${interval_time}
fi
let "i++"
done
echo ==========自动化脚本任务执行完成==========

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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!