rsync工具同步方法

rsync介紹

rsync是一個網絡間的實時同步目錄或文件的工具,能夠實現數據增量同步,可以起到熱備和比全量備份數據節省時間及帶寬的優點,通常的備份文件或目錄都是以拷貝形式全量備份,如果文件過於龐大,則需要很多時間,假如是通過網絡間來拷貝一個全量備份,那則又會消耗較大的網絡資源,rsync可以起到增量備份,可以只同步備份新增加的部分文件或目錄,可以極大的減少時間和網絡資源的使用
Rsync的特性如下:

1)支持拷貝特殊文件如鏈接,設備等

2)可以有排除指定文件或目錄同步的功能,相當於打包命令tar

3)可以保持原來文件或目錄的權限,時間,軟硬鏈接等所有屬性均不改變。

4)可實現增量同步,即只同步發生變化的數據,因此數據傳輸效率更高

5)可以使用rcp,rsh,ssh等方式來配合傳輸文件,也可以通過直接的socker鏈接

6)支持匿名的或認證的進程模式傳輸,方便進行數據備份及鏡像

rsync 同步格式(來源目錄:需要拷貝的原文件或目錄,目標目錄:指拷貝到的目標目錄上)
rsync [option選項] 來源目錄 目的目錄
rsync [option選項] 來源目錄 用戶@遠程主機:目的目錄
rsync [option選項] 用戶@遠程主機來源目錄 目標目錄
rsync [option選項] 來源目錄 user@遠程主機::目標目錄
rsync [option選項] user@遠程主機::來源目錄 目標目錄

使用yum安裝rsync同步工具:yum install -y rsync

rsync常用選項
-a  包含選項 -rtokgoD
-r  同步目錄時需要添加的選項,類似cp時的-r選項
-v  同步時輸出的信息,顯示同步的過程
-l   保留軟連接,不會同步軟連接指向的源文件,這個-l選項同步會導致軟連接失效
-L    保留軟連接同時同步軟連接的源文件
-p   同步時保持文件原權限屬性,不改變文件的原權限
-o   同步文件或目錄時保持原先的屬主,不改變文件的原屬主
-g   同步時保持文件或目錄的屬組,不改變文件的原屬組
-D  保持設備文件信息
-t    同步時保持文件時間的屬性,不改變文件的原時間屬性
--delete   同步過程中對比目標是否有源沒有的文件,如果目標擁有源沒有的文件,則將目標內比源多的文件內容刪除處理,如同步web程序目錄時排除掉產生的log日誌目錄不進行同步
--exclude   過濾掉某些文件不做同步,如--exclude "1.log" 會把文件名包含1.log的文件或目錄過濾掉不做同步
-P   顯示同步過程,比如傳輸速率,比-v選項更加詳細
-u   該選項表示爲:如果目標中存在的文件比源中的文件新時,則不同步該文件,同步時以最新的文件爲主,源不會去覆蓋目標內的文件內容
-z  傳輸時壓縮,以zip格式壓縮,壓縮時節省帶寬,但是相對的比較消耗CPU

rsync常用選項示例
使用rsync同步一個目錄

----------------------------------進行目錄同步
[root@localhost src]# rsync -av /usr/local/src/tmp/ /tmp/tmp-1
sending incremental file list
created directory /tmp/tmp-1
./
ipt.sh
passwd
siyan.ipt
helloworld/

sent 2,487 bytes received 231 bytes 5,436.00 bytes/sec
total size is 1,821 speedup is 0.67

--------------------------------------查看同步結果
[root@localhost src]# ll -h /tmp/tmp-1/
總用量 36K
-rw-r--r-- 1 root root 358 7月 13 01:31 ipt.sh
-rw-r--r-- 1 root root 487 7月 1 18:35 passwd
-rw-r--r-- 1 root root 472 7月 16 14:09 siyan.ipt

同步一個軟連接和軟連接源文件的目錄到目標目錄

[root@localhost tmp]# rsync -avL /usr/local/src/tmp/ /tmp/tmp-1/
sending incremental file list
symlink has no referent: "/usr/local/src/tmp/ipt"
./
ipta.sh

sent 677 bytes received 39 bytes 1,432.00 bytes/sec
total size is 1,821 speedup is 2.54
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

這個錯誤是因爲當前目錄中包含失效的軟連接文件,這時我們需要查找出失效的軟連接文件進行修復或刪除處理

[root@localhost tmp]# ls -l
總用量 36
lrwxrwxrwx 1 root root 6 7月 16 22:28 ipt -> ipt.sh                   ← 失效軟連接會反覆閃爍
-rw-r--r-- 1 root root 358 7月 13 01:31 ipta.sh
-rw-r--r-- 1 root root 487 7月 1 18:35 passwd
-rw-r--r-- 1 root root 472 7月 16 14:09 siyan.ipt

使用-L同步一個包含軟連接的文件,創建一個軟連接文件然後進行同步
命令 : rsync -avL /usr/local/src/tmp/ /tmp/tmp-1/ 同步後軟連接會被同步成一個普通的文件

--------------------------------恢復軟連接文件
[root@localhost tmp]# mv ipta.sh ipt.sh          
[root@localhost tmp]# ls -l
總用量 36
lrwxrwxrwx 1 root root   6 7月  16 22:28 ipt -> ipt.sh
-rw-r--r-- 1 root root 358 7月  13 01:31 ipt.sh
-rw-r--r-- 1 root root 487 7月   1 18:35 passwd
-rw-r--r-- 1 root root 472 7月  16 14:09 siyan.ipt

--------------------------------同步包含軟連接的目錄
[root@localhost tmp]# rsync -avL /usr/local/src/tmp/ /tmp/tmp-1/     
sending incremental file list
./
ipt

sent 686 bytes  received 39 bytes  1,450.00 bytes/sec
total size is 2,179  speedup is 3.01

------------------------------------查看同步後的情況
[root@localhost tmp]# ls -l /tmp/tmp-1/
ipt
ipt.sh
passwd
siyan.ipt
helloworlrd

------------------------------------軟連接被變爲普通文件同步過來了
[root@localhost tmp]# ls -l /tmp/tmp-1/
總用量 40
-rw-r--r-- 1 root root 358 7月  13 01:31 ipt          
-rw-r--r-- 1 root root 358 7月  13 01:31 ipt.sh
-rw-r--r-- 1 root root 487 7月   1 18:35 passwd
-rw-r--r-- 1 root root 472 7月  16 14:09 siyan.ipt

--delete 選項 同步覆蓋掉源內不存在的文件,如目標中存在1234.log,源裏不存在1234.log,那麼同步時會刪除掉目標內的1234.log文件

----------------------------------目標目錄中存在新文件123.log
[root@localhost tmp]# ls -l /tmp/tmp-1/
總用量 40
-rw-r--r-- 1 root root 20 7月 5 10:43 1
-rw-r--r-- 1 root root 0 7月 16 23:11 123.log
-rw-r--r-- 1 root root 100 7月 5 10:19 1.txt
-rw-r--r-- 1 root root 25 7月 5 10:44 2
-rw-r--r-- 1 root root 93 7月 5 15:29 a
-rw-r--r-- 1 root root 242 7月 5 10:19 awkshell.sh
drwxr-xr-x 2 root root 6 7月 1 20:10 helloworld
-rw-r--r-- 1 root root 24 7月 5 10:20 id.txt
-rw-r--r-- 1 root root 358 7月 13 01:31 ipt
-rw-r--r-- 1 root root 358 7月 13 01:31 ipt.sh
-rw-r--r-- 1 root root 487 7月 1 18:35 passwd
-rw-r--r-- 1 root root 472 7月 16 14:09 siyan.ipt

----------------------------------同步會刪除目標內比源多的文件或內容
[root@localhost tmp]# rsync -avL --delete /usr/local/src/tmp/ /tmp/tmp-1/
sending incremental file list
deleting 123.log
./

sent 285 bytes  received 31 bytes  632.00 bytes/sec
total size is 2,179  speedup is 6.90

------------------------------------查看同步後的結果,目標比源多的文件(123.log)被刪除
[root@localhost tmp]# ls -l /tmp/tmp-1/
總用量 40
-rw-r--r-- 1 root root  20 7月   5 10:43 1
-rw-r--r-- 1 root root 100 7月   5 10:19 1.txt
-rw-r--r-- 1 root root  25 7月   5 10:44 2
-rw-r--r-- 1 root root  93 7月   5 15:29 a
-rw-r--r-- 1 root root 242 7月   5 10:19 awkshell.sh
drwxr-xr-x 2 root root   6 7月   1 20:10 helloworld
-rw-r--r-- 1 root root  24 7月   5 10:20 id.txt
-rw-r--r-- 1 root root 358 7月  13 01:31 ipt
-rw-r--r-- 1 root root 358 7月  13 01:31 ipt.sh
-rw-r--r-- 1 root root 487 7月   1 18:35 passwd
-rw-r--r-- 1 root root 472 7月  16 14:09 siyan.ipt

rsync --exclude 排除某些文件或目錄不進行同步,需求如不同步日誌文件

[root@localhost tmp]# rsync -avL --exclude "awkshell.sh" --exclude "id.txt" /usr/local/src/tmp/ /tmp/tmp-1/
sending incremental file list
./
1
1.txt
2
a
ipt
ipt.sh
passwd
siyan.ipt
helloworld/

sent 2,498 bytes received 175 bytes 5,346.00 bytes/sec
total size is 1,913 speedup is 0.72

------------------------------------------對比同步後的文件
[root@localhost tmp]# tree /usr/local/src/tmp/ /tmp/tmp-1/
/usr/local/src/tmp/
├── 1
├── 1.txt
├── 2
├── a
├── awkshell.sh
├── helloworld
├── id.txt
├── ipt -> ipt.sh
├── ipt.sh
├── passwd
└── siyan.ipt
/tmp/tmp-1/
├── 1
├── 1.txt
├── 2
├── a
├── helloworld
├── ipt
├── ipt.sh
├── passwd
└── siyan.ipt

rsync -P 選項 顯示傳輸時的詳細信息,傳輸速率,傳輸時間等信息(這次同步了--exclude排除的文件)

[root@localhost tmp]# rsync -avLP /usr/local/src/tmp/ /tmp/tmp-1/
sending incremental file list
awkshell.sh
            242 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/12)
id.txt
             24 100% 23.44kB/s 0:00:00 (xfr#2, to-chk=5/12)

sent 634 bytes received 55 bytes 1,378.00 bytes/sec
total size is 2,179 speedup is 3.16

rsync -u 選項會比較同步時所有文件的時間屬性信息,同樣的兩個文件,如果目標的時間屬性更新,則不會在同步時把源文件覆蓋目標文件,針對目標上的這個比拷貝源還新的文件不做操作

[root@localhost tmp]# echo "gfdhgfjsdfgdfhgfgsxdfg" > 6666.txt     源文件內容
[root@localhost tmp]# echo "1235545" > /tmp/tmp-1/6666.txt          目標文件內容
[root@localhost tmp]# rsync -avPu /usr/local/src/tmp/ /tmp/tmp-1/   -u指定新文件不同步的操作
sending incremental file list

sent 319 bytes received 13 bytes 664.00 bytes/sec
total size is 1,850 speedup is 5.57
[root@localhost tmp]# cat /usr/local/src/tmp/6666.txt            查看源文件內容
gfdhgfjsdfgdfhgfgsxdfg 
[root@localhost tmp]# cat /tmp/tmp-1/6666.txt             查看目標文件內容跟同步之前沒有改變
1235545

rsync主機間的同步方式

rsync推送同步,把文件或目錄推送到其他主機上:rsync -avP 需要同步的目錄 目標主機ip:/目標目錄路徑,如:
rsync -avP /usr/local/src/ 192.168.1.223:/usr/local/src/tmp

[root@localhost src]# rsync -avP /usr/local/src/ 192.168.1.223:/usr/local/src/tmp
The authenticity of host '192.168.1.223 (192.168.1.223)' can't be established.
ECDSA key fingerprint is b5:e7:41:c5:d5:90:ab:36:27:41:40:06:72:1d:9c:f6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.223' (ECDSA) to the list of known hosts.
[email protected]'s password: 
sending incremental file list
./
Python-3.7.0.tgz
     22,745,726 100% 63.90MB/s 0:00:00 (xfr#1, to-chk=0/2)

sent 22,751,393 bytes received 38 bytes 2,676,638.94 bytes/sec
total size is 22,745,726 speedup is 1.00

推送方式的同步結果
rsync工具同步方法


rsync拉取同步的方式,對遠程主機的整個目錄進行主動拷貝: rsync 遠程IP:/拷貝的目錄路徑 /存放在本機的路徑/,如下命令:rsync -avr 192.168.1.223:/usr/local/src/tmp /usr/local/src

[root@localhost src]# rsync -avr 192.168.1.223:/usr/local/src/tmp /usr/local/src
[email protected]'s password: 
receiving incremental file list
tmp/
tmp/1
tmp/1.txt
tmp/2
tmp/6666.txt
tmp/Python-3.7.0.tgz
tmp/a
tmp/awkshell.sh
tmp/id.txt
tmp/ipt -> ipt.sh
tmp/ipt.sh
tmp/passwd
tmp/siyan.ipt
tmp/helloworld/

sent 248 bytes  received 22,753,964 bytes  4,137,129.45 bytes/sec
total size is 22,747,576  speedup is 1.00
-----------------------------將遠程主機上的目錄整個拷貝過來
[root@localhost src]# ls
tmp
[root@localhost src]# cd tmp/
[root@localhost tmp]# ls
1      2         a            helloworld  ipt     passwd            siyan.ipt
1.txt  6666.txt  awkshell.sh  id.txt      ipt.sh  Python-3.7.0.tgz

主動同步的執行結果,結合上條同步實踐可以看到把遠程主機的tmp整個目錄拷貝過來了,兩臺機器上的目錄下的文件數量和名稱也是一致的

rsync工具同步方法


rsync通過非默認的ssh端口同步

通過非22端口同步,指定自定義ssh端口進行同步,rsync -avr -e "ssh -p 指定自定義端口" /本機存放的目錄/ 遠程主機IP:/遠程目錄/ ,命令如:rsync -avr -e "ssh -p 22" /tmp/ 192.168.1.223:/tmp/tmp-1


[root@localhost tmp]# rsync -avr -e "ssh -p 22" /usr/local/src/ 192.168.1.223:/tmp/tmp-1
[email protected]'s password: 
sending incremental file list
./
tmp/
tmp/1
tmp/1.txt
tmp/2
tmp/6666.txt
tmp/Python-3.7.0.tgz
tmp/a
tmp/awkshell.sh
tmp/id.txt
tmp/ipt -> ipt.sh
tmp/ipt.sh
tmp/passwd
tmp/siyan.ipt
tmp/helloworld/

sent 22,753,983 bytes received 243 bytes 5,056,494.67 bytes/sec
total size is 22,747,576 speedup is 1.00
[root@localhost tmp]# ls
1 2 a helloworld ipt passwd siyan.ipt
1.txt 6666.txt awkshell.sh id.txt ipt.sh Python-3.7.0.tgz

同步後將主動同步時的tmp目錄進行了刪除,從這一點也可以看到進行了同步

此外rsync可以通過系統服務的方式來定時同步主機之間的文件,關於如何配置rsync服務同步的方式請參閱其他資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章