rsync之實戰
簡介
rsync是遠端(或本地)複製和同步檔案最常用的命令。藉助rsync命令,你可以跨目錄,跨磁碟和跨網路遠端與本地資料進行復制和同步。舉例來說:在兩臺Linux主機之間進行資料備份和映象。本文介紹在Linux主機上進行遠端和本地傳輸檔案的常見用法,不需要root賬戶也可以允許rsync。
rsync特性
- 高效地複製同步資料到對端,或者對端到本地
- 支援複製連結、裝置、屬主、屬組、許可權
- 比scp(Secure Copy)更快。rsync使用遠端更新協議(remote-update protocol),這允許僅僅傳輸兩組檔案之間的差異。對於首次傳輸,它將檔案或目錄的全部內容從源複製到目標,但是從下次起,它僅將變化部分複製到目標。
- Rsync消耗較少的頻寬,因為它使用壓縮和解壓縮方法,同時傳送和接收資料兩端。HTTP壓縮技術
基本語法
rsync options source destination
-v : 詳細模式輸出
-r : 遞迴拷貝資料,但是傳輸資料時不保留時間戳和許可權
-a : 歸檔模式, 歸檔模式總是遞迴拷貝,而且保留符號連結、許可權、屬主、屬組時間戳
-z : 壓縮傳輸
-h : human-readable
--progress: 顯示傳輸過程
--exclude=PATTERN 指定排除傳輸的檔案模式
--include=PATTERN 指定需要傳輸的檔案模式
--delete 同步時,刪除那些DST中有,而SRC沒有的檔案
--max-size:限定傳輸檔案大小的上限
--dry-run:顯示那些檔案將被傳輸,並不會實際傳輸
--bwlimit:限制傳輸頻寬
-W:拷貝檔案,不進行增量檢測
使用場景
本地拷貝同步檔案、目錄
同步一個檔案從本地一個目錄到另一個目錄,如果目標目錄不純在,會自動建立
[root@tecmint]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18Mspeedup is 1.10
同步目錄
[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00
遠端拷貝同步檔案、目錄
本地到遠端
[root@tecmint]$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/ root@192.168.0.101's password: sending incremental file list ./ httpd-2.2.3-82.el5.centos.i386.rpm mod_ssl-2.2.3-82.el5.centos.i386.rpm nagios-3.5.0.tar.gz nagios-plugins-1.4.16.tar.gz sent 4993369 bytes received 91 bytes 399476.80 bytes/sec total size is 4991313 speedup is 1.00
遠端到本地
[root@tecmint]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms root@192.168.0.100's password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 91 bytes received 4.99M bytes 322.16K bytes/sec total size is 4.99M speedup is 1.00
通過ssh使用rsync
SSH(Secure Shell)以加密方式傳輸資料時,接獲資料並破解很難。rsync同ssh一起使用可以增強傳輸按許可權,注意可能需要使用者密碼。
從本地到遠端
[root@tecmint]# rsync -avzhe ssh backup.tar root@192.168.0.100:/backups/ root@192.168.0.100's password: sending incremental file list backup.tar sent 14.71M bytes received 31 bytes 1.28M bytes/sec total size is 16.18M speedup is 1.10
從遠端到本地
[root@tecmint]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/ root@192.168.0.100's password: receiving incremental file list install.log sent 30 bytes received 8.12K bytes 1.48K bytes/sec total size is 30.74K speedup is 3.77
傳輸資料時顯示傳輸過程
使用--progress引數
[root@tecmint]# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs root@192.168.0.100's password: sending incremental file list created directory /root/rpmpkgs rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm 1.02M 100% 2.72MB/s 0:00:00 (xfer#1, to-check=3/5) rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm 99.04K 100% 241.19kB/s 0:00:00 (xfer#2, to-check=2/5) rpmpkgs/nagios-3.5.0.tar.gz 1.79M 100% 1.56MB/s 0:00:01 (xfer#3, to-check=1/5) rpmpkgs/nagios-plugins-1.4.16.tar.gz 2.09M 100% 1.47MB/s 0:00:01 (xfer#4, to-check=0/5) sent 4.99M bytes received 92 bytes 475.56K bytes/sec total size is 4.99M speedup is 1.00
使用--exclude和--include
傳輸R開頭的檔案、目錄,派出其他情況的檔案、目錄
[root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/var/lib/rpm/ /root/rpm root@192.168.0.101's password: receiving incremental file list created directory /root/rpm ./ Requirename Requireversion sent 67 bytes received 167289 bytes 7438.04 bytes/sec total size is 434176 speedup is 2.59
說明:
1. 如果多個需要排除或者包含的時候, 需要設定多個,例如
rsync -avzh --exclude=".svn" --exclude="temp" 說明:同步除.svn和temp之外的檔案 當然還有一種方式, 可以使用 --exclude-from=exclude.list 的方式指定檔案, 該檔案中填寫具體的過濾檔案 例如exclude.list檔案內容: .svn *.log
exclude-from指定的檔案最好是絕對路徑, 而exclude.list檔案中定義的則為相對路徑
使用--delete
--delete用於同步時,刪除那些DST中有,而SRC沒有的檔案
再target主機建立test.txt
[root@tecmint]# touch test.txt [root@tecmint]# rsync -avz --delete root@192.168.0.100:/var/lib/rpm/ . Password: receiving file list ... done deleting test.txt ./ sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55
使用--max-size
--max-size用於限制傳輸時檔案的大小,只有≤max-size的檔案才會被傳輸
[root@tecmint]# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.100:/root/tmprpm root@192.168.0.100's password: sending incremental file list created directory /root/tmprpm ./ Conflictname Group Installtid Name Provideversion Pubkeys Requireversion Sha1header Sigmd5 Triggername __db.001 sent 189.79K bytes received 224 bytes 13.10K bytes/sec total size is 38.08M speedup is 200.43
傳輸完畢後自動刪除原始檔、目錄
假設你有一個主Web Server和一個數據備份Sever,你建立了每日備份任務並與備份伺服器同步備份,你不希望在Web Server中保留該本地備份副本。
那麼,你是否會等待傳輸完成然後手動刪除這些本地備份檔案? 當然不。 可以使用--remove-source-files選項完成此自動刪除
[root@tecmint]# rsync --remove-source-files -zvh backup.tar /tmp/backups/
backup.tar
sent 14.71M bytes received 31 bytes 4.20M bytes/sec
total size is 16.18M speedup is 1.10
[root@tecmint]# ll backup.tar
ls: backup.tar: No such file or directory
使用--dry-run
如果你對rsync不熟悉,貿然使用rsync可能會搞亂對端檔案、目錄。藉助--dry-run可以讓你知道會傳輸些什麼東西,但實際上並沒有傳輸任何東西。如果輸出結果與你的預期吻合,可以去掉--dry-run,進行實際的傳輸工作。
root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/ backup.tar sent 35 bytes received 15 bytes 100.00 bytes/sec total size is 16.18M speedup is 323584.00 (DRY RUN)
設定同步時頻寬
使用--bwlimit可以設定同步時網路頻寬上限
[root@tecmint]# rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ root@192.168.0.100:/root/tmprpm/ root@192.168.0.100's password: sending incremental file list sent 324 bytes received 12 bytes 61.09 bytes/sec total size is 38.08M speedup is 113347.05
rsync由於採用遠端更新協議(remote-update protocol),預設是同步變化的位元組或塊。使用-W可以取消這種機制,整個檔案同步
[root@tecmint]# rsync -zvhW backup.tar /tmp/backups/backup.tar backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10