linux - 同步: dont delete few files in source after upload

标签 linux shell networking server rsync

我想编写一个 shell 脚本来使用 rsync 在名为 sourcedestination 的文件夹之间传输文件。

文件夹的内容

A
B
C
test1.log
test2.json

我想将所有文件从传输到目标,并希望删除除test1.logtest2.json之外的所有源文件

即将所有文件上传到目的地后, 文件夹的内容应为:

test1.log
test2.json

目标文件夹的内容应该是:

    A
    B
    C
test1.log
test2.json

我尝试过

rsync -aP --timeout=120 --remove-source-files -e "ssh -o StrictHostKeyChecking=no -i /id" source user@1.1.1.1:/destination_path/ --exclude=test1.log --exclude=test2.json

但在这种情况下,test1.logtest2.json 将被排除在上传之外。

但我希望所有文件都上传到目的地,其中大多数文件在上传后从源中删除,但希望在源中保留少量文件。

有什么办法可以用rsync来做到这一点吗?

最佳答案

正如user1934428指出的,如果我使用--remove-source-files,则无法保持某些文件在传输后不被删除。 因此,我最终使用了以下解决方案,该解决方案效率不高,因为必须调用 rsync 两次。

rsync -aP --timeout=120 --remove-source-files -e "ssh -o StrictHostKeyChecking=no -i /id" source user@1.1.1.1:/destination_path/ --exclude=test1.log --exclude=test2.json

rsync -aP --timeout=120 -e "ssh -o StrictHostKeyChecking=no -i /id" source user@1.1.1.1:/destination_path/ 

关于linux - 同步: dont delete few files in source after upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954516/

相关文章:

linux - Magento 在实时服务器上移动媒体文件夹后产品图像未显示在前端和后端

Linux 设备树帮助(GPIO Controller /中断)

windows - 当您在 Windows 命令 shell 中移动文件时,目录级别会发生什么情况?

c - Google 环聊桌面应用流程

networking - 以太网和 OSI 模型

c++ - 理解用于实现的 select() 函数

c - C中的别名内存

linux - 如何克隆 OpenLDAP 数据库

通过 shell 脚本的 Java 命令行参数

shell - 在Shell脚本中的ssh session 中评估和分配变量