【问题标题】:rsync skip non existing files on sourcersync 跳过源上不存在的文件
【发布时间】:2015-02-22 12:37:46
【问题描述】:

我正在用 python 开发一个可以从多台机器收集日志的脚本。我正在使用rsync。但是有一个问题。我有多个服务的日志,如下所示:

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

此文件和文件夹的路径在代码中指定。但有时我会遇到一些日志文件还不存在的情况。并且 rsync 没有成功完成。
如何跳过源计算机上不存在的文件?
附言我正在使用 saltstack 来统治机器,所以我调用:

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

【问题讨论】:

  • 最好使用 try/except
  • @PadraicCunningham 也不例外。我称它为 cli 实用程序,它只是不返回 ret_code 0。
  • 你是如何使用它的?我以为你可能在使用 subprocess 或 pexpect
  • @PadraicCunningham 抱歉,我忘了把它包含在我的帖子中。我正在使用 SaltStack。我做 __salt__['cmd.retcode']('rsync -a file1 file2..')
  • 你能复制目录而不是单个文件吗?

标签: python linux rsync


【解决方案1】:

使用--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 之间的某个时间添加的。

online version 3.06 rsync man page 没有提及。但the git repository 表示此选项是在 2009 年添加的。

【讨论】:

  • 谢谢!我得到了 3.0.6,所以我没有在我的手册页中找到这个选项。
  • --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) [with rsync v. 3.1.2 协议版本31]
  • 根据新闻文件(download.samba.org/pub/rsync/NEWS#3.1.0),选项是在3.1.0版本中添加的。
猜你喜欢
  • 2012-03-02
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多