梦想的生活,就是去新西兰放羊

rsync上传文件bash脚本

 在更新代码到服务器的时候, 通常使用FTP、GIT之类的更新代码。 然而很多时候, 感觉并不好用

  FTP上传,总需要记得自己修改了什么,FTP工具得先浏览某目录,再上传; GIT固然是好, 可是又得在服务器上装Git配hook事件, 一台还好, 多了搞起麻烦。

  所以一般我就配合git用rsync命令更新代码,  windows开发环境蛋疼了,Git windows版也没带rsync命令——当然我的是mac。

 

 往往要将不同目录上传到不同服务器。经常我在每一个项目下写一个rsync.sh脚本。下面出一个合并版, 它们属于 同一 git项目下。

分别上传one、two、three、four...到不同服务器

 

tongbu.sh

#!/bin/bash
#
# rsync上传文件到服务器脚本
# 上传:
# ./tongbu.sh test --dry-run
# ./tongbu.sh three-test --dry-run
#
sudoPwd="123"
SOURCE_DIR="/web/wwwroot/jd.com/"
SSH_PEM="/web/ssh/my.pem"
PUBLIC_EXCLUDE="--exclude=tongbu.sh --exclude=nbproject --exclude=.DS_Store --exclude=.htaccess --exclude=*.txt --exclude=database.php --exclude=config --exclude=.git --exclude=.gitignore"

GitIsUpToDate=$(git status | grep -c 'branch is up-to-date with');
GitIsClean=$(git status | grep -c 'working directory clean');
if [ $GitIsClean = 0 ] || [ $GitIsUpToDate = 0 ]; then
    echo 'git工作目录有修改没有提交 或 不是 up-to-date 状态, 请先处理git代码!!';
    exit 0;
fi

# whether server is test
if [ "${1}" ] && [ "${1:(-4)}" = "test" ]; then 
    srcIsTest=true;
else 
    srcIsTest=false;
fi

case $1 in
    one | one-test)
        if $srcIsTest; then
            SOURCE_DIR=${SOURCE_DIR}""
            DEST='ec2-user@54.2.2.2:/var/www/html/jd.com/'
        else
            DEST='ec2-user@54.2.2.2:/var/www/html/jd.com/'
        fi
        echo $sudoPwd | rsync -rvu $2 --exclude=two --exclude=three --exclude=four --exclude=five --exclude=six --exclude=seven --exclude=eight --exclude=nine --exclude=ten --exclude=eleven --exclude=twelve --exclude=thirteen --exclude=fourteen --exclude=fiveteen --exclude=fifteen --exclude=sixteen --exclude=seventeen --exclude=eighteen --exclude=nineteen --exclude=tweenty $PUBLIC_EXCLUDE -e "ssh -i $SSH_PEM" $SOURCE_DIR $DEST
        ;;

    two | two-test)
        SOURCE_DIR=${SOURCE_DIR}"two/"
        if $srcIsTest; then
            DEST='/web/uploads/'
        else
            DEST='/web/uploads2/'
        fi
        echo $sudoPwd | rsync -rvu $2 $PUBLIC_EXCLUDE -e "ssh -i $SSH_PEM" $SOURCE_DIR $DEST
        ;;

    test)
        echo "开始以--dry-run方式测试同步操作,不上传....";
        DEST='/web/uploads/'
        echo $sudoPwd | rsync -rvu -n $PUBLIC_EXCLUDE -e "ssh -i $SSH_PEM" $SOURCE_DIR $DEST
        ;;

    version)
        echo "同步脚本 1.0.0";
        ;;

    *)
        echo '未知目录,不能同步!';
        ;;
esac

exit $?

 

./tongbu.sh  one  -dry-run

./tongbu.sh  one-test

 

posted @ 2016-01-15 14:12  Shautch Donne  阅读(676)  评论(0编辑  收藏  举报