若是你的linux没有rsync命令请使用 yum install -y rsync 安装。linux
其使用语法以下算法
** rsync经常使用选项**ssh
-a 归档模式,表示以递归方式传输文件,并保持全部属性,等同于-rlptgoD, -a选项后面能够跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个例如 -a--no-l 等同于-rptgoD工具
-r 对子目录以递归模式处理,主要是针对目录来讲的,若是单独传一个文件不须要加-r,可是传输的是目录必须加-r选项unix
-v 打印一些信息出来,好比速率,文件数量等code
-l 保留软链结递归
-L 向对待常规文件同样处理软链结,若是是SRC中有软链接文件,则加上该选项后将会把软链接指向的目标文件拷贝到DSTdns
-p 保持文件权限rem
-o 保持文件属主信息get
-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
[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
[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
** 当咱们须要使用脚本自动同步远程数据时,脚本中没法根据提示输入远程主机的密码,就须要使用秘钥登陆,且秘钥密码为空。** 秘钥登陆的配置方法以前介绍过就再也不赘述了。
[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
[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/
就能够了。