记一个困扰我 2 天的问题——rsync 命令 作者: Chuwen 时间: 2022-12-28 分类: Linux 评论 最近在忙着迁移服务器,流水线脚本没改,只是换了服务器,结果新服务器 `rsync` 有问题,不遵从 `--exclude=` 规则,导致 `.env` 文件被删除 但是手动拿出执行又是正常的 ## 服务器信息 **旧服务器**(使用*原脚本*运行正常) ```shell 18:30:41 root@shine-prod ~ lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 8.5.2111 Release: 8.5.2111 Codename: n/a 18:30:43 root@shine-prod ~ rsync --version rsync version 3.1.3 protocol version 31 Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes, prealloc rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. ``` **新服务器**(使用*原脚本*运行错误) ```shell root@shine-prod-1:~# lsb_release -a LSB Version: security-11.1.0ubuntu4-noarch Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS Release: 22.04 Codename: jammy root@shine-prod-1:~# rsync --version rsync version 3.2.3 protocol version 31 Copyright (C) 1996-2020 by Andrew Tridgell, Wayne Davison, and others. Web site: https://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, hardlink-specials, symlinks, IPv6, atimes, batchfiles, inplace, append, ACLs, xattrs, optional protect-args, iconv, symtimes, prealloc, stop-at, no crtimes Optimizations: SIMD, no asm, openssl-crypto Checksum list: xxh128 xxh3 xxh64 (xxhash) md5 md4 none Compress list: zstd lz4 zlibx zlib none rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. ``` ## 原脚本 注意看以下脚本中 `--exclude={'.git','.idea','storage','node_modules','.env','shell','docker-compose.yaml'}` 部分,这个如果是通过阿里云流水线-主机部署执行这条命令,不会起作用,但是你手动去执行又是正常的 导致把你的 `.env` 文件给删了,然后后置脚本就运行失败了 *就是因为这个把我折腾死的烦躁、怀疑人生* ```shell rm -rf $TMP_DEPLOY_PATH mkdir -p $TMP_DEPLOY_PATH mkdir -p $ES_STAGING_PATH tar zxvf "$package_download_path" -C "$TMP_DEPLOY_PATH" EXCLUDE_COMPOSER_ARG=$([ "$ES_UPDATE_COMPOSER" = "true" ] && echo "" || echo --exclude="vendor") ####### 预发布环境部署 rsync -zarp --delete \ --exclude={'.git','.idea','storage','node_modules','.env','shell','docker-compose.yaml'} \ $EXCLUDE_COMPOSER_ARG \ $TMP_DEPLOY_PATH $DEPLOY_STAGING_PATH cd $DEPLOY_STAGING_PATH || exit docker-compose exec app sh deploy/laravels.sh ``` ## 解决方案-现脚本 一次偶然的尝试,改成 `--exclude=".env"` ... 这样一个一个加上,发现脚本正常运行,达到我预期效果,`.env` 终于不会被删除了!!! ```shell rm -rf $TMP_DEPLOY_PATH mkdir -p $TMP_DEPLOY_PATH mkdir -p $ES_STAGING_PATH tar zxvf "$package_download_path" -C "$TMP_DEPLOY_PATH" >>/dev/null EXCLUDE_COMPOSER_ARG=$([ "$ES_UPDATE_COMPOSER" = "true" ] && echo "" || echo --exclude="vendor") #### 预发布环境部署 rsync -zarp --delete \ --exclude=".git" --exclude=".idea" --exclude=".env" \ --exclude="storage" --exclude="node_modules" --exclude="shell" \ --exclude="docker-compose.yaml" $EXCLUDE_COMPOSER_ARG \ $TMP_DEPLOY_PATH $DEPLOY_STAGING_PATH cd $DEPLOY_STAGING_PATH || exit docker compose exec -T app sh deploy/laravels.sh ``` --- 具体原因还未找到,等找到原因再更新文章