2014-12-24 28 views
7

我正在開發一個可以從多臺機器收集日誌的python腳本。我正在使用rsync。但是有一個問題。我有多個服務的日誌,如下所示:rsync跳過源上的非現有文件

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

此代碼中指定了此文件和文件夾的路徑。但是有時我會遇到一些日誌文件尚不存在的情況。並且rsync不能成功完成。
如何跳過源機器上不存在的文件?
P.S.我使用saltstack統治機器,所以我呼籲:

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

也許最好使用一個嘗試/除了 –

+0

@PadraicCunningham有沒有例外。我稱之爲cli工具,它只是不返回ret_code 0. –

+0

你怎麼使用它?我以爲你可能正在使用subprocess或pexpect –

回答

11

使用--ignore-missing-args

版本3.1.0手冊頁說,

- 忽略失蹤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年添加。

+1

Thanx!我得到了3.0.6所以我沒有在我的手冊頁中找不到這個選項。 –