码迷,mamicode.com
首页 > 其他好文 > 详细

rsync同步

时间:2018-04-17 20:52:30      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:rsync   同步   

rsync常用选项

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

[root@lynn-04 ~]# yum install -y rsync

本机文件同步

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

sent 1759 bytes  received 31 bytes  3580.00 bytes/sec
total size is 1685  speedup is 0.94

从本机同步到远程 报错远程机器上没有安装rsync

[root@lynn-04 ~]# rsync -av /etc/passwd root@192.168.130.128:/tmp/1.txt
The authenticity of host ‘192.168.130.128 (192.168.130.128)‘ can‘t be established.
ECDSA key fingerprint is SHA256:3WEYnU1l7WjaDAQHkhPIcM9Mvh7wYYFkKjG0+C0BriY.
ECDSA key fingerprint is MD5:22:a0:fb:e7:8c:44:98:c4:df:5b:64:4d:58:db:94:75.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes‘ or ‘no‘: yes
Warning: Permanently added ‘192.168.130.128‘ (ECDSA) to the list of known hosts.
root@192.168.130.128‘s password: 
bash: rsync: command not found
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

[root@lynn-06 ~]# yum install -y rsync

再次同步

[root@lynn-04 ~]# rsync -av /etc/passwd root@192.168.130.128:/tmp/1.txt
root@192.168.130.128‘s password: 
sending incremental file list
passwd

sent 1759 bytes  received 31 bytes  275.38 bytes/sec
total size is 1685  speedup is 0.94
[root@lynn-04 111]# rsync -av /root/111/ /tmp/111_test/
sending incremental file list
created directory /tmp/111_test
./
12.txt
12_txt.swp
332 -> /tmp/333-ln.txt
333
333.txt
4913
222/

sent 365 bytes  received 117 bytes  964.00 bytes/sec
total size is 15  speedup is 0.03
[root@lynn-04 111]# ls -l /tmp/111_test/
total 0
-rw-r--r-- 1 root root  0 Apr 17 19:17 12.txt
-rw-r--r-- 1 root root  0 Apr 17 19:17 12_txt.swp
drwxr-xr-x 2 root root  6 Apr 17 19:18 222
lrwxrwxrwx 1 root root 15 Apr 17 19:31 332 -> /tmp/333-ln.txt
-rw-r--r-- 1 root root  0 Apr 17 19:23 333
-rw-r--r-- 1 root root  0 Apr 17 19:27 333.txt
-rw-r--r-- 1 root root  0 Apr 17 19:18 4913

-L 加上该选项后,同步软链接时会把源文件给同步

[root@lynn-04 111]# rsync -avL /root/111/ /tmp/111_test/
sending incremental file list
332

sent 180 bytes  received 32 bytes  424.00 bytes/sec
total size is 0  speedup is 0.00
[root@lynn-04 111]# ls -l /tmp/111_test/
total 0
-rw-r--r-- 1 root root 0 Apr 17 19:17 12.txt
-rw-r--r-- 1 root root 0 Apr 17 19:17 12_txt.swp
drwxr-xr-x 2 root root 6 Apr 17 19:18 222
-rw-r--r-- 1 root root 0 Apr 17 19:28 332
-rw-r--r-- 1 root root 0 Apr 17 19:23 333
-rw-r--r-- 1 root root 0 Apr 17 19:27 333.txt
-rw-r--r-- 1 root root 0 Apr 17 19:18 4913

--delete 删除DEST中SRC没有的文件

[root@lynn-04 ~]# ls 111
12.txt  12_txt.swp  222  332  333  333.txt  4913
[root@lynn-04 ~]# ls /tmp/111_test
12.txt  12_txt.swp  222  332  333  333.txt  4913
[root@lynn-04 ~]# touch /tmp/111_test/hahaha.txt
[root@lynn-04 ~]# ls /tmp/111_test
12.txt  12_txt.swp  222  332  333  333.txt  4913  hahaha.txt
[root@lynn-04 ~]# rsync -avL --delete /root/111/ /tmp/111_test/
sending incremental file list
./
deleting hahaha.txt

sent 144 bytes  received 16 bytes  320.00 bytes/sec
total size is 0  speedup is 0.00
[root@lynn-04 ~]# ls /tmp/111_test
12.txt  12_txt.swp  222  332  333  333.txt  4913

--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步

[root@lynn-04 ~]# ls 111/
12.txt  12_txt.swp  222  332  333  333.txt  4913
[root@lynn-04 ~]# ls /tmp/111_test/
[root@lynn-04 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_test/
sending incremental file list
./
12_txt.swp
332
333
4913
222/

sent 275 bytes  received 95 bytes  740.00 bytes/sec
total size is 0  speedup is 0.00
[root@lynn-04 ~]# ls /tmp/111_test/
12_txt.swp  222  332  333  4913

-P 显示同步过程,比如速率,比-v更加详细

[root@lynn-04 ~]# rsync -aP /root/111/ /tmp/111_test/
sending incremental file list
./
12.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
12_txt.swp
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
332 -> /tmp/333-ln.txt
333
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=3/8)
333.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/8)
4913
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=1/8)
222/

sent 365 bytes  received 117 bytes  964.00 bytes/sec
total size is 15  speedup is 0.03

指定端口链接同步

[root@lynn-04 ~]# rsync -aP -e "ssh -p 22" 192.168.130.128:/tmp/666.txt /tmp/111.txt
root@192.168.130.128‘s password: 
receiving incremental file list
666.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 76 bytes  23.56 bytes/sec
total size is 0  speedup is 0.00

rsync同步

标签:rsync   同步   

原文地址:http://blog.51cto.com/10963213/2104571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!