您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页rsync上传文件bash脚本

rsync上传文件bash脚本

来源:爱玩科技网

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

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

 

分别上传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

 

转载于:https://www.cnblogs.com/antarctican/p/5133170.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务