更新代码脚本

#!/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 "\033[31m$1 $faild\033[0m" && exit 1
    else
        echo -e "\033[32m$1 $ok\033[0m"
    fi
}

#usage: $1 is remote IP, $2 is ProjectName, $3 is Tag ID
if [ $# -lt 3 ];then
    echo -e "\033[31m usage: sh $0 IP ProjectName Tag_ID\033[0m" 
    echo -e "\033[31m example: sh $0 172.16.xx.xx YiZhuang 亦庄OA-1.0.2.201710.2\033[0m" 
    echo -e "\033[31m explain: 升级主机要配置ssh免密,可以使用auto_ssh.sh \033[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 "\033[31mTag $3 not exist\033[0m" && exit 3
    fi
else
    echo -e "\033[31m$code not exist\033[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 "\033[31m$1 $path not exist !\033[0m" && exit 5
    fi
else
    echo -e "\033[31m$1 network unreachable !\033[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

 

posted @ 2017-09-11 15:48  wangmo  阅读(210)  评论(0编辑  收藏  举报