Linux遠(yuǎn)程備份工具Rsync使用案例
參考 : http://www.thegeekstuff.com/2010/09/rsync-command-examples/
注:下面所有例子中 – - 之間實(shí)際上是沒有空格的,使用時(shí)請(qǐng)刪除空格。
Rsync是一個(gè)遠(yuǎn)程數(shù)據(jù)同步工具,可通過LAN 或互聯(lián)網(wǎng)快速同步多臺(tái)主機(jī)間的文件。Rsync 本來是用以取代 rcp的一個(gè)工具,它當(dāng)前由 rsync.samba.org 維護(hù)。Rsync 使用所謂的”Rsync演算法”來使本地和遠(yuǎn)程兩個(gè)主機(jī)之間的文件達(dá)到同步,這個(gè)算法只傳送兩個(gè)文件的不同部分,而不是每次都整份傳送,因此速度相當(dāng)快。
Rsync 的特色:
- 快速:***次同步時(shí) rsync 會(huì)復(fù)制全部內(nèi)容,但在下一次只傳輸修改過的文件。
- 安全:rsync 允許通過 ssh 協(xié)議來加密傳輸數(shù)據(jù)。
- 更少的帶寬:rsync 在傳輸數(shù)據(jù)的過程中可以實(shí)行壓縮及解壓縮操作,因此可以使用更少的帶寬。
- 特權(quán):安裝和執(zhí)行 rsync 無需特別的權(quán)限
基本語法:
rsync options source destination
源和目標(biāo)都可以是本地或遠(yuǎn)程,在進(jìn)行遠(yuǎn)程傳輸?shù)臅r(shí)候,需要指定登錄名、遠(yuǎn)程服務(wù)器及文件位置
樣例:
1 在本地機(jī)器上對(duì)兩個(gè)目錄進(jìn)行同步
$ 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 $
參數(shù):
- -z 開啟壓縮
- -v 詳情輸出
- -r 表示遞歸
2 利用 rsync -a 讓同步時(shí)保留時(shí)間標(biāo)記
rsync 選項(xiàng) -a 稱為歸檔模式,執(zhí)行以下操作
- 遞歸模式
- 保留符號(hào)鏈接
- 保留權(quán)限
- 保留時(shí)間標(biāo)記
- 保留用戶名及組名
$ 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 $
3 僅同步一個(gè)文件
$ 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
4 從本地同步文件到遠(yuǎn)程服務(wù)器
$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/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
就像你所看到的,需要在遠(yuǎn)程目錄前加上 ssh 登錄方式,格式為 username@machinename:path
5 同步遠(yuǎn)程文件到本地
和上面差不多,做個(gè)相反的操作
$ rsync -avz thegeekstuff@192.168.200.10:/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
6 同步時(shí)指定遠(yuǎn)程 shell
用 -e 參數(shù)可以指定遠(yuǎn)程 ssh ,比如用 rsync -e ssh 來指定為 ssh
$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/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
7 不要覆蓋被修改過的目的文件
使用 rsync -u 選項(xiàng)可以排除被修改過的目的文件
$ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ rsync -avzu thegeekstuff@192.168.200.10:/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
8 僅僅同步目錄權(quán)(不同步文件)
使用 -d 參數(shù)
$ rsync -v -d thegeekstuff@192.168.200.10:/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
9 查看每個(gè)文件的傳輸進(jìn)程
使用 – -progress 參數(shù)
$ rsync -avz – -progress thegeekstuff@192.168.200.10:/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
10 刪除在目的文件夾中創(chuàng)建的文件
用 – -delete 參數(shù)
# Source and target are in sync. Now creating new file at the target. $ > new-file.txt $ rsync -avz – -delete thegeekstuff@192.168.200.10:/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
11 不要在目的文件夾中創(chuàng)建新文件
有時(shí)能只想同步目的地中存在的文件,而排除源文件中新建的文件,可以使用 – -exiting 參數(shù)
$ rsync -avz –existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2′s password: receiving file list … done ./ sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96
12 查看源和目的文件之間的改變情況
用 -i 參數(shù)
$ rsync -avzi thegeekstuff@192.168.200.10:/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
輸出結(jié)果中在每個(gè)文件最前面會(huì)多顯示 9 個(gè)字母,分別表示為
> 已經(jīng)傳輸
f 表示這是一個(gè)文件
d 表示這是一個(gè)目錄
s 表示尺寸被更改
t 時(shí)間標(biāo)記有變化
o 用戶被更改
g 用戶組被更改
13 在傳輸時(shí)啟用包含和排除模式
$ rsync -avz – -include ‘P*’ – -exclude ‘*’ thegeekstuff@192.168.200.10:/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
14 不要傳輸大文件
使用 – - max-size 參數(shù)
$ rsync -avz – -max-size=’100K’ thegeekstuff@192.168.200.10:/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
15 傳輸所有文件
不管有沒有改變,再次把所有文件都傳輸一遍,用 -W 參數(shù)
# rsync -avzW thegeekstuff@192.168.200.10:/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