rsync跳过源文件中不存在的文件

19

我正在使用Python编写一个脚本,可以从多台机器收集日志。我正在使用rsync。但是出现了一个问题。我有多个服务的日志看起来像:

service1.log
service2.log
service3.log
... and so on

代码中指定了这些文件和文件夹的路径。但有时我会遇到一些日志文件尚不存在的情况,而rsync则无法成功完成。
如何跳过源机器上不存在的文件?
附言:我正在使用saltstack来管理机器,所以我调用:

__salt__['cmd.retcode']('rsync -a file1 file2...')

最好使用try/except。 - Padraic Cunningham
@PadraicCunningham 抱歉,我忘记在我的帖子中包含它了。我正在使用SaltStack。我使用__salt__['cmd.retcode']('rsync -a file1 file2..')。 - oleg.foreigner
你能复制整个目录而不是单独的文件吗? - unutbu
@PadraicCunningham 是一个列表。我有大约10个服务,需要收集它们的日志。但是有时其中一些不会写任何日志,因此文件就不存在了。 - oleg.foreigner
@oleg.foreigner,有一个--ignore-non-existing标志可能有效。 - Padraic Cunningham
显示剩余11条评论
1个回答

34

使用--ignore-missing-args

版本3.1.0手册页指出,

--ignore-missing-args

 When rsync is first processing the explicitly requested
 source files (e.g. command-line arguments or --files-from
 entries), it is normally an error if the file cannot be
 found.  This option suppresses that error, and does not
 try to transfer the file.  This does not affect subsequent
 vanished-file errors if a file was initially found to be
 present and later is no longer there.
例如:

% rsync --version
rsync  version 3.1.0  protocol version 31
% rsync bogusfile dest
rsync: link_stat "/tmp/bogusfile" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.0]
% rsync --ignore-missing-args bogusfile dest
# <no error>

--ignore-missing-args选项是在版本3.06和3.1.0之间的某个时候添加的。

在线版本3.06的rsync手册没有提到它。但是git存储库表明该选项是在2009年添加的。


1
谢谢!我使用的是3.0.6版本,所以在我的man手册中没有找到这个选项。 - oleg.foreigner
7
当使用--ignore-missing-args参数时,如果源文件的父目录不存在,仍然会显示错误。例如:rsync --ignore-missing-args /tmp/bogusfile /newfile 将正常工作,因为 /tmp/ 存在。但是 rsync --ignore-missing-args /tmp/bogusdirectory/bogusfile /newfile 将失败,并显示如下错误信息: rsync: change_dir "/tmp/bogusdirectory" failed: No such file or directory (2) [使用 rsync v. 3.1.2 协议版本31]。 - Rafa
该选项是在版本3.1.0中添加的,根据新闻文件(https://download.samba.org/pub/rsync/NEWS#3.1.0)。 - Lacek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接