1. 程式人生 > >rsync同步

rsync同步

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 [email protected]:/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.
[email protected]‘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 [email protected]:/tmp/1.txt
[email protected]‘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
[email protected]‘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同步