如果你是一位运维工程师,你很可能会面对几十台,几百台甚至上千台服务器,除了批量操作外,环境同步和数据同步也是必不可少的技能。
说道同步,不得不提的利器就是rsync,今天就说说我们从这个工具中看到的同步艺术。
【不带任何选项】
我们经常这样使用rsync:
$ rsync main.c machineB:/home/userB
1 只要目的端的问价内容和源端不一样,就会触发数据同步,rsync会确保两边的文件内容一样
2 但是rsync不会同步文件的modify time,凡是有数据同步的文件,目的端的文件总会被修改为最新时刻的时间
3 rsync不会太关注目的端文件的rwx权限,如果目的端没有此文件,那么权限会保持与源端一致;如果目的端有此文件,则权限不会随着源端改变
4 只要rsync有对源文件的读权限,且对目标路径有写权限,rsync就能确保目的端文件同步到源端一致。
5 rsync只能以登录目的端的账号来创建文件,它没有能力保持目的端文件的属主和属组和源端一致。
用例【-r选项】:
ip:local
- [root@centos65 liumengyang]# rsync -r /home/liumengyang 10.0.96.180:/home
- root@10.0.96.180's password:
- [root@centos65 liumengyang]# echo "hello world" >> test.txt
- [root@centos65 liumengyang]#
- [root@centos65 liumengyang]#
- [root@centos65 liumengyang]# ll
- total 236
- -rw-r--r-- 1 root root 235482 Jul 8 14:02 ls_orchid.gbk
- -rw-r--r-- 1 root root 12 Sep 25 14:09 test.txt
ip:remote
- [root@localhost liumengyang]# ll
- total 236
- -rw-r--r--. 1 root root 235482 Sep 25 14:10 ls_orchid.gbk
- -rw-r--r--. 1 root root 31 Sep 25 14:10 test.txt
- [root@localhost liumengyang]#
- [root@localhost liumengyang]#
- [root@localhost liumengyang]#
- [root@localhost liumengyang]# cat test.txt
- hello world
- nice to meet you!
备注:当文件中的内容发生改变的时候,运用上述命令,远程服务器中的文件也会相应的改变,modify time会记录文件的最新修改时间。
【-t选项】
我们经常这样使用-t选项
$ rsync -t main.c machineB:/home/userB
1 使用-t选项后,rsync总会想着一件事,那就是将源文件的"modify time"同步到目标机器。
2 带有-t选项rsync,会变得聪明一些,它会在同步之前先对比两边文件的时间戳和文件大小,如果一致,则就认为两边的文件一致,对此文件就不在采取更新行为了。
3 因为rsync的聪明,也会反被聪明误。如果目的端的文件的时间戳和文件大小一致,但是文件的内容不是一致的,rsync发现不了并且不进行更新。这就是个bug.
4 对于rsync自作聪明的做饭,解决方法就是【-I】 选项了。
使用实例:
local:
- [root@centos65 liumengyang]# rsync -rt /home/liumengyang 10.0.96.180:/home
- root@10.0.96.180's password:
- [root@centos65 liumengyang]#
- [root@centos65 liumengyang]#
- [root@centos65 liumengyang]# ll
- total 236
- -rw-r--r-- 1 root root 235482 Jul 8 14:02 ls_orchid.gbk
- -rw-r--r-- 1 root root 31 Sep 25 14:10 test.txt
remote:
- [root@localhost liumengyang]#
- [root@localhost liumengyang]# ll
- total 236
- -rw-r--r--. 1 root root 235482 Jul 8 14:02 ls_orchid.gbk
- -rw-r--r--. 1 root root 31 Sep 25 14:10 test.txt
- [root@localhost liumengyang]#
经过对比,源端和目的端的时间戳一致!
【-I选项】
$ rsync -I main.c machineB:/home/userB
1 -I选项会让rsync变得乖巧老实,它会挨个文件发起数据同步
2 -I选项可以确保数据的一致性,但是会有相应的代价,那就是速度会相应变慢,因为我们放弃了"quick check"策略
3 无论情况如何,目的端的文件的modify time总会被更新到当前时刻
【-v选项】
这个选项简单易懂,就是让rsync输出更多的信息,我们可以举一个例子:
$ rsync -vI main.c machineB:/home/userB main.c sent 81 bytes received 42 bytes 246.00 bytes/sec total size is 11 speedup is 0.09
你增加越多的v,就可以获得越多的日志信息。
$ rsync -vvvvt abc.c machineB:/home/userB cmd= machine=machineB user= path=/home/userB cmd[0]=ssh cmd[1]=machineB cmd[2]=rsync cmd[3]=--server cmd[4]=-vvvvte. cmd[5]=. cmd[6]=/home/userB opening connection using: ssh machineB rsync --server -vvvvte. . /home/userB note: iconv_open("ANSI_X3.4-1968", "ANSI_X3.4-1968") succeeded. (Client) Protocol versions: remote=28, negotiated=28 (Server) Protocol versions: remote=30, negotiated=28 [sender] make_file(abc.c,*,2) [sender] flist start=0, used=1, low=0, high=0 [sender] i=0 abc.c mode=0100664 len=11 flags=0 send_file_list done file list sent send_files starting server_recv(2) starting pid=31885 recv_file_name(abc.c) received 1 names [receiver] i=0 abc.c mode=0100664 len=11 recv_file_list done get_local_name count=1 /home/userB recv_files(1) starting generator starting pid=31885 count=1 delta transmission enabled recv_generator(abc.c,0) abc.c is uptodate generate_files phase=1 send_files phase=1 recv_files phase=1 generate_files phase=2 send files finished total: matches=0 hash_hits=0 false_alarms=0 data=0 generate_files finished recv_files finished client_run waiting on 14318 sent 36 bytes received 16 bytes 104.00 bytes/sec total size is 11 speedup is 0.21 _exit_cleanup(code=0, file=main.c, line=1031): entered _exit_cleanup(code=0, file=main.c, line=1031): about to call exit(0)
【-z选项】
这个是压缩选项,只要使用了这个选项,rsync就会把发向对端的数据先进行压缩再进行传输。对于网络传输速度较慢的情况下建议使用。
一般情况下,-z的压缩算法会和gzip的一样。
【-l选项】
如果我们要同步一个软链接文件,你猜rsync会提示什么:
$ ll total 128 -rw-rw-r-- 1 userA userA 11 Dec 26 07:00 abc.c lrwxrwxrwx 1 userA userA 5 Dec 26 11:35 softlink -> abc.c $ rsync softlink machineB:/home/userB skipping non-regular file "softlink"
rsync拒绝了我们的请求。一旦发现某个文件是个软连接,就会无视它,除非增加-l选项
使用实例:
local:
- [root@centos65 liumengyang]# ln -s test.txt test
- [root@centos65 liumengyang]# ll
- total 236
- -rw-r--r-- 1 root root 235482 Jul 8 14:02 ls_orchid.gbk
- lrwxrwxrwx 1 root root 8 Sep 25 14:51 test -> test.txt
- -rw-r--r-- 1 root root 31 Sep 25 14:10 test.txt
- [root@centos65 liumengyang]# rsync -rtl /home/liumengyang 10.0.96.180:/home
remote:
- [root@localhost liumengyang]# ll
- total 236
- -rw-r--r--. 1 root root 235482 Jul 8 14:02 ls_orchid.gbk
- lrwxrwxrwx. 1 root root 8 Sep 25 14:51 test -> test.txt
- -rw-r--r--. 1 root root 31 Sep 25 14:10 test.txt
未经允许不得转载:OZ分享-吉家大宝官方博客 » rsync同步常用命令