防止 rsync 删除排除的目录

防止 rsync 删除排除的目录

我通过 Ansible 的同步模块具有以下任务定义:

synchronize: src='{{ local_app_path }}/.' dest='{{ remote_app_path }}/' perms=no owner=yes rsync_opts=--delete-after

运行此任务将生成以下命令:

rsync --delay-updates -FF --compress --archive --no-perms --rsh 'ssh  -S none -o StrictHostKeyChecking=no' --rsync-path="sudo rsync" --delete-after --out-format='<<CHANGED>>%i %n%L' "../src/." "[email protected]:/var/www/app/"

我的目录布局如下:

src/
    # ...
    app/
    test/
    node_modules/
    package.json
provisioning/
    # ...
    playbook.yml
.rsync-filter

这是我的.rsync 过滤器:

exclude /src/.env
exclude /src/node_modules

现在我希望运行我的同步任务并让服务器上的最终目录结构如下所示(注意缺少的node_modules文件夹):

app/
test/
package.json

这可行。但是,一旦我node_modules在服务器上创建了一个文件夹...

app/
test/
node_modules/
package.json

...然后再次运行同步,node_modules文件夹再次从服务器中删除(即使我已在 .rsync 过滤器中将其排除):

app/
test/
package.json

我希望node_modules文件夹保留在服务器上,因为我已将其列在我的文件夹中,.rsync-filter并且没有使用该--delete-excluded选项。

我如何防止我排除的文件/目录被 rsync 删除?

非常感谢你的帮助! :)

答案1

正是这个-FF导致了你的问题。这会阻止-rsync-filter文件被复制,正如 rsync 手册页所说...

MERGE-FILE FILTER RULES
       ...
       These per-directory rule files must be created on the sending side because it is the
       sending side that is being scanned for the available files to transfer. These rule files may also
       need to be transferred to the receiving side if you want them to affect what files don’t get deleted
       (see PER-DIRECTORY RULES AND DELETE below).

重点是引用部分的最后一句话。更改-FF-F可实现所需的行为。

答案2

我遇到了与 OP 作者非常类似的问题。这就是我发现的。但是,我还没有亲自尝试过。但它应该有效。

TLDR:我认为您需要在 Ansible 方面进行"--exclude=node_modules"补充。rsync_opts

如果你阅读rsync手册,你会看到这些选项的摘要

            --delete                delete extraneous files from dest dirs
            --delete-before         receiver deletes before xfer, not during
            --delete-during         receiver deletes during the transfer
            --delete-delay          find deletions during, delete after
            --delete-after          receiver deletes after transfer, not during
            --delete-excluded       also delete excluded files from dest dirs

这个选择--delete-excluded很有趣。

--delete-excluded
              In  addition  to deleting the files on the receiving side that
              are not on the sending side, this tells rsync to  also  delete
              any  files  on  the  receiving  side  that  are  excluded (see
              --exclude).  See the FILTER RULES section for a  way  to  make
              individual exclusions behave this way on the receiver, and for
              a way to protect files from --delete-excluded.   See  --delete
              (which is implied) for more details on file-deletion.

但你实际上并不需要它,当然,除非它引用了--exclude你需要的选项。

--exclude=PATTERN
              This option is a simplified form of the --filter  option  that
              defaults  to  an  exclude  rule  and  does  not allow the full
              rule-parsing syntax of normal filter rules.

              See the FILTER RULES section for detailed information on  this
              option.

添加"--exclude=node_modules"到您的rsync_opts

相关内容