有没有什么办法可以从一个bash脚本知道什么时候一个rsync命令不下载任何东西(即文件夹是同步的),我试过使用退出代码,但只有当错误发生时才会有所不同。
rsync的例子只下载不同步的文件:
$ rsync -av –progress humbo @ hkremote:/ home / humbo / dumps / *。gz。
receiving file list ... 9 files to consider _dump_7.gz 35927279 100% 1.19MB/s 0:00:28 (xfer#1, to-check=1/9) _dump_8.gz 39387704 100% 1.17MB/s 0:00:32 (xfer#2, to-check=0/9) sent 64 bytes received 75324472 bytes 1205192.58 bytes/sec total size is 354448073 speedup is 4.71
$ echo $?
0
档案同步:
$ rsync -av –progress humbo @ hkremote:/ home / humbo / dumps / *。gz。
receiving file list ... 9 files to consider sent 20 bytes received 205 bytes 90.00 bytes/sec total size is 354448073 speedup is 1575324.77
$ echo $?
0
您可以使用rsync --stats然后使用grep "Number of files transferred" ,然后用awk打印这个统计数据,方法如下:
rsync -av --stats --progress humbo@hkremote:/home/humbo/dumps/*.gz . | grep 'Number of files transferred' | awk -F ": " '{print $2}'
当没有任何东西被传输时,这个结果是0。
Rsync没有退出代码,当它运行一个副本不同,它不会复制任何东西。 退出代码仅适用于导致复制问题的故障。 你将不得不围绕bash脚本中的函数编写一些逻辑来判断它是否没有复制任何文件。
以前从来没有这样做,但如果你的目录不是太大的话,它可能会工作。
在启动rsync之前创build一个md5sum ,并在完成之后与md5sum进行比较。 像这样的东西:
#!/bin/bash md5sum=$(ssh humbo@hkremote tar -c /home/humbo/dumps|md5sum) . . # The rest of your script goes here . . md5sum_after_rsync=$(ssh humbo@hkremote tar -c /home/humbo/dumps|md5sum) if test $md5sum == $md5sum_after_rsync then echo "nothing has changed" else echo "something has changed" fi
你可以使用rsync -i 。 空输出意味着没有变化。
或者rsync --stats和parsing输出…
运行rsync -n ,你会得到不同步的文件列表:
[alexus@wcmisdlin02 Desktop]$ rsync --help | grep dry-run -n, --dry-run perform a trial run with no changes made [alexus@wcmisdlin02 Desktop]$