【发布时间】:2019-01-27 23:40:39
【问题描述】:
我想编写一个 shell 脚本来使用 rsync 在名为 source 和 destination 的文件夹之间传输文件。
source文件夹的内容
A
B
C
test1.log
test2.json
我想将所有文件从source转移到destination,并想删除除test1.log和test2.json之外的所有源文件
即在将所有文件上传到目的地后, source 文件夹的内容应该是:
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.log 和 test2.json 被排除在上传之外。
但我希望所有文件都上传到目的地,其中大部分在上传后从源中删除,但希望在源中保留少量文件。
rsync 有什么办法吗?
【问题讨论】:
-
rsync从不从源目录中删除某些内容(请参阅删除手册页)。如果您想从源中删除它,请在之后使用rm命令。 -
来自 rsync 手册页:--remove-source-files - 发件人删除同步文件(非目录)。如果使用--remove-source-files,一旦发件人发送文件,这些文件就会从源文件夹中删除。我认为不需要使用
rm命令。 -
糟糕,我忽略了这部分。感谢您指出这一点。但是,根据我对 rsync 手册页的理解,这将删除所有已传输的文件。你不能选择之后只删除其中的一部分。
标签: linux shell networking server rsync