1 rsync本来不能侦测文件改名
ubuntu中默认安装的rsync是在同步文件的时候,当文件发生删除、修改、创建的时候,都可以侦测到。但是当同步源文件改名时,rsync同步到目标文件时不是简单的改名,而是会先删除原有文件然后再重新传输文件过去,当文件很大的时候,这种方案会比较耗时。如果rsync能够自动侦测到原文件改名并自动将目标文件改名,则这个过程就会是瞬间完成。
2 给rsync打detect-renamed补丁
困扰我们的问题也会困扰别人,已经有大牛为rsync开发了侦测改名的补丁。到网址https://www.samba.org/ftp/rsync/上面下载rsync-3.1.3.tar.gz及rsync-patches-3.1.3.tar.gz文件,将它们放到一个目录下然后再解压缩,将会都解压到rsync-3.1.3目录下。进入到该目录,执行:
$ patch -p1 <patches/detect-renamed.diff
patching file compat.c
Hunk #2 succeeded at 124 (offset -1 lines).
patching file flist.c
Hunk #1 succeeded at 60 (offset -1 lines).
Hunk #2 succeeded at 121 (offset -1 lines).
Hunk #3 succeeded at 290 (offset -1 lines).
Hunk #4 succeeded at 2469 (offset -24 lines).
patching file generator.c
Hunk #1 succeeded at 78 (offset -1 lines).
Hunk #2 succeeded at 97 (offset -1 lines).
Hunk #3 succeeded at 105 (offset -1 lines).
Hunk #4 succeeded at 116 (offset -1 lines).
Hunk #5 succeeded at 125 (offset -1 lines).
Hunk #6 succeeded at 150 (offset -1 lines).
Hunk #7 succeeded at 291 (offset -1 lines).
Hunk #8 succeeded at 348 (offset -1 lines).
Hunk #9 succeeded at 369 (offset -1 lines).
Hunk #10 succeeded at 411 (offset -1 lines).
Hunk #11 succeeded at 577 (offset -1 lines).
Hunk #12 succeeded at 602 (offset -1 lines).
Hunk #13 succeeded at 627 (offset -1 lines).
Hunk #14 succeeded at 647 (offset -1 lines).
Hunk #15 succeeded at 659 (offset -1 lines).
Hunk #16 succeeded at 704 (offset -1 lines).
Hunk #17 succeeded at 1417 (offset -9 lines).
Hunk #18 succeeded at 1707 (offset -9 lines).
Hunk #19 succeeded at 1996 (offset -9 lines).
Hunk #20 succeeded at 2412 (offset -9 lines).
Hunk #21 succeeded at 2428 (offset -9 lines).
Hunk #22 succeeded at 2470 (offset -9 lines).
Hunk #23 succeeded at 2517 (offset -9 lines).
patching file options.c
Hunk #1 succeeded at 81 (offset -1 lines).
Hunk #2 succeeded at 391 (offset -2 lines).
Hunk #3 succeeded at 579 (offset -5 lines).
Hunk #4 succeeded at 1604 (offset -5 lines).
Hunk #5 succeeded at 1613 (offset -5 lines).
Hunk #6 succeeded at 1976 (offset -7 lines).
patching file rsync.yo
Hunk #1 succeeded at 402 (offset -1 lines).
Hunk #2 succeeded at 1591 (offset -7 lines).
patching file syscall.c
patching file util.c
Hunk #1 succeeded at 1168 (offset -4 lines).
Hunk #2 succeeded at 1179 (offset -4 lines).
patching file proto.h
patching file rsync.1
Hunk #1 succeeded at 478 (offset -1 lines).
Hunk #2 succeeded at 1821 (offset -8 lines).
然后再执行:
$ patch -p1 <patches/detect-renamed-lax.diff
patching file generator.c
patching file options.c
patching file rsync.yo
patching file rsync.1
然后再执行下列命令:
$ sudo ./configure
$ sudo make
$ sudo make install
然后再查看rsync版本是否变成3.1.3:
root@server-1:~/rsync-3.1.3# rsync --version
rsync version 3.1.3 protocol version 31
Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, no ACLs, xattrs, iconv, symtimes, prealloc
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
说明rsync打补丁并安装成功。
查看rsync的--detect-renamed参数:
-y, --fuzzy find similar file for basis if no dest file
--detect-renamed try to find renamed files to speed up the transfer
--detect-renamed-lax ... and assume identical to source files (risky!)
--detect-moved ... only if basenames match (less risky)
这里说了这个参数能够让rsync在检测到文件改名以后加速传输过程。
3 同步文件测试
同步命令加上--detect-renamed参数。
src=/var/log/nginx
dst=lzj@192.168.1.85::ftp
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,create,delete,attrib $src | while read files
do
rsync -vzrtopg --delete-delay --progress --exclude "*.gz" --password-file=/etc/rsync.pass $src $dst --detect-renamed
done
exit 0
现在测试文件改名。将源文件夹中的文件改名:
ubuntu@ip-172-31-26-91:/var/log/nginx$ ls
access.log access.log.12.gz access.log.2.gz access.log.4.gz access.log.7.gz error.log error.log.2.gz error.log.5.gz error.log.8.gz
access.log.10.gz access.log.13.gz access.log.3.gz access.log.5.gz access.log.8.gz error.log.1 error.log.3.gz error.log.6.gz error.log.9.gz
access.log.11.gz access.log.14.gz access.log.4 access.log.6.gz access.log.9.gz error.log.10.gz error.log.4.gz error.log.7.gz
ubuntu@ip-172-31-26-91:/var/log/nginx$ sudo mv access.log.4 access.log.5
inofify监听到的事件:
sent 215 bytes received 19 bytes 156.00 bytes/sec
total size is 9,637,614 speedup is 41,186.38
building file list ...
5 files to consider
nginx/
nginx/access.log
284,503 100% 90.22MB/s 0:00:00 (xfr#1, to-chk=3/5)
nginx/access.log.5
9,353,941 100% 46.70MB/s 0:00:00 (xfr#2, to-chk=2/5)
deleting nginx/access.log.4
正常的传输过程就200多K每秒,这里的传输速度达到了46M/s,是因为rsync侦测到改名以后加速了传输过程。