最近,又臨近博主所負責的一個大型項目的發版了。以前有提到過,該項目涉及到50-60臺服務器的發版。且項目客戶規定發版需在晚上10-11點左右開始進行,這裏博主不得不說每次發佈最後都是眼花繚亂。mysql
博主最近想了下,仍是決定作些自動化發版的腳本;首先,在批量關閉服務器、批量重啓服務器、批量延遲重啓服務器、以及批量nginx重啓、kill、reload方面均可以作成自動化腳本。固然,還有資源備份、代碼備份等等。nginx
自動化腳原本發版後,將一鍵關閉、重啓全部tomcat,一鍵實現全部Nginx的downline、online、kill,一鍵備份代碼(包括定時任務代碼、普通服務器代碼、接口服務器代碼、消息服務器代碼等),一鍵資源備份,一鍵數據庫備份。固然,在執行自動化腳本以前,服務器必須先配置ssh免密登陸。web
經過上面的自動化腳本後,咱們的整個發版過程就很是簡單了:sql
(1)經過增量打包工具patch-generator-desk實現本次版本增量代碼打包,具體參照patch-generator-desk打包軟件shell
(2)使用xftp上傳覆蓋代碼,因爲打包出來是能夠直接上傳實現替換,故此步驟很是簡單(此步驟已經編寫自動化腳本實現代碼發佈,執行上傳到腳本所在服務器的專用代碼發佈目錄,執行腳本發佈便可)數據庫
(3)一鍵執行代碼備份腳本apache
(4)執行n_downline_ssh.sh腳本實現Nginx請求重寫跳轉tomcat
(5)執行一鍵關閉全部tomcat服務器腳本t_kill.shbash
(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 ==========自動化腳本任務執行完成==========
最後總結,因爲博主其它數據資源備份腳本還沒寫好,今天先寫到這裏,其它腳本後續將補充到此篇文章。以上是博主本次文章的所有內容,若是你們以爲博主的文章還不錯,請點贊;若是您對博主其它服務器技術或者博主本人感興趣,請關注博主博客,而且歡迎隨時跟博主溝通交流。