jenkins代码rsync推送脚本带日志记录和代码分机房处理示例
# 发布脚本
# 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-srv02eus_ip_list="1.1.1.1 1.1.1.2"# eop eop-cart-core-srv01 eop-cart-core-srv02eop_ip_list="1.1.1.3 1.1.1.4"# src directorysrc_directory="services/pcsrv"# dst directorydst_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}.logmkdir -p ${pubMsgDir}############################################################## 非apache用户运行脚本,则退出if [ `whoami` != "apache" ];then echo " only apache user can run me" | tee -a ${pubMsgFile} exit 1fifunction 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 eusfunction cp_config_eus(){ ${config_script} ${src_path} eus}# cp multiconfig to eopfunction cp_config_eop(){ ${config_script} ${src_path} eop}# 只同步美西机房function rsync_eus(){# cp eus multiconfig to euscp_config_eus# rsync eusfor eus_ip in $eus_ip_listdo 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 eopcp_config_eop# rsync eopfor eop_ip in $eop_ip_listdo 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 1fiOPT=$1case $OPT inall) 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/shif [ $# -ne 2 ]then echo "Cpconfig fail! The reason for the failure is a missing parameter!" exit 1fiRootpath=$1Env=$2sourceDir=${Rootpath}/mutienv/$EnvtargetDir=${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 1ficd ${sourceDir}configfiles=$(find ./ -type f -print)for configfile in $configfilesdo 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 fidoneecho "Config copy success!"echo "========== end apply config ================"
配置替换脚本
# cp env.production to .envfunction 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
赞 (0)