仅当有传输时才进行 cron 日志 rsync

仅当有传输时才进行 cron 日志 rsync

我每 5 分钟运行一次这个 crontab:

date >> ~/18/rsync.log
rsync -vaz user@r18:~/assets ~/18 >> ~/18/rsync.log 2>&1

它每 5 分钟将其添加到日志文件中:

Thu Aug 16 13:00:01 MSK 2012
receiving incremental file list

sent 506 bytes  received 541488 bytes  361329.33 bytes/sec
total size is 12954651209  speedup is 23901.84

有时它会添加实际的传输日志:

Thu Aug 16 13:10:01 MSK 2012
receiving incremental file list
assets/response/20120816/
assets/response/20120816/1017161.doc
assets/response/20120816/1017162.doc
assets/response/20120816/1017163.doc

sent 568 bytes  received 561686 bytes  1124508.00 bytes/sec
total size is 12954864201  speedup is 23040.95

我想忽略空传输日志并保留实际传输列表。有没有办法配置 rsync 以仅在非空传输上生成详细输出?

答案1

不要使用 rsync 的 -v 选项,而是使用 --out-format:

rsync --out-format="%n%L" -az user@r18:~/assets ~/18 >> ~/18/rsync.log 2>&1

仅在传输文件时才会有输出,不传输文件时则不会有输出。若要获得更多输出,请查看 rsync.conf 手册页中的“日志格式”部分。

答案2

请检查 rsync --log-file-format 和 --log-file 开关。即使不传输任何内容,默认日志文件格式也会向日志文件添加 2 行,但请检查手册。也许如果您更改日志格式,将只添加包含传输文件的条目。

答案3

好的,我编写了一些巧妙的 shell 脚本来过滤来自 rsync 的空传输的无意义垃圾邮件。如果您知道任何更好的检测方法,请添加您的答案。

#!/bin/sh

LOG=$HOME/18/sync.log
TMP=$HOME/18/temp.log
SRC=user@r18:~/assets
DST=$HOME/18

echo >> $TMP
date >> $TMP
rsync -az $SRC $DST --log-file=$TMP --log-file-format='%10l %n%L'
[ `cat $TMP | wc -l` != 4 ] && cat $TMP >> $LOG
rm $TMP

答案4

你最好使用观察者每当文件发生变化时运行rsync。它基于incron但可以递归监视目录。

$ git clonet git://github.com/splitbrain/Watcher.git
$ cd Watcher
$ cp watcher.ini ~/.watcher.ini

~/.watcher.ini

[DEFAULT]
logfile=/tmp/watcher.log
pidfile=/tmp/watcher.pid
[job1]
watch=/home/quanta/x
events=create,delete
recursive=true
autoadd=true
command=rsync -vaz quanta@localhost:~/x ~/y --log-file=/home/quanta/rsync.log

启动守护进程:

$ ./watcher start
$ ps -ef | grep [w]atcher
quanta    3695     1  0 17:01 ?        00:00:00 /usr/bin/python2.7 ./watcher.py restart

rsync.log

2012/08/16 17:01:42 [3710] receiving file list
2012/08/16 17:01:42 [3724] .d..t...... x/
2012/08/16 17:01:42 [3724] >f.st...... x/a.txt
2012/08/16 17:01:42 [3724] sent 42 bytes  received 180 bytes  444.00 bytes/sec
2012/08/16 17:01:42 [3724] total size is 45  speedup is 0.20
2012/08/16 17:01:42 [3731] receiving file list
2012/08/16 17:01:42 [3745] sent 14 bytes  received 103 bytes  234.00 bytes/sec
2012/08/16 17:01:42 [3745] total size is 45  speedup is 0.38
2012/08/16 17:01:42 [3752] receiving file list
2012/08/16 17:01:42 [3766] sent 14 bytes  received 103 bytes  234.00 bytes/sec
2012/08/16 17:01:42 [3766] total size is 45  speedup is 0.38
2012/08/16 17:01:42 [3773] receiving file list
2012/08/16 17:01:42 [3787] sent 14 bytes  received 103 bytes  234.00 bytes/sec
2012/08/16 17:01:42 [3787] total size is 45  speedup is 0.38

相关内容