1. 程式人生 > >10.28-10.29 rsync工具介紹及常用選項,rsync通過ssh同步

10.28-10.29 rsync工具介紹及常用選項,rsync通過ssh同步

rsync ssh rsync常用 rsync工具

八周二次課(3月27日)

10.28 rsync工具介紹

10.29/10.30 rsync常用選項

10.31 rsync通過ssh同步



10.28 rsync工具介紹

技術分享圖片


Linux文件同步工具-rsync

此工具很重要,以後可能每天都需要用到。要熟悉熟練使用


備份方式

本地備份,把數據從A的C目錄傳輸到A的C目錄。類似於cp,但是跟cp不一樣。

遠程備份,把數據從A傳輸到B上。


本地備份

rsync -av /etc/passwd /tmp/1.txt


遠程備份

rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt


rsync格式


*SRC 源目錄,源文件

*DEST 目標目錄,目標文件


rsync [OPTION] … SRC DEST

本地與本地拷貝數據


rsync [OPTION] … SRC [user@]host:DEST

本地拷貝數據至遠程

[user@]host:DEST 格式可以對應[email protected]


rsync [OPTION] … [user@]host:SRC DEST

遠程目錄同步數據到本地。


rsync [OPTION] … SRC [user@]host::DEST

與其他驗證方式不同。


rsync [OPTION] … [user@]host::SRC DEST

與其他驗證方式不同。



10.29 rsync常用選項(上)


技術分享圖片


Linux文件同步工具-rsync


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格式



10.30 rsync常用選項(下)


技術分享圖片


實驗

本地備份

[root@centos7-01 ~]# mkdir 111

[root@centos7-01 ~]# cd 111/

[root@centos7-01 111]# touch 12.tx~ 12.txt 12_txt.swp 4913 aming3

[root@centos7-01 111]# mkdir 222

[root@centos7-01 111]# ls

12.tx~ 12.txt 12_txt.swp 222 4913 aming3

[root@centos7-01 111]# rsync -av /root/111/ /tmp/111_dest/

sending incremental file list

created directory /tmp/111_dest

./

12.txt

12.tx~

12_txt.swp

4913

aming3

222/


sent 311 bytes received 114 bytes 850.00 bytes/sec

total size is 0 speedup is 0.00


--delete 用法 同步效果要與源一樣 加粗

[root@centos7-01 111]# touch /tmp/111_dest/new.txt

[root@centos7-01 111]# rsync -av --delete /root/111/ /tmp/111_dest/

sending incremental file list

deleting new.txt


sent 110 bytes received 13 bytes 246.00 bytes/sec

total size is 0 speedup is 0.00


上述輸出結果可以看出new.txt被刪除掉。

下面 ls /tmp/111_dest/可以看到new.txt已經被刪掉

[root@centos7-01 111]# ls /tmp/111_dest/

12.tx~ 12.txt 12_txt.swp 222 4913 aming3


--exclude用法

先刪掉/111_dest/下的所有文件,重新同步過/root/111/

[root@centos7-01 111]# rm -rf /tmp/111_dest/*

[root@centos7-01 111]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/

sending incremental file list

./

12.tx~

12_txt.swp

4913

aming3

222/


sent 265 bytes received 95 bytes 720.00 bytes/sec

total size is 0 speedup is 0.00


可以看到txt都被過濾掉了,剩余的文件都被同步到了/root/111/裏


--exclude支持多個在同一條命令上,命令示意如下。

[root@centos7-01 111]# rsync -avL --exclude "*.txt" --exclude "aming*" /root/111/ /tmp/111_dest/

sending incremental file list


-P用法

先刪掉/tmp/111_dest/

[root@centos7-01 111]# !rm

rm -rf /tmp/111_dest/*

[root@centos7-01 111]# rsync -avP /root/111/ /tmp/111_dest/

sending incremental file list

./

12.txt

0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=5/7)

12.tx~

0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=4/7)

12_txt.swp

0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/7)

4913

0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=2/7)

aming3

0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=1/7)

222/


sent 311 bytes received 114 bytes 850.00 bytes/sec

total size is 0 speedup is 0.00


-u用法

需求:讓目標文件更加新,這時候需要用到-u

#cd /tmp/111_dest/

#vi 4913 添加文字內容

1112321321oijiojoiffdl

sdlkasjklwjqewq

1111

333hnjkhk

jk


添加-u選項,

[root@centos7-01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/

sending incremental file list

./


sent 113 bytes received 16 bytes 258.00 bytes/sec

total size is 0 speedup is 0.00

[root@centos7-01 111_dest]# cat 4913

1112321321oijiojoiffdl

sdlkasjklwjqewq

1111

333hnjkhk

jk

cat完之後,發現目標文件4913編輯的內容沒被覆蓋掉,



10.31 rsync通過ssh同步


Linux文件同步工具-rsync


技術分享圖片


打開兩臺虛擬機操作

兩臺虛擬機能互相通信,分別是1和2號


把1的/etc/passwd 同步到2上,/tmp/aming.txt

[root@centos7-01 111_dest]# rsync /etc/passwd 192.168.189.129:/tmp/aming.txt

[email protected]'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]

[root@centos7-01 111_dest]# rsync /etc/passwd 192.168.189.129:/tmp/aming.txt


出現以上報錯消息,此報錯意思是因為2上面沒有安裝rsync命令,此時在2號安裝即可。

[root@centos7-01 111_dest]# rsync -av /etc/passwd 192.168.189.129:/tmp/aming.txt

[email protected]'s password:

sending incremental file list

passwd


sent 1176 bytes received 31 bytes 268.22 bytes/sec

total size is 1102 speedup is 0.91

上面屬於推文件動作


反向操作,把2號的數據,同步到1上

[root@centos7-01 111_dest]# rsync -avP 192.168.189.129:/tmp/aming.txt /tmp/123.txt

[email protected]'s password:

receiving incremental file list

aming.txt

1102 100% 1.05MB/s 0:00:00 (xfer#1, to-check=0/1)


sent 30 bytes received 1184 bytes 161.87 bytes/sec

total size is 1102 speedup is 0.91

這屬於拉文件動作。


指定端口

rsync -av -e "ssh -p 22" test1/ 192.168.189.129:/tmp/aming.txt


10.28-10.29 rsync工具介紹及常用選項,rsync通過ssh同步