Linux命令——rsync


參考:Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux 

How to Sync Files/Directories Using Rsync with Non-standard SSH Port

How to Use Rsync to Sync New or Changed/Modified Files in Linux

How to Sync Two Apache Web Servers/Websites Using Rsync

簡介

rsync是遠程(或本地)復制和同步文件最常用的命令。 借助rsync命令,你可以跨目錄,跨磁盤和跨網絡遠程與本地數據進行復制和同步。舉例來說:在兩台Linux主機之間進行數據備份和鏡像。本文介紹在Linux主機上進行遠程和本地傳輸文件的常見用法,不需要root賬戶也可以允許rsync。

rsync特性

  1. 高效地復制同步數據到對端,或者對端到本地
  2. 支持復制鏈接、設備、屬主、屬組、權限
  3. 比scp(Secure Copy)更快。rsync使用遠程更新協議( remote-update protocol ),這允許僅僅傳輸兩組文件之間的差異。對於首次傳輸,它將文件或目錄的全部內容從源復制到目標,但是從下次起,它僅將變化部分復制到目標。
  4. 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.18M  speedup is 1.10
View Code

再演示同步目錄

[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
View Code

遠程拷貝同步文件、目錄

本地到遠程

[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
View Code

遠程到本地

[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
View Code

通過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
View Code

從遠程到本地

[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
View Code

傳輸數據時顯示傳輸過程

使用--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
View Code

使用--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
View Code

使用--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
View Code

使用--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
View Code

傳輸完畢后自動刪除源文件、目錄

假設你有一個主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
View Code

使用--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)
View Code

設置同步時帶寬

使用--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
View Code

同步整個文件

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
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM