linux上的rsync命令詳解【轉】

1. rsync 簡介

rsync 就是遠程同步的意思remote sync.
rsync 被用在UNIX / Linux執行備份操作操作.
rsync 工具包被用來從一個位置到另一個位置高效地同步文件和文件夾. rsync可以實現在同一臺機器的不同文件直接備份,也可以跨服務器備份.

2. rsync的重要特性

  • 速度快: 初次同步時, rsync會全量拷貝從源文件或目錄到目標位置. 第二次往後同步時, rsync 僅僅會拷貝變化的數據塊或字節到目標位置這將使得文件傳輸非常迅速.
  • 安全: rsync 可以使用ssh協議加密傳輸.
  • 佔用帶寬少: rsync 在發送時會壓縮數據塊, 接收後再解壓縮數據塊. 所以和其他文件傳輸協議比起來, rsync在跨主機傳輸文件時會佔用較小的帶寬.
  • 不需要特殊權限: 安裝和運行rsync 不需要特殊權限.

3. 用法

rsync 語法如下

$  rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

從語法結構我們可以看出, 源和目標即可以在本地也可以在遠端. 如果是遠端的話,需要指明登錄用戶名, 遠端服務器名, 和遠端文件或目錄. 同時源可以是多個, 目標位置只能是一個.

4. 示例

4.1. 示例 1. 同步同一臺機上的兩個目錄

$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes  received 1098 bytes  54966.00 bytes/sec
total size is 44867  speedup is 1.63
$

說明:

-z: --compress 使用壓縮機制

-v: --verbose 打印詳細信息

-r: --recursive 以遞歸模式同步子目錄

注意: 同步完成後, 我們會發現文件的時間戳timestamps發生了改變.

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 bin  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root bin  949 Sep  2  2009 /root/temp/sva.xml

4.2. 示例 2: 保留文件的時間戳

有時我們希望拷貝或同步時, 時間戳不要發生變化, 源文件是什麼時間戳,目標文件就是什麼時間戳, 這時我們需要使用 -a --archive 歸檔模式選項. -a 選項相當於7個選項的組合 -rlptgoD

-r, --recursive: 遞歸模式Recursive mode
-l, --links: 將符號鏈接當作符號鏈接文件拷貝, 不拷貝符合鏈接指向的文件內容.
-p, --perms: 保留文件權限
-t, --times: 保留修改時間戳
-g, --group: 保留用戶組信息
-o, --owner: 保留用戶信息(需要超級用戶權限)
-D, 相當於 --devices --specials 的組合, 保留設備文件, 保留特殊文件.

$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent 26499 bytes  received 1104 bytes  55206.00 bytes/sec
total size is 44867  speedup is 1.63
$

同步完成後, 我們再來看文件屬性, 時間戳信息得到了保留, 不僅如此文件的所有者 和所在組也得到保留.

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /root/temp/sva.xml

4.3. 示例 3: 拷貝單個文件

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys

sent 42 bytes  received 12380 bytes  3549.14 bytes/sec
total size is 12288  speedup is 0.99

說明: Pubkeys 是一個普通文件

4.4. 示例 4. 從本地拷貝多個文件到遠端

使用rsync, 也可以從本地拷貝多個文件或目錄到遠端, 以下即爲示例:

$ rsync -avz /root/temp/ [email protected]:/home/thegeekstuff/temp/
Password:
building file list ... done
./
rpm/
rpm/Basenames
rpm/Conflictname

sent 15810261 bytes  received 412 bytes  2432411.23 bytes/sec
total size is 45305958  speedup is 2.87

注意:
與本地文件拷貝不同的地方在於, 當拷貝文件到遠程服務器時, 我們指定遠程主機上的用戶名, 服務器地址, 路徑等信息, 類是於使用scp命令拷貝, 如果沒有設置ssh免密碼登錄我們還需要提供遠程用戶的密碼等信息.有時你不想頻繁輸入密碼, 或者rsync運行在一個無人執守的腳本里面, 這是需要預先設置ssh免密登錄, 或者使用結合expect命令,自動輸入密碼, 但是出於安全考慮, 密碼需要加密. 所以在條件許可的情況下, 還是推薦設置ssh免密登錄.

4.5. 示例 5. 從遠程服務器拷貝文件到本地

與示例 4 稍有不同, 這時遠端目錄或文件作爲源位置, 本地目錄或文件作爲目標位置, 示例如下:

$ rsync -avz [email protected]:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

4.6. 示例 6. Remote shell for Synchronization

rsync 允許指定遠程主機上運行shell命令.
這時需要使用 -e 選項:
-e, --rsh=COMMAND 指定遠端使用的shell命令

Use rsync -e ssh to specify which remote shell to use. In this case, rsync will use ssh.

$ rsync -avz -e ssh [email protected]:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames

sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

4.7. 示例 7. 拷貝時不覆蓋目標位置已修改過的文件

在一下特殊的使用場景中, 我們不希望拷貝文件時, 我們不希望拷貝過程覆蓋掉目標位置中用戶做出的修改. 這時我們需要使用 -u 選項明確的告訴rsync命令保留用戶在目標文件中作出的修改. 在下面的例子中, 文件Basenames是用戶基於上次的拷貝, 修改過的文件, 當我們使用了-u 選項後, 該文件中的修改將不會被覆蓋掉.

$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames
$ rsync -avzu [email protected]:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/

sent 122 bytes  received 505 bytes  114.00 bytes/sec
total size is 45305958  speedup is 72258.31
$ ls -lrt
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames

4.8. 示例 8. 僅拷貝目錄結構, 不拷貝文件

在某些特殊場景中, 我們只需要遠程服務器上的目錄結構, 而不希望花大量時間, 帶寬拷貝文件內容, 這時我們可以使用 -d, --dirs選項來達到目的.

$ rsync -v -d [email protected]:/var/lib/ .
Password:
receiving file list ... done
logrotate.status
CAM/
YaST2/
acpi/

sent 240 bytes  received 1830 bytes  318.46 bytes/sec
total size is 956  speedup is 0.46

4.9. 示例 9. 文件傳輸時顯示進度

有時我們希望拷貝文件時, 能實時的顯示拷貝進度, 以及傳輸速率等信息. 尤其是拷貝大文件時, 程序不輸出信息, 用戶往往無法區分程序是在響應中, 還是已經掛起, 在這種情況下如果使用 –progress 就會非常有幫助.
rsync –progress option displays detailed progress of rsync execution as shown below.

$ rsync -avz --progress [email protected]:/var/lib/rpm/ /root/temp/
Password:
receiving file list ...
19 files to consider
./
Basenames
     5357568 100%   14.98MB/s    0:00:00 (xfer#1, to-check=17/19)
Conflictname
       12288 100%   35.09kB/s    0:00:00 (xfer#2, to-check=16/19)

sent 406 bytes  received 15810211 bytes  2108082.27 bytes/sec
total size is 45305958  speedup is 2.87

你也可以使用rsnapshot工具 (rsnapshot會調用rsync)來備份本地linux服務器, 或者備份遠程linux服務器.

4.10. 示例 10. 同步時刪除目標位置多餘的文件或目錄

有時我們希望目標文件和源文件保持嚴格一致, 不要多文件也不要少文件, 這是我們可能需要使用 -delete 選項來達到目的. 如果使用 -delete 選項, rsync將刪除目標位置多餘的文件或文件夾. 此選項還可以結合--delete-excluded 選項一起使用, 添加一些例外的文件.

# 現在Source and target 文件是一致的. 現在讓我們在目標位置創建一個新文件.
$ touch new-file.txt
$ rsync -avz --delete [email protected]:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./

sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

上述示例中, new-file.txt 是源文件中沒有的文件, 其將會在拷貝時被刪除掉.

4.11. 示例 11. Do not Create New File at the Target

在某些特殊的場景下, 我們只想更新, 目標位置已經存在的文件或目錄, 而不關心源位置的新文件, 這時我們可以使用-existing 選項僅僅更新已經存在的文件.
讓我們來驗證一下這個選項的功能, 首先在源端添加一個新文件 new-file.txt.

[/var/lib/rpm ]$ > new-file.txt
Next, execute the rsync from the target.
$ rsync -avz --existing [email protected]:/var/lib/rpm/ .
[email protected] password:
receiving file list..  done

sent 26 bytes  received 419 bytes  46.84 bytes/sec
total size is 88551424  speedup is 198991.96

從上面的例子可以看到, 由於加了--existing選項新文件new-file.txt沒有被拷貝到目標位置

4.12. 示例 12. 查看目標位置和源位置之間的差異

選項-i, --itemize-changes 非常有用, 當我們想了解目標位置和源位置的文件差異時.

在源端:

$ ls -l /var/lib/rpm
-rw-r--r-- 1 root root  5357568 2010-06-24 08:57 Basenames
-rw-r--r-- 1 root root    12288 2008-05-28 22:03 Conflictname
-rw-r--r-- 1 root root  1179648 2010-06-24 08:57 Dirnames

在目標端:

$ ls -l /root/temp
-rw-r--r-- 1 root root    12288 May 28  2008 Conflictname
-rw-r--r-- 1 bin  bin   1179648 Jun 24 05:27 Dirnames
-rw-r--r-- 1 root root        0 Sep  3 06:39 Basenames

注意: 在上面的例子中, 源位置和目標位置有兩處差異. 第一, 源文件Basenames的所有者和組 與 目標文件不同, 第一Dirnames文件大小也不一樣.
現在讓我們來看看rsync會怎樣顯示這些差異

$ rsync -avzi [email protected]:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames

sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec
total size is 45305958  speedup is 20.76

輸出信息只在相應文件前面顯示了9個字母來標識改變, 這些字母具體是什麼意思呢? 請參考以下詳細說明

表示文件已經被拷貝到了本地
f 代表該項目是一個文件.
s 代表文件大小發生了變化.
t 代表時間戳有差異.
o 所有者有差異
g 所屬組有差異.

4.13. 示例 13. 使用通配符過濾文件

rsync 可以使用--include 和 --exclude 選項結合通配符進行文件或文件夾過濾

$ rsync -avz --include 'P*' --exclude '*' [email protected]:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys

sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec
total size is 32768000  speedup is 3.19

在上面的示例中, 僅僅以P打頭的文件和文件夾被包含了進來, 其他的文件都被過濾在拷貝的過程中被排除在外了.

4.14. 示例 14. 不拷貝大文件

可以使用--max-size 告訴rsync 不要拷貝大小超過某個值的文件, 可以使用K, M, G指定文件大小, M for megabytes and G for gigabytes.

$ rsync -avz --max-size='100K' [email protected]:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername

sent 252 bytes  received 123081 bytes  18974.31 bytes/sec
total size is 45305958  speedup is 367.35

4.15. 示例 15. 拷貝整個文件

rsync 有個重要優點就是, 可以做到在拷貝的過程中, 只拷貝發生變化了的部分, 而不是發送整個文件.
但是在某些場景中, 比如文件較少, 文件size較小時, 我們的帶寬又足夠大, cpu資源相對又貧乏, 我們不希望它這樣做, 因爲畢竟計算源端和目標端的checksum, 並做對比, 也需要額外cpu開銷. 這時我們可以使用 -W, --whole-file 選項, 讓rsync不用計算那麼多, 一上來就直接開始傳送文件. 我們可以像下面這麼做.

#  rsync -avzW  [email protected]:/var/lib/rpm/ /root/temp
Password:
receiving file list ... done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name

sent 406 bytes  received 15810211 bytes  2874657.64 bytes/sec
total size is 45305958  speedup is 2.87

5. 後記

本文原文位於鵬叔的技術博客空間 - linux上的rsync命令詳解, 獲取最近更新請訪問原文.

更多linux相關知識, 請參考鵬叔的技術博客linux tag, 獲取實時更新的Linux文章.

6. 參考文檔

15個Rsync命令實例

轉自
轉載請註明出處, 更多博文請訪問https://www.cnblogs.com/guoapeng/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章