jenkins程式碼rsync推送指令碼帶日誌記錄和程式碼分機房處理示例
阿新 • • 發佈:2021-01-07
# 釋出指令碼
# vim rsync_golang_pcsrv.sh
#!/bin/bash # 指令碼中最後一個管道命令返回非0 就退出 set -e # 指令碼中管道命令返回非0 也退出 set -o pipefail # 指令碼中變數存在空 就退出 set -u ##############jenkins拉取git程式碼同步到 publish02 上,然後觸發 publish02 上的該指令碼進行 ws/eus/eop 三個機房同步#################### ###### rsync risksrv.cloud.org data ######################################passwd="/data/www/.rsync/rsyncd.pcsrv" exclude_list="--exclude=.svn --exclude=.git --exclude=.gitignore --exclude='*.log' --exclude=.gitattributes --exclude=apollocache/" # eus eus-cart-core-srv01 eus-cart-core-srv02 eus_ip_list="1.1.1.1 1.1.1.2" # eop eop-cart-core-srv01 eop-cart-core-srv02 eop_ip_list="1.1.1.3 1.1.1.4" # src directory src_directory="services/pcsrv" # dst directory dst_directory="services/pcsrv" ## 時間變數 Date=$(date +'%Y%m%d%H%M%S') DATE_Y=$(date +'%Y') DATE_M=$(date +'%m') DATE_D=$(date +'%d') ## 日誌目錄 pubMsgDir=/data/www/logs/rsync_log/$DATE_Y/$DATE_M/$DATE_D/ pubMsgFile=${pubMsgDir}/${Date}.log mkdir -p ${pubMsgDir} ############################################################# # 非apache使用者執行指令碼,則退出if [ `whoami` != "apache" ];then echo " only apache user can run me" | tee -a ${pubMsgFile} exit 1 fi function func_rsync_status() { if [[ $? == 0 || $? == 23 ]];then rsync_edit=1 else rsync_edit=0 echo "`date` 同步到目標失敗! " | tee -a ${pubMsgFile} exit 1 fi } src_path="/data/www/vhosts/${src_directory}" config_script=${src_path}/cpconfig.sh # cp multiconfig to eus function cp_config_eus() { ${config_script} ${src_path} eus } # cp multiconfig to eop function cp_config_eop() { ${config_script} ${src_path} eop } # 只同步美西機房 function rsync_eus() { # cp eus multiconfig to eus cp_config_eus # rsync eus for eus_ip in $eus_ip_list do echo "####################rsync eus_ip start################################" | tee -a ${pubMsgFile} echo $eus_ip | tee -a ${pubMsgFile} rsync -zavP --delete $exclude_list --password-file=$passwd /data/www/vhosts/${src_directory}/ apache@$eus_ip::apache/data/www/vhosts/${dst_directory}/ | tee -a ${pubMsgFile} func_rsync_status echo "################### rsync eus_ip end #######################" | tee -a ${pubMsgFile} done } # 只同步歐洲德國機房 function rsync_eop() { # cp multiconfig to eop cp_config_eop # rsync eop for eop_ip in $eop_ip_list do echo "####################rsync eop_ip start################################" | tee -a ${pubMsgFile} echo $eop_ip | tee -a ${pubMsgFile} rsync -zavP --delete $exclude_list --timeout=600 --password-file=$passwd /data/www/vhosts/${src_directory}/ apache@$eop_ip::apache/data/www/vhosts/${dst_directory}/ | tee -a ${pubMsgFile} func_rsync_status echo "################### rsync eop_ip end #######################" | tee -a ${pubMsgFile} done } # 同步所有機房 function rsync_all() { # rsync eus rsync_eus # rsync eop rsync_eop } ##################### MAIN ############################### usage () { echo "" echo " Please Input server infomation!" echo "" echo " USAGE: `basename $0` [all|eus|eop]" echo "" } if [ $# != 1 ] then usage >&2 exit 1 fi OPT=$1 case $OPT in all) echo "start rsync `basename $0` to all servers" rsync_all ;; eus) echo "start rsync `basename $0` to eus servers" rsync_eus ;; eop) echo "start rsync `basename $0` to eop servers" rsync_eop ;; *) echo "Usage:`basename $0` [all|eus|eop]" ;; esac
# 程式碼指令碼
[root@publish02:/data/www/vhosts/services/pcsrv]# tree
. ├── app.properties ├── conf │ ├── logconf.xml │ ├── service.yml │ └── settings.yml ├── cpconfig.sh ├── index.html ├── mutienv │ ├── alisz │ │ └── app.properties │ ├── eop │ │ └── app.properties │ └── eus │ └── app.properties └── pcsrv
拷貝替換指令碼
[/data/www/vhosts/services/pcsrv]# more cpconfig.sh
#!/bin/sh if [ $# -ne 2 ] then echo "Cpconfig fail! The reason for the failure is a missing parameter!" exit 1 fi Rootpath=$1 Env=$2 sourceDir=${Rootpath}/mutienv/$Env targetDir=${Rootpath} echo "========= start apply confg ============" echo "yes | cp -fa ${sourceDir}/. ${targetDir}/" yes | cp -fa ${sourceDir}/. ${targetDir}/ if [ $? -ne 0 ] then echo "Cpconfig fail! The reason for the failure is copy config error!" exit 1 fi cd ${sourceDir} configfiles=$(find ./ -type f -print) for configfile in $configfiles do currentMd5=$(md5sum ${sourceDir}/$configfile | cut -d ' ' -f1) targetMd5=$(md5sum ${targetDir}/$configfile | cut -d ' ' -f1) echo $configfile echo $currentMd5 echo $targetMd5 if [ "$currentMd5" != "$targetMd5" ] then echo "Cpconfig fail! The reason for the failure is $configfile md5sum errro" exit 1 fi done echo "Config copy success!" echo "========== end apply config ================"
配置替換指令碼
# cp env.production to .env function cp_config_env() { srcfile="/data/www/vhosts/pixcutapi.wondershare.com/httpdocs/.env.production" dstfile="/data/www/vhosts/pixcutapi.wondershare.com/httpdocs/.env" yes | cp -fa /data/www/vhosts/pixcutapi.wondershare.com/httpdocs/.env.production /data/www/vhosts/pixcutapi.wondershare.com/httpdocs/.env if [ $? -ne 0 ] then echo "Cpconfig fail! The reason for the failure is copy config error!" exit 1 fi currentMd5=$(md5sum $srcfile | cut -d ' ' -f1) targetMd5=$(md5sum $dstfile | cut -d ' ' -f1) echo $dstfile echo $currentMd5 echo $targetMd5 if [ "$currentMd5" != "$targetMd5" ] then echo "Cpconfig fail! The reason for the failure is $configfile md5sum errro" exit 1 fi echo "Config copy success!" echo "========== end apply config ================" } cp_config_env