rsync工具介绍 使用场景:A机器传输文件到B机器。或者备份数据到远程机器。本机的A目录到B目录,其中A目录数据不断的增加,每小时备份到B机器。使用cp显然不合适。 •rsync -av /etc/passwd /tmp/1.txt //-a 保含了好几个选项 • rsync格式 本地目录 目标目录 • rsync [OPTION] … SRC DEST • rsync [OPTION] … SRC [user@]host:DEST rsync -av /tmp/1.txt root@192.168.188.128:/tmp/2.txt //不写用户就是当前的用户,对方机器没有会报错

• rsync [OPTION] … [user@]host:SRC DEST //远程目录拷贝到本地目录下 • rsync [OPTION] … SRC [user@]host::DEST //::可以是目标也可以是源 • rsync [OPTION] … [user@]host::SRC DEST

•rsync常用选项 • -a 包含-rtplgoD • -r 同步目录时要加上,类似cp时的-r选项 • -v 同步时显示一些信息,让我们知道同步的过程 • -l 保留软连接 • -L 加上该选项后,同步软链接时会把源文件给同步 • -p 保持文件的权限属性 • -o 保持文件的属主 //如果对方没有就显示数字 • -g 保持文件的属组 • -D 保持设备文件信息 • -t 保持文件的时间属性 --delete 删除DEST中SRC没有的文件 --exclude 过滤指定文件,如--exclude “logs” •-P 显示同步过程,比如速率,比-v更加详细 -u (update)加上该选项后,如果DEST中的文件比SRC新,则不同步。 •-z 传输时压缩

rsync常用选项(下) 测试: rsync -av /root/lsx/ /tmp/lsx_dest/ //不加L的情况。同步目录后面加/。加/表示把目录的内容同步过来。不加/就是把目录和内容一起同步过来。 ll /tmp/lsx_dest/ lrwxrwxrwx 1 root root 12 11月 30 23:53 123.txt -> /tmp/123.txt lrwxrwxrwx 1 root root 11 11月 30 23:44 12.txt -> /tmp/12.txt [root@localhost ~]# rsync -avL /root/lsx/ /tmp/lsx_dest/ //加L的就相当于没有小l。会把源文件内容拷贝过去 ll /tmp/lsx_dest/ -rw-r--r-- 1 root root 9 11月 30 23:52 123.txt

2.--delete ll /tmp/lsx_dest/ lrwxrwxrwx 1 root root 12 11月 30 23:53 123.txt -> /tmp/123.txt -rw-r--r-- 1 root root 6 11月 30 23:39 1.txt -rw-r--r-- 1 root root 0 12月 1 00:11 new.txt [root@localhost ~]# rsync -av --delete /root/lsx/ /tmp/lsx_dest/ //加--delete把目标文件中src没有的文件删除(如果是做镜像备份就需要加)

sending incremental file list deleting new.txt

[root@localhost ~]# ll /tmp/lsx_dest/ 总用量 8 lrwxrwxrwx 1 root root 12 11月 30 23:53 123.txt -> /tmp/123.txt -rw-r--r-- 1 root root 6 11月 30 23:39 1.txt 3.-u cat /tmp/lsx_dest/1.txt dadad jia123 cat /root/lsx/1.txt dadad rsync -av /root/lsx/ /tmp/lsx_dest/ //不加u的情况 sending incremental file list ./ 1.txt cat /tmp/lsx_dest/1.txt dadad

cat /tmp/lsx_dest/1.txt dadad 123 rsync -avu /root/lsx/ /tmp/lsx_dest/ //加u的情况 sending incremental file list ./ cat /tmp/lsx_dest/1.txt dadad 123

4.--exclude rsync -av --exclude ".txt" /root/lsx/ /tmp/lsx_dest/ //--exclude过滤txt文件。支持连续多个格式 --exclude ".txt" --exclude "." sending incremental file list ./ passwd

ls /tmp/lsx_dest/ passwd ls /root/lsx/ 123.txt 12.txt 1.txt passwd

5.-P rsync -avP --exclude "*.txt" /root/lsx/ /tmp/lsx_dest/ //-P可以看到传输过程几兆每秒(是否卡死) sending incremental file list ./ passwd 883 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)

sent 974 bytes received 34 bytes 2016.00 bytes/sec total size is 883 speedup is 0.88

通过ssh同步 A机器到B机器。 rsync通过ssh方式同步 rsync -av test1/ 192.168.133.132:/tmp/test2/ //把test1目录同步到另外一台机器 rsync /etc/passwd 192.168.211.149:/root/ //推文件。拉 The authenticity of host '192.168.211.149 (192.168.211.149)' can't be established. ECDSA key fingerprint is 0f:bd:8f:21:d3:48:a7:6c:f0:ae:00:b4:56:9e:c7:da. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.211.149' (ECDSA) to the list of known hosts. root@192.168.211.149's password: bash: rsync: 未找到命令 rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9] 原因是机器默认是没有rsync命令的。所以要在另外一台机器安装rsync

rsync -avz 192.168.211.149:/root/passwd /tmp/passwd //拉文件

!!如果对方端口不是22 rsync -avz -e "ssh -p 22" 192.168.211.149:/root/passwd /tmp/passwd root@192.168.211.149's password: receiving incremental file list

sent 11 bytes received 36 bytes 18.80 bytes/sec total size is 883 speedup is 18.79

ssh -p 22 192.168.211.149 //这种方式可以直接登录到对方机器