rsync同步数据

rsync数据同步工具

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。

如果你的linux没有rsync命令请使用 yum install -y rsync 安装。

其使用语法如下

  • rsync [OPTION]... SRC DEST
  • rsync [OPTION]... SRC [USER@]host:DEST
  • rsync [OPTION]... [USER@]HOST:SRC DEST
  • rsync [OPTION]... [USER@]HOST::SRC DEST
  • rsync [OPTION]... SRC [USER@]HOST::DEST
  • rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

** rsync常用选项**

  • -a 归档模式,表示以递归方式传输文件,并保持所有属性,等同于-rlptgoD, -a选项后面可以跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个例如 -a--no-l 等同于-rptgoD

  • -r 对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项

  • -v 打印一些信息出来,比如速率,文件数量等

  • -l 保留软链结

  • -L 向对待常规文件一样处理软链结,如果是SRC中有软连接文件,则加上该选项后将会把软连接指向的目标文件拷贝到DST

  • -p 保持文件权限

  • -o 保持文件属主信息

  • -g 保持文件属组信息

  • -D 保持设备文件信息

  • -t 保持文件时间信息

  • --delete 删除那些DST中SRC没有的文件

  • --exclude=PATTERN 指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)

  • --progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等

  • -u 加上这个选项后将会把DST中比SRC还新的文件排除掉,不会覆盖

举例分析:

[root@localhost test]# rsync -av /tmp/1.txt /tmp/test2/
sending incremental file list
created directory /tmp/test2
1.txt

sent 759 bytes  received 68 bytes  1,654.00 bytes/sec
total size is 669  speedup is 0.81
[root@localhost test]# cd /tmp/test2
[root@localhost test2]# ls
1.txt
  • 上面这就是语法中的第一种,本地同步文件,-v选项就是现实速率,文件大小等
[root@localhost ~]# rsync -avl /tmp/ root@192.168.254.130:/tmp/
Enter passphrase for key '/root/.ssh/id_rsa': 
root@192.168.254.130's password: 
Permission denied, please try again.
root@192.168.254.130's password: 
sending incremental file list
./
1.txt
.ICE-unix/
.Test-unix/
.X11-unix/
.XIM-unix/
.font-unix/
test/
test/1.txt
test2/
test2/1.txt

sent 2,451 bytes  received 114 bytes  394.62 bytes/sec
total size is 2,007  speedup is 0.78
  • 上面这是语法中的第二种,将本地的/tmp/目录下的文件同步到远程192.168.254.130的/tmp/下,root@是以root的身份,不加这个默认也是root。
[root@localhost tmp]# rsync -avrl root@192.168.254.130:/tmp/ /tmp/

root@192.168.254.130's password: 
receiving incremental file list
./

sent 34 bytes  received 333 bytes  56.46 bytes/sec
total size is 2,022  speedup is 5.51
[root@localhost tmp]# ls
1.txt  2.txt  test  test2
[root@localhost tmp]# ll 2.txt 
lrwxrwxrwx. 1 root root 15 7月  29 17:08 2.txt -> /etc/passwd.old
  • 在这个例子中我们发现,虽然我们成功的将远程机器的/tmp目录同步了过来,且软链接2.txt也同步了过来,但是软链接是报错的,也就是2.txt指向的/etc/passwd.old文件在本地并没有,如果要在数据同步时将/etc/passwd.old这样的软链接文件一起同步,需要使用-L选项。

** 当我们需要使用脚本自动同步远程数据时,脚本中无法根据提示输入远程主机的密码,就需要使用秘钥登录,且秘钥密码为空。** 秘钥登录的配置方法之前介绍过就不再赘述了。

[root@localhost ~]# rsync -avl root@192.168.254.130:/tmp/ /tmp/
receiving incremental file list
./
2.txt -> /etc/passwd.old
asound.conf
dnsmasq.conf
dracut.conf
e2fsck.conf
host.conf
kdump.conf
krb5.conf
ld.so.conf
libaudit.conf
libuser.conf
locale.conf
logrotate.conf
man_db.conf
mke2fs.conf
nsswitch.conf
resolv.conf
rsyncd.conf
rsyslog.conf
sestatus.conf
sudo-ldap.conf
sudo.conf
sysctl.conf
updatedb.conf
vconsole.conf
yum.conf

sent 512 bytes  received 57,070 bytes  38,388.00 bytes/sec
total size is 57,111  speedup is 0.99
  • --delete 选项 删除那些DST中SRC没有的文件
[root@llll tmp]# cp /etc/*.conf /tmp/
[root@llll tmp]# ls
1.txt        dnsmasq.conf  host.conf   ld.so.conf     locale.conf     mke2fs.conf    rsyncd.conf    sudo.conf       test           vconsole.conf
2.txt        dracut.conf   kdump.conf  libaudit.conf  logrotate.conf  nsswitch.conf  rsyslog.conf   sudo-ldap.conf  test2          yum.conf
asound.conf  e2fsck.conf   krb5.conf   libuser.conf   man_db.conf     resolv.conf    sestatus.conf  sysctl.conf     updatedb.conf
[root@llll tmp]# rsync -avl --delete root@192.168.254.100:/tmp/ /tmp/
Enter passphrase for key '/root/.ssh/id_rsa': 
root@192.168.254.100's password: 
Permission denied, please try again.
root@192.168.254.100's password: 
receiving incremental file list
deleting yum.conf
deleting vconsole.conf
deleting updatedb.conf
deleting sysctl.conf
deleting sudo.conf
deleting sudo-ldap.conf
deleting sestatus.conf
deleting rsyslog.conf
deleting rsyncd.conf
deleting resolv.conf
deleting nsswitch.conf
deleting mke2fs.conf
deleting man_db.conf
deleting logrotate.conf
deleting locale.conf
deleting libuser.conf
deleting libaudit.conf
deleting ld.so.conf
deleting krb5.conf
deleting kdump.conf
deleting host.conf
deleting e2fsck.conf
deleting dracut.conf
deleting dnsmasq.conf
deleting asound.conf
./

sent 34 bytes  received 329 bytes  19.62 bytes/sec
total size is 2,022  speedup is 5.57
[root@llll tmp]# ls
1.txt  2.txt  test  test2

在使用ssh的方式远程同步数据的时候,如果ssh服务更改过端口了,则需要指定端口

rsync -avl -e "ssh -port 22" root@192.168.254.100:/tmp/ /tmp/ 就可以了。

转载于:https://my.oschina.net/u/3731306/blog/1917466

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值