您的位置:首页 > 其它

10.28 rsync工具介绍 10.29/10.30 rsync常用选项 10.31 rsync通

2018-07-18 21:23 603 查看
rsync工具介绍
安装命令:yum install -y rsync

[root@aminglinux-123 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 2,372 bytes  received 35 bytes  4,814.00 bytes/sec
total size is 2,280  speedup is 0.95
[root@aminglinux-123 ~]# rsync -av /etc/passwd 192.168.193.128:/tmp/1.txt
The authenticity of host '192.168.193.128 (192.168.193.128)' can't be established.
ECDSA key fingerprint is SHA256:/pKbINKTISanvNQ+5fJAqgFOnBJ7wbI68LKeFPZcVA0.
ECDSA key fingerprint is MD5:06:1f:8f:91:36:47:28:0c:72:08:6b:9f:28:b0:49:19.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.193.128' (ECDSA) to the list of known hosts.
root@192.168.193.128's password:
sending incremental file list

sent 45 bytes  received 12 bytes  2.65 bytes/sec
total size is 2,280  speedup is 40.00
上例中会把/etc/passwd同步到/tmp/目录下,并改名为1.txt。远程复制命令:rsync -av /etc/passwd 192.168.193.128:/root/
rsync的命令格式
*SRC源目录,源文件
*DEST目标目录,目标文件
rsync [OPTION] … SRC DEST本地与本地拷贝数据
rsync [OPTION] … SRC [user@]host:DEST本地拷贝数据至远程[user@]host:DEST 格式可以对应root@192.168.189.128
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则能避免此情况发生)
-L 加上该选项后,同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息
-t 保持文件的时间属性(mtime,ctime,atime)
--delete 删除DEST中SRC没有的文件例如B上没有A所没有的文件,A目录有123,B有4,如果此时加了此选项,A->B,会把B的4删掉。使其子文件同步。如果想一模一样,就要加--delete,如果不想一模一样,可以不加。

--exclude=PATTERN 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步。(支持通配)
-P 显示同步过程,比如速率,比-v更加详细
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步。大意就是,把DST中比SRC还新的文件排除掉,不会覆盖。update简写。
-z 传输时压缩zip格式
[root@aminglinux-123 rsync]# mkdir test2
[root@aminglinux-123 rsync]# ls test2
[root@aminglinux-123 rsync]# rsync -a test1 test2
[root@aminglinux-123 rsync]# ls test2
test1
[root@aminglinux-123 rsync]# ls test2/test1/
1  123.txt  2  3
避免把test1放到test2当中命令是:

[root@aminglinux-123 rsync]# ls test2/test1/
1  123.txt  2  3
[root@aminglinux-123 rsync]# ^C
[root@aminglinux-123 rsync]# rm -rf test2
[root@aminglinux-123 rsync]# rsync -a test1/ test2/
[root@aminglinux-123 rsync]# ls -l test2
总用量 0
-rw-r--r-- 1 root root  0 7月  18 16:15 1
lrwxrwxrwx 1 root root 13 7月  18 16:15 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 7月  18 16:15 2
-rw-r--r-- 1 root root  0 7月  18 16:15 3
rsync -a test1/ test2/ 备份目录时,要加斜杠。

rsync -l:可以把SRC中软连接的目标文件复制到DST。
[root@aminglinux-123 rsync]# rm -rf test2
[root@aminglinux-123 rsync]# rsync -avl test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt -> /root/123.txt
2
3

sent 248 bytes  received 107 bytes  710.00 bytes/sec
total size is 13  speedup is 0.04
[root@aminglinux-123 rsync]# ls -l test2/
总用量 0
-rw-r--r-- 1 root root  0 7月  18 16:15 1
lrwxrwxrwx 1 root root 13 7月  18 16:15 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 7月  18 16:15 2
-rw-r--r-- 1 root root  0 7月  18 16:15 3
使用-u选项

不加-u选项,会把test2/1的创建时间变成test1/1一样。下面的例子创建时间是一样。

[root@aminglinux-123 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 7月  18 16:15 test1/1
-rw-r--r-- 1 root root 0 7月  18 16:15 test2/1
不加-u如下所示:

[root@aminglinux-123 rsync]# echo "1111" >test2/1
[root@aminglinux-123 rsync]# ll test2/1
-rw-r--r-- 1 root root 5 7月  18 20:50 test2/1
[root@aminglinux-123 rsync]# rsync -a test1/1 test2/
[root@aminglinux-123 rsync]# ll test2/1
-rw-r--r-- 1 root root 0 7月  18 16:15 test2/1
这里test2/1的创建时间还是和test1一样。下面是加-u选项,如下所示:
[root@aminglinux-123 rsync]# echo "1111" >test2/1
[root@aminglinux-123 rsync]# ll test2/1
-rw-r--r-- 1 root root 5 7月  18 20:54 test2/1
[root@aminglinux-123 rsync]# rsync -avu test1/1 test2/
sending incremental file list

sent 39 bytes  received 12 bytes  102.00 bytes/sec
total size is 0  speedup is 0.00
[root@aminglinux-123 rsync]# ll test2/1 test1/1
-rw-r--r-- 1 root root 0 7月  18 16:15 test1/1
-rw-r--r-- 1 root root 5 7月  18 20:54 test2/1
加上-u选项后,不会再把test1/1同步为test2/1了。
使用--delete选项
[root@aminglinux-123 rsync]# rm -f test1/123.txt
[root@aminglinux-123 rsync]# ls test1/
1  2  3
[root@aminglinux-123 rsync]# rsync -av test1/ test2/
sending incremental file list
./
1

sent 127 bytes  received 38 bytes  330.00 bytes/sec
total size is 0  speedup is 0.00
上例中,test2/目录并没有删除123.txt。下面加上--delete选项,示例命令如下:

[root@aminglinux-123 rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt

sent 81 bytes  received 23 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00
[root@aminglinux-123 rsync]# ls test2/
1  2  3
这里test2/目录下的123.txt也被删除了。

如果DST中增加文件了,而SRC当中没有这些文件,同步时加上--delete选项后同样会删除新增的文件。
[root@aminglinux-123 rsync]# touch test2/4
[root@aminglinux-123 rsync]# ls test1/
1  2  3
[root@aminglinux-123 rsync]# ls test2/
1  2  3  4
[root@aminglinux-123 rsync]# rsync -a --delete test1/ test2/
[root@aminglinux-123 rsync]# ls test1/
1  2  3
[root@aminglinux-123 rsync]# ls test2/
1  2  3
使用--exclude选项

[root@aminglinux-123 rsync]# touch test1/4
[root@aminglinux-123 rsync]# rsync -a --exclude="4" test1/ test2/
[root@aminglinux-123 rsync]# ls test1/
1  2  3  4
[root@aminglinux-123 rsync]# ls test2/
1  2  3
该选项还可以与匹配字符*一起使用。

[root@aminglinux-123 rsync]# ls test1/
1  1.txt  2  2.txt  3  4
[root@aminglinux-123 rsync]# rsync -a --progress  --exclude="*.txt" test1/ test2/
sending incremental file list
./
4
0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/5)
--progress选项,主要用来观察rsync同步过程状态的。

rsync应用实例
通过ssh的方式
[root@aminglinux-123 rsync]# rsync -avl test1/ 192.168.193.128:/tmp/test2/
root@192.168.193.128's password:
sending incremental file list
created directory /tmp/test2
./
1
1.txt
2
2.txt
3
4

sent 374 bytes  received 166 bytes  98.18 bytes/sec
total size is 0  speedup is 0.00
[root@aminglinux-123 rsync]# rsync -avl test1/ 192.168.193.128:/tmp/test2/ ./test3/
Unexpected remote arg: 192.168.193.128:/tmp/test2/
rsync error: syntax or usage error (code 1) at main.c(1343) [sender=3.1.2]
通过密钥登录远程主机

[root@aminglinux-129 ~]# rsync -avl test1/ 192.168.193.129:/tmp/test4/
root@192.168.193.129's password:
sending incremental file list
rsync: change_dir "/root//test1" failed: No such file or directory (2)
created directory /tmp/test4

sent 20 bytes  received 45 bytes  11.82 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rsync 应用 实例