rsync同步常用命令

如果你是一位运维工程师,你很可能会面对几十台,几百台甚至上千台服务器,除了批量操作外,环境同步和数据同步也是必不可少的技能。

说道同步,不得不提的利器就是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

[html]  view plain copy
  1. [root@centos65 liumengyang]# rsync -r /home/liumengyang  10.0.96.180:/home  
  2. root@10.0.96.180's password:   

[html]  view plain copy
  1. [root@centos65 liumengyang]# echo "hello world" >> test.txt  
  2. [root@centos65 liumengyang]#   
  3. [root@centos65 liumengyang]#   
  4. [root@centos65 liumengyang]# ll  
  5. total 236  
  6. -rw-r--r-- 1 root root 235482 Jul  8 14:02 ls_orchid.gbk  
  7. -rw-r--r-- 1 root root     12 Sep 25 14:09 test.txt  
ip:remote

[html]  view plain copy
  1. [root@localhost liumengyang]# ll  
  2. total 236  
  3. -rw-r--r--. 1 root root 235482 Sep 25 14:10 ls_orchid.gbk  
  4. -rw-r--r--. 1 root root     31 Sep 25 14:10 test.txt  
  5. [root@localhost liumengyang]#   
  6. [root@localhost liumengyang]#   
  7. [root@localhost liumengyang]#   
  8. [root@localhost liumengyang]# cat test.txt   
  9. hello world  
  10.   
  11. 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:

[html]  view plain copy
  1. [root@centos65 liumengyang]# rsync -rt /home/liumengyang  10.0.96.180:/home  
  2. root@10.0.96.180's password:   
  3. [root@centos65 liumengyang]#   
  4. [root@centos65 liumengyang]#   
  5. [root@centos65 liumengyang]# ll  
  6. total 236  
  7. -rw-r--r-- 1 root root 235482 Jul  8 14:02 ls_orchid.gbk  
  8. -rw-r--r-- 1 root root     31 Sep 25 14:10 test.txt  
remote:

[html]  view plain copy
  1. [root@localhost liumengyang]#   
  2. [root@localhost liumengyang]# ll  
  3. total 236  
  4. -rw-r--r--. 1 root root 235482 Jul  8 14:02 ls_orchid.gbk  
  5. -rw-r--r--. 1 root root     31 Sep 25 14:10 test.txt  
  6. [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:

[html]  view plain copy
  1. [root@centos65 liumengyang]# ln -s test.txt test   
  2. [root@centos65 liumengyang]# ll  
  3. total 236  
  4. -rw-r--r-- 1 root root 235482 Jul  8 14:02 ls_orchid.gbk  
  5. lrwxrwxrwx 1 root root      8 Sep 25 14:51 test -> test.txt  
  6. -rw-r--r-- 1 root root     31 Sep 25 14:10 test.txt  

[html]  view plain copy
  1. [root@centos65 liumengyang]# rsync -rtl /home/liumengyang  10.0.96.180:/home  


remote:
[html]  view plain copy
  1. [root@localhost liumengyang]# ll  
  2. total 236  
  3. -rw-r--r--. 1 root root 235482 Jul  8 14:02 ls_orchid.gbk  
  4. lrwxrwxrwx. 1 root root      8 Sep 25 14:51 test -> test.txt  
  5. -rw-r--r--. 1 root root     31 Sep 25 14:10 test.txt  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值