更新代码脚本

#!/bin/bash
#mo
#20170911
#Update all oa code

code=/root/code/$2/syberosoa
path=/home/seim
time=`date +%Y%m%d-%H%M`

#Determine the results
result() {
    if [ $? != 0 ];then
        echo -e "33[31m$1 $faild33[0m" && exit 1
    else
        echo -e "33[32m$1 $ok33[0m"
    fi
}

#usage: $1 is remote IP, $2 is ProjectName, $3 is Tag ID
if [ $# -lt 3 ];then
    echo -e "33[31m usage: sh $0 IP ProjectName Tag_ID33[0m" 
    echo -e "33[31m example: sh $0 172.16.xx.xx YiZhuang 亦庄OA-1.0.2.201710.233[0m" 
    echo -e "33[31m explain: 升级主机要配置ssh免密,可以使用auto_ssh.sh 33[0m" && exit 2
fi

#pull tag code
ok="checkout tag ok !";faild="checkout tag faild !"
if [ -d $code ];then
    cd $code
    git checkout master
    git pull
    git tag | grep -Fx $3
    if [ $? == 0 ];then
        git checkout $3
        result
        cd ..
    else
        echo -e "33[31mTag $3 not exist33[0m" && exit 3
    fi
else
    echo -e "33[31m$code not exist33[0m" && exit 4
fi

#backup remote code
ok="backup code ok !";faild="backup code faild !"
ping -c 1 $1
if [ $? == 0 ];then
    ssh -t -p 22 root@$1 "cd $path/syberosoa"
    if [ $? == 0 ];then
        ssh -t -p 22 root@$1 "cp -arf $path/syberosoa/ $path/syberosoa_$time\_bak"
        result 
    else
        echo -e "33[31m$1 $path not exist !33[0m" && exit 5
    fi
else
    echo -e "33[31m$1 network unreachable !33[0m" && exit 6
fi

#rsync code to remote
ok="rsync code ok !";faild="rsync code faild !"
rsync -avzp --delete --exclude-from="/root/scripts/exclude.list" --exclude '.git' $code root@$1:$path
result

#set permission
ok="set permission ok !";faild="set permission faild !"
ssh -t -p 22 root@$1 "chmod -R 755 $path"
ssh -t -p 22 root@$1 "chown -R seim.seim $path"
result
原文地址:https://www.cnblogs.com/wangmo/p/7505334.html