http://os.51cto.com/art/201009/225962.htm
Linux遠程備份工具Rsync使用案例
Rsync是一個遠程數據同步工具,可通過LAN或互聯網快速同步多臺主機間的文件。Rsync具有快速、安全、佔用帶寬少以及無需特權等特色。本文舉例介紹了15種使用Rsync進行遠程數據同步的方法,幫助大家更好的學習Rsync的使用。
AD:
參考 :http://www.thegeekstuff.com/2010/09/rsync-command-examples/
注:下面所有例子中 – -之間實際上是沒有空格的,使用時請刪除空格。
Rsync是一個遠程數據同步工具,可通過LAN或互聯網快速同步多臺主機間的文件。Rsync本來是用以取代 rcp的一個工具,它當前由rsync.samba.org 維護。Rsync使用所謂的”Rsync演算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。
Rsync的特色:
快速:第一次同步時 rsync會複製全部內容,但在下一次只傳輸修改過的文件。
安全:rsync允許通過 ssh協議來加密傳輸數據。
更少的帶寬:rsync在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬。
特權:安裝和執行 rsync無需特別的權限
基本語法:
rsyncoptions source destination
源和目標都可以是本地或遠程,在進行遠程傳輸的時候,需要指定登錄名、遠程服務器及文件位置
樣例:
1在本地機器上對兩個目錄進行同步
$rsync -zvr /var/opt/installation/inventory/ /root/temp
buildingfile list … done
sva.xml
svB.xml
.
sent26385 bytes received 1098 bytes 54966.00 bytes/sec
totalsize is 44867 speedup is 1.63
$
參數:
-z開啓壓縮
-v詳情輸出
-r表示遞歸
2利用 rsync -a讓同步時保留時間標記
rsync選項 -a 稱爲歸檔模式,執行以下操作
遞歸模式
保留符號鏈接
保留權限
保留時間標記
保留用戶名及組名
$rsync -azv /var/opt/installation/inventory/ /root/temp/
buildingfile list … done
./
sva.xml
svB.xml
.
sent26499 bytes received 1104 bytes 55206.00 bytes/sec
totalsize is 44867 speedup is 1.63
$
3僅同步一個文件
$rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys
sent42 bytes received 12380 bytes 3549.14 bytes/sec
totalsize is 12288 speedup is 0.99
4從本地同步文件到遠程服務器
$rsync -avz /root/temp/[email protected]:/home/thegeekstuff/temp/
Password:
buildingfile list … done
./
rpm/
rpm/Basenames
rpm/Conflictname
sent15810261 bytes received 412 bytes 2432411.23 bytes/sec
totalsize is 45305958 speedup is 2.87
就像你所看到的,需要在遠程目錄前加上ssh 登錄方式,格式爲username@machinename:path
5同步遠程文件到本地
和上面差不多,做個相反的操作
$rsync -avz [email protected]:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/
rpm/Basenames
.
sent406 bytes received 15810230 bytes 2432405.54 bytes/sec
totalsize is 45305958 speedup is 2.87
6同步時指定遠程 shell
用 -e參數可以指定遠程 ssh,比如用 rsync -e ssh來指定爲 ssh
$rsync -avz -e ssh [email protected]:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/
rpm/Basenames
sent406 bytes received 15810230 bytes 2432405.54 bytes/sec
totalsize is 45305958 speedup is 2.87
7不要覆蓋被修改過的目的文件
使用 rsync-u 選項可以排除被修改過的目的文件
$ls -l /root/temp/Basenames
total39088
-rwxr-xr-x1 root root 4096 Sep 2 11:35 Basenames
$rsync -avzu [email protected]:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/
sent122 bytes received 505 bytes 114.00 bytes/sec
totalsize is 45305958 speedup is 72258.31
$ls -lrt
total39088
-rwxr-xr-x1 root root 4096 Sep 2 11:35 Basenames
8僅僅同步目錄權(不同步文件)
使用 -d參數
$rsync -v -d [email protected]:/var/lib/ .
Password:
receivingfile list … done
logrotate.status
CAM/
YaST2/
acpi/
sent240 bytes received 1830 bytes 318.46 bytes/sec
totalsize is 956 speedup is 0.46
9查看每個文件的傳輸進程
使用 – -progress參數
$rsync -avz – -progress [email protected]:/var/lib/rpm//root/temp/
Password:
receivingfile list …
19files to consider
./
Basenames
5357568100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19)
Conflictname
12288100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent406 bytes received 15810211 bytes 2108082.27 bytes/sec
totalsize is 45305958 speedup is 2.87
10刪除在目的文件夾中創建的文件
用 – -delete參數
#Source and target are in sync. Now creating new file at the target.
$> new-file.txt
$rsync -avz – -delete [email protected]:/var/lib/rpm/ .
Password:
receivingfile list … done
deletingnew-file.txt
./
sent26 bytes received 390 bytes 48.94 bytes/sec
totalsize is 45305958 speedup is 108908.55
11不要在目的文件夾中創建新文件
有時能只想同步目的地中存在的文件,而排除源文件中新建的文件,可以使用– -exiting 參數
$rsync -avz –existing [email protected]:/var/lib/rpm/ .
[email protected]′spassword:
receivingfile list … done
./
sent26 bytes received 419 bytes 46.84 bytes/sec
totalsize is 88551424 speedup is 198991.96
12查看源和目的文件之間的改變情況
用 -i參數
$rsync -avzi [email protected]:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
>f.st….Basenames
.f….og.Dirnames
sent48 bytes received 2182544 bytes 291012.27 bytes/sec
totalsize is 45305958 speedup is 20.76
輸出結果中在每個文件最前面會多顯示9 個字母,分別表示爲
>已經傳輸
f表示這是一個文件
d表示這是一個目錄
s表示尺寸被更改
t時間標記有變化
o用戶被更改
g用戶組被更改
13在傳輸時啓用包含和排除模式
$rsync -avz – -include ‘P*’ – -exclude ‘*’[email protected]:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
./
Packages
Providename
Provideversion
Pubkeys
sent129 bytes received 10286798 bytes 2285983.78 bytes/sec
totalsize is 32768000 speedup is 3.19
14不要傳輸大文件
使用 – -max-size 參數
$rsync -avz – -max-size=’100K’[email protected]:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername
sent252 bytes received 123081 bytes 18974.31 bytes/sec
totalsize is 45305958 speedup is 367.35
15傳輸所有文件
不管有沒有改變,再次把所有文件都傳輸一遍,用-W 參數
#rsync -avzW [email protected]:/var/lib/rpm/ /root/temp
Password:
receivingfile list … done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name
sent406 bytes received 15810211 bytes 2874657.64 bytes/sec
totalsize is 45305958 speedup is 2.87