2018-1-30 8周2次課 rsync
rsync傳輸數據,備份到遠程,類似於cp
rsync不僅可以實現A機器到B機器,也可以實現從本機A目錄到B目錄的數據傳輸
cp的話,如果是不斷寫入的文件,cp過去,覆蓋文件,文件過大的話,既浪費時間,又占磁盤IO,此時使用rsync
rsync可以實現增量拷貝,只增加變更過的文件
·拷貝文件:
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt sending incremental file list passwd sent 1053 bytes received 31 bytes 2168.00 bytes/sec total size is 979 speedup is 0.90
·遠程去同步、拷貝:rsync -av 目錄 用戶名@ip:目錄
[root@localhost ~]# rsync -av /etc/passwd [email protected]:/tmp/1.txt
rsync格式:
rsync [OPTION] … SRC DEST
rsync [OPTION] … SRC [user@]host:DEST
rsync [OPTION] … [user@]host:SRC DEST
rsync [OPTION] … SRC [user@]host::DEST
rsync [OPTION] … [user@]host::SRC DEST
(兩個 :即是目標也是源)
10.29 rsync常用選項(上)
rsync常用選項:
-a 包含-rtplgoD
-r 同步目錄時要加上,類似cp時的-r選項
-v 同步時顯示一些信息,讓我們知道同步的過程
-l 保留軟連接
-L 加上該選項後,同步軟鏈接時會把源文件給同步
-p 保持文件的權限屬性
-o 保持文件的屬主
-g 保持文件的屬組
-D 保持設備文件信息
-t 保持文件的時間屬性
--delete 刪除DEST中SRC沒有的文件(使DEST和SRC目錄完全一樣,為了安全可以不加)
--exclude 過濾指定文件,如--exclude “logs”會把文件名包含logs的文件或目錄過濾,不同步
-P 顯示同步過程,比如速率,比-v更加詳細
-u 加上該選項後,如果DEST中的文件比SRC新,則不同步
-z 傳輸時壓縮
10.30 rsync常用選項(下)
·rsync常用:rsync -av = rsync -rtplgoDv
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ 1.txt a.txt b.txt yum.log -> /tmp/yum.log awk/ awk/1.txt awk/10 awk/test.txt sent 1907 bytes received 136 bytes 4086.00 bytes/sec total size is 1484 speedup is 0.73
·同步軟鏈接時會把源文件給同步:rsync -L
[root@localhost ~]# ll 111/ 總用量 8 -rw-r--r--. 1 root root 0 1月 25 21:56 1.txt -rw-r--r--. 1 root root 426 1月 25 21:49 a.txt drwxr-xr-x. 2 root root 45 1月 25 21:50 awk -rw-r--r--. 1 root root 136 1月 25 21:49 b.txt lrwxrwxrwx. 1 root root 12 1月 25 22:00 yum.log -> /tmp/yum.log [root@localhost ~]# ll /tmp/111_dest/ 總用量 8 -rw-r--r--. 1 root root 0 1月 25 21:56 1.txt -rw-r--r--. 1 root root 426 1月 25 21:49 a.txt drwxr-xr-x. 2 root root 45 1月 25 21:50 awk -rw-r--r--. 1 root root 136 1月 25 21:49 b.txt lrwxrwxrwx. 1 root root 12 1月 25 22:00 yum.log -> /tmp/yum.log [root@localhost ~]# rsync -avL /root/aminglinux/ /tmp/111_dest/ ##加L選項會把 l 選項覆蓋 sending incremental file list yum.log sent 190 bytes received 32 bytes 444.00 bytes/sec total size is 1472 speedup is 6.63 [root@localhost ~]# ll /tmp/111_dest/ 總用量 8 -rw-r--r--. 1 root root 0 1月 25 21:56 1.txt -rw-r--r--. 1 root root 426 1月 25 21:49 a.txt drwxr-xr-x. 2 root root 45 1月 25 21:50 awk -rw-r--r--. 1 root root 136 1月 25 21:49 b.txt -rw-r--r--. 1 root root 0 1月 25 22:01 yum.log ##軟鏈接被替換成鏈接文件
給源文件增加一些內容,那麽rsync後會把內容同步
[root@localhost ~]# echo "128yyuhaoiuhfliaud" > /tmp/yum.log [root@localhost ~]# rsync -avL 111/ /tmp/111_dest/ sending incremental file list yum.log sent 213 bytes received 32 bytes 490.00 bytes/sec total size is 1491 speedup is 6.09 [root@localhost ~]# ll /tmp/111_dest/ 總用量 12 -rw-r--r--. 1 root root 0 1月 25 21:56 1.txt -rw-r--r--. 1 root root 426 1月 25 21:49 a.txt drwxr-xr-x. 2 root root 45 1月 25 21:50 awk -rw-r--r--. 1 root root 136 1月 25 21:49 b.txt -rw-r--r--. 1 root root 19 1月 25 22:12 yum.log [root@localhost ~]# cat !$ cat /tmp/111_dest/ cat: /tmp/111_dest/: 是一個目錄 [root@localhost ~]# cat /tmp/111_dest/yum.log 128yyuhaoiuhfliaud
·刪除DEST中SRC沒有的文件:--delete
[root@localhost ~]# ls /tmp/111_dest/ 1.txt a.txt awk b.txt yum.log [root@localhost ~]# touch /tmp/111_dest/new.txt [root@localhost ~]# rsync -avL --delete 111/ /tmp/111_dest/ sending incremental file list ./ deleting new.txt sent 146 bytes received 16 bytes 324.00 bytes/sec total size is 1491 speedup is 9.20 [root@localhost ~]# ls /tmp/111_dest/ 1.txt a.txt awk b.txt yum.log
·過濾指定文件:rsync --exclude
[root@localhost ~]# ls /tmp/111_dest/ 1.txt a.txt awk b.txt yum.log [root@localhost ~]# rm -rf /tmp/1 111_dest/ 1.txt [root@localhost ~]# rm -rf /tmp/111_dest/* [root@localhost ~]# ls /tmp/111_dest/ [root@localhost ~]# rsync -avL --exclude "*.txt" 111/ /tmp/111_dest/ sending incremental file list ./ yum.log awk/ awk/10 sent 240 bytes received 57 bytes 594.00 bytes/sec total size is 67 speedup is 0.23 [root@localhost ~]# ls /tmp/111_dest/ awk yum.log
(支持多個--exclude來過濾多種文件)
·顯示同步過程(比如速率,比-v更加詳細):rsync -P
[root@localhost ~]# rsync -avP 111/ /tmp/111_dest/ sending incremental file list ./ 1.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=7/9) a.txt 426 100% 0.00kB/s 0:00:00 (xfer#2, to-check=6/9) b.txt 136 100% 132.81kB/s 0:00:00 (xfer#3, to-check=5/9) yum.log -> /tmp/yum.log awk/ awk/1.txt 16 100% 7.81kB/s 0:00:00 (xfer#4, to-check=2/9) awk/10 48 100% 23.44kB/s 0:00:00 (xfer#5, to-check=1/9) awk/test.txt 846 100% 413.09kB/s 0:00:00 (xfer#6, to-check=0/9) sent 1899 bytes received 136 bytes 4070.00 bytes/sec total size is 1484 speedup is 0.73
·讓DEST中的文件比SRC新(新的不會被刪除):rsync -u
[root@localhost ~]# cat 111/a.txt [root@localhost ~]# echo "12l3rw8yrioauh" > /tmp/111_dest/a.txt [root@localhost ~]# rsync -avu 111/ /tmp/111_dest/ sending incremental file list ./ sent 167 bytes received 16 bytes 366.00 bytes/sec total size is 1484 speedup is 8.11 [root@localhost ~]# cat /tmp/111_dest/a.txt 12l3rw8yrioauh
·傳輸時壓縮:rsync -z
[root@localhost ~]# rsync -avPz 111/ /tmp/111_dest/ sending incremental file list sent 164 bytes received 13 bytes 354.00 bytes/sec total size is 1484 speedup is 8.38
(沒有什麽明顯效果,如果文件多時,可以節省帶寬)
10.31 rsync通過ssh同步
環境:A機器IP:192.168.65.128,B機器IP:192.168.65.129,互相可以通信
·rsync通過ssh方式同步(推文件):rsync [OPTION] … SRC [user@]host:DEST
[root@localhost ~]# rsync -av /etc/passwd 192.168.65.129:/tmp/arsenal.txt [email protected]'s password: sending incremental file list passwd sent 1053 bytes received 31 bytes 240.89 bytes/sec total size is 979 speedup is 0.90
B機器上文件內容:
·可以反過來同步(拉文件):rsync [OPTION] … [user@]host:SRC DEST
[root@localhost ~]# rsync -avP 192.168.65.129:/tmp/arsenal.txt /tmp/123.txt [email protected]'s password: receiving incremental file list arsenal.txt 979 100% 956.05kB/s 0:00:00 (xfer#1, to-check=0/1) sent 30 bytes received 1063 bytes 312.29 bytes/sec
·連接指定端口:
[root@localhost ~]# rsync -av -e "ssh -p 22" /etc/passwd 192.168.65.129:/tmp/123.txt [email protected]'s password: sending incremental file list passwd sent 1053 bytes received 31 bytes 240.89 bytes/sec total size is 979 speedup is 0.90
(-e 後面接命令,ssh -p 連接遠程端口)
2018-1-30 8周2次課 rsync