10.28 rsync工具介紹
rsync指令是一個遠端資料同步工具,可通過LAN/WAN快速同步多台主機間的檔案。rsync使用所謂的“rsync算法”來使本地和遠端兩個主機之間的檔案達到同步,這個算法隻傳送兩個檔案的不同部分,而不是每次都整份傳送,是以速度相當快。
文法
rsync有六種不同的工作模式:
mark
注: src表示源檔案,dest表示目的檔案
10.29 Options
-a:包含-rtplgoD
-r:同步目錄時加上表示對子目錄進行遞歸處理
-t:保持檔案的時間屬性
-p:保持檔案的權限屬性
-l:保留軟連結
-g:儲存檔案數組
-o:保持檔案的屬主
-D:儲存裝置檔案資訊
-v:=visual,可視化
-L:同步軟連結的同時同步其源檔案
-P:顯示同步過程,比v更詳細
-u:=update,加上該選項,如果DEST中檔案比SRC中的新,則不同步
-z:=zip,傳輸時壓縮
--delete:删除DEST中SRC沒有的檔案
--exclude:過濾指定檔案,不同步
10.30 文法應用:
同步本地檔案
[root@adai003 ~]# rsync -av /etc/passwd /tmp/11.txt
sending incremental file list
passwd
sent 1222 bytes received 31 bytes 2506.00 bytes/sec
total size is 1148 speedup is 0.92
将本地機器的内容拷貝到遠端機器
[root@adai003 ~]# rsync -av /etc/passwd [email protected]:/tmp/11.txt
[email protected]'s password:
sent 1222 bytes received 31 bytes 358.00 bytes/sec
注: 進行遠端同步的時候,遠端機和本地機必須都安裝有rsync工具才可以。
将本地機器的内容拷貝到遠端機器,當端口不一緻時:
[root@adai003 ~]# rsync -av -e "ssh -p 22" /etc/passwd [email protected]:/tmp/11.txt
sent 31 bytes received 12 bytes 7.82 bytes/sec
total size is 1148 speedup is 26.70
将遠端機器的内容拷貝到本地機器。
[root@adai003 ~]# rsync -av [email protected]:/etc/passwd /tmp/22.txt
receiving incremental file list
sent 42 bytes received 83 bytes 27.78 bytes/sec
total size is 1149 speedup is 9.19
10.31選項應用
rsync -avP 顯示詳盡的同步過程
[root@adai003 ~]# rsync -avP /root/grep/ /tmp/grep_dest/
created directory /tmp/grep_dest
./
717.sh
65 100% 0.00kB/s 0:00:00 (xfer#1, to-check=20/22)
aaa.sed.shell
47 100% 3.53kB/s 0:00:00 (xfer#2, to-check=19/22)
…………
rsync -avL 同步軟連結的同時同步其源檔案
[root@adai003 ~]# rsync -avL /root/grep/ /tmp/grep_dest/
adailink
sent 1530 bytes received 32 bytes 3124.00 bytes/sec
total size is 14275 speedup is 9.14
rsync -av --delete 删除目标檔案中和源檔案中不一樣的檔案
[root@adai003 ~]# touch /tmp/grep_dest/new2.txt
[root@adai003 ~]# rsync -av --delete /root/grep/ /tmp/grep_dest/
deleting new2.txt ##删除檔案new2.txt
adailink -> /root/awk/test.txt
sent 405 bytes received 19 bytes 848.00 bytes/sec
total size is 13178 speedup is 31.08
rsync -av --exclude 傳輸時過濾掉指定檔案
[root@adai003 ~]# rsync -av --exclude ".txt" --exclude ".shell" /root/grep/ /tmp/grep_dest/
boot.log.bak
注: 可以同時過濾多種檔案。
本文轉自 芬野 51CTO部落格,原文連結:http://blog.51cto.com/yuanhaohao/2047458,如需轉載請自行聯系原作者