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-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
原文地址:https://www.cnblogs.com/reblue520/p/14248338.html