Rsync的使用方法
生活随笔
收集整理的這篇文章主要介紹了
Rsync的使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?目錄:
一、什么是Rsync二、Rsync同步算法
三、Rsync安裝配置
(一)rsync的安裝配置
1 rsync軟件包的獲取
2 rsync的安裝
3 rsync的配置
(二)rsyncd.conf配置文件參數說明
1 全局參數
2 模塊參數
(三)rsync常用命令
四、Rsync實例測試
(一)測試環境
(二)測試目的
(三)測試方式及測試結果
1 SSH方式
2 后臺服務方式
3 使用crontab建立腳本 實現定期同步備份文件
4 與最佳搭檔inotify的合作 實現實時觸控式自動更新
5 使用inotify+rsync實現雙向同步更新文件
(四)測試結果數據分析
1 測試目的
2 測試數據來源
3 測試結果數據(三次測試結果)
4 測試結果數據分析
五、問題排除及注意事項
附錄一:源主機和備份服務器的配置文檔
附錄二:Rsync的INSTLL文檔
附錄三:Inotify的INSTALL文檔
一、什么是Rsync
Rsync(remote synchronize),顧名思義,可以知道這是一個遠程數據同步工具,可通過LAN/WAN快速同步多臺主機間的文件。Rsync使用所謂的“Rsync算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。
Rsync本來是用于替代rcp的一個工具,目前由rsync.samba.org維護,所以rsync.conf文件的格式類似于samba 的主配置文件。Rsync可以通過rsh或ssh使用,也能以daemon模式去運行,在以daemon方式運行時Rsync server會打開一個873端口,等待客戶端去連接。連接時,Rsync server會檢查口令是否相符,若通過口令查核,則可以開始進行文件傳輸。第一次連通完成時,會把整份文件傳輸一次,以后則就只需進行增量備份。
Rsync支持大多數的類Unix系統,無論是Linux、Solaris還是BSD上都經過了良好的測試。此外,它在windows平臺下也有相應的版本,如cwRsync和Sync2NAS等工具。
Rsync的基本特點如下:
1.可以鏡像保存整個目錄樹和文件系統;
2.可以很容易做到保持原來文件的權限、時間、軟硬鏈接等;
3.無須特殊權限即可安裝;
4.優化的流程,文件傳輸效率高;
5.可以使用rsh、ssh等方式來傳輸文件,當然也可以通過直接的socket連接;
6.支持匿名傳輸。
二、Rsync同步算法
Rsync之所以同步文件的速度相當快,是因為“Rsync同步算法”能在很短的時間內計算出需要備份的數據,關于Rsync的同步算法描述如下:假定在1號和2號兩臺計算機之間同步相似的文件A與B,其中1號對文件A擁有訪問權,2號對文件B擁有訪問權。并且假定主機1號與2號之間的網絡帶寬很小。那么rsync算法將通過下面的五個步驟來完成:
1、2號將文件B分割成一組不重疊的固定大小為S字節的數據塊,最后一塊可能會比S 小
2、2號對每一個分割好的數據塊執行兩種校驗:一種是32位的滾動弱校驗,另一種是128位的MD4強校驗
3、2號將這些校驗結果發給1號
4、1號通過搜索文件A的所有大小為S的數據塊(偏移量可以任選,不一定非要是S的倍數),來尋找與文件B的某一塊有著相同的弱校驗碼和強校驗碼的數據塊。這項工作可以借助滾動校驗的特性很快完成
5、1號發給2號一串指令來生成文件A在2號上的備份。這里的每一條指令要么是對文件B經擁有某一個數據塊而不須重傳的證明,要么是一個數據塊,這個數據塊肯定是沒有與文件B的任何一個數據塊匹配上的
三、Rsync的安裝配置
Rsync目前由rsync.samba.org維護,所以rsync.conf文件的格式類似于samba 的主配置文件,其主配置文件為rsyncd.conf。
(一)rsync的安裝配置
1、rsync軟件包的獲取
rsync的主頁地址為:http://rsync.samba.org/
最新版為rsync-3.0.8.tar.gz
下載到本地src文件夾:
[promise@bogon src]# wget?http://rsync.samba.org/ftp/rsync/src/rsync-3.0.8.tar.gz
2、rsync的安裝
解壓:
[promise@bogon src]# tar -zxvf rsync-3.0.8.tar.gz
進入rsync-3.0.8文件夾:
[promise@bogon src]# cd rsync-3.0.8
切換到root權限:
[promise@bogon src]# su -
編譯安裝:
[root@bogon rsync-3.0.8]# ./configure –prefix=/usr
[root@bogon rsync-3.0.8]# make && make install
清理現場:
[root@bogon rsync-3.0.8]# make clean
3、rsync的配置
修改/etc/xinetd.d/rsync文件:
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#? ?? ? allows crc checksumming etc.
service rsync
{
disable = yes
socket_type? ???= stream
wait? ?? ?? ?? ?= no
user? ?? ?? ?? ?= root
server? ?? ?? ? = /usr/bin/rsync
server_args? ???= –daemon
log_on_failure??+= USERID
}
將 disable = yes
改為 disable = no
保存退出
(二)rsyncd.conf配置文件參數說明
1、全局參數
在文件中[module]之前的所有參數都是全局參數,當然也可以在全局參數部分定義模塊參數,這時候該參數的值就是所有模塊的默認值。
Port
指定后臺程序使用的端口號,默認為873。
motd file
“motd file”參數用來指定一個消息文件,當客戶連接服務器時該文件的內容顯示給客戶,默認是沒有motd文件的。
log file
“log file”指定rsync的日志文件,而不將日志發送給syslog。比如可指定為“/var/log/rsyncd.log”。
pid file
指定rsync的pid文件,通常指定為“/var/run/rsyncd.pid”。
syslog facility
指定rsync發送日志消息給syslog時的消息級別,常見的消息級別是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7。默認值是daemon。
2、模塊參數
主要是定義服務器哪個目錄要被同步。其格式必須為“[module]”形式,這個名字就是在rsync 客戶端看到的名字,其實有點象Samba服務器提供的共享名。而服務器真正同步的數據是通過 path 來指定的。我們可以根據自己的需要,來指定多個模塊,模塊中可以定義以下參數:
comment
給模塊指定一個描述,該描述連同模塊名在客戶連接得到模塊列表時顯示給客戶。默認沒有描述定義。
path
指定該模塊的供備份的目錄樹路徑,該參數是必須指定的。
use chroot
如果”use chroot”指定為true,那么rsync在傳輸文件以前首先chroot到path參數所指定的目錄下。這樣做的原因是實現額外的安全防護,但是缺點是需要以root權限,并且不能備份指向外部的符號連接所指向的目錄文件。默認情況下chroot值為true。
uid
該選項指定當該模塊傳輸文件時守護進程應該具有的uid,配合gid選項使用可以確定哪些可以訪問怎么樣的文件權限,默認值是”nobody”。
gid
該選項指定當該模塊傳輸文件時守護進程應該具有的gid。默認值為”nobody”。
max connections
指定該模塊的最大并發連接數量以保護服務器,超過限制的連接請求將被告知隨后再試。默認值是0,也就是沒有限制。
list
該選項設定當客戶請求可以使用的模塊列表時,該模塊是否應該被列出。如果設置該選項為false,可以創建隱藏的模塊。默認值是true。
read only
該選項設定是否允許客戶上載文件。如果為true那么任何上載請求都會失敗,如果為false并且服務器目錄讀寫權限允許那么上載是允許的。默認值為true。
exclude
用來指定多個由空格隔開的多個文件或目錄(相對路徑),并將其添加到exclude列表中。這等同于在客戶端命令中使用–exclude來指定模式,一個模塊只能指定一個exclude選項。但是需要注意的一點是該選項有一定的安全性問題,客戶很有可能繞過exclude列表,如果希望確保特定的文件不能被訪問,那就最好結合uid/gid選項一起使用。
exclude from
指定一個包含exclude模式的定義的文件名,服務器從該文件中讀取exclude列表定義。
include
用來指定不排除符合要求的文件或目錄。這等同于在客戶端命令中使用–include來指定模式,結合include和exclude可以定義復雜的exclude/include規則。
include from
指定一個包含include模式的定義的文件名,服務器從該文件中讀取include列表定義。
auth users
該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這里的用戶和系統用戶沒有任何關系。如果”auth users”被設置,那么客戶端發出對該模塊的連接請求以后會被rsync請求challenged進行驗證身份這里使用的 challenge/response認證協議。用戶的名和密碼以明文方式存放在”secrets file”選項指定的文件中。默認情況下無需密碼就可以連接模塊(也就是匿名方式)。
secrets file
該選項指定一個包含定義用戶名:密碼對的文件。只有在”auth users”被定義時,該文件才有作用。文件每行包含一個username:passwd對。一般來說密碼最好不要超過8個字符。沒有默認的 secures file名,需要限式指定一個(例如:/etc/rsyncd.passwd)。注意:該文件的權限一定要是600,否則客戶端將不能連接服務器。
strict modes
該選項指定是否監測密碼文件的權限,如果該選項值為true那么密碼文件只能被rsync服務器運行身份的用戶訪問,其他任何用戶不可以訪問該文件。默認值為true。
hosts allow
該選項指定哪些IP的客戶允許連接該模塊。客戶模式定義可以是以下形式:單個IP地址,例如:172.16.10.16;整個網段,例如:172.16.10.0/24,也可以是172.16.0.0/255.255.255.0 多個IP或網段需要用空格隔開,“*”則表示所有,默認是允許所有主機連接。
hosts deny
指定不允許連接rsync服務器的機器,可以使用hosts allow的定義方式來進行定義。默認是沒有hosts deny定義。
ignore errors
指定rsyncd在判斷是否運行傳輸時的刪除操作時忽略server上的IO錯誤,一般來說rsync在出現IO錯誤時將將跳過–delete操作,以防止因為暫時的資源不足或其它IO錯誤導致的嚴重問題。
ignore nonreadable
指定rysnc服務器完全忽略那些用戶沒有訪問權限的文件。這對于在需要備份的目錄中有些文件是不應該被備份者得到的情況是有意義的。
lock file
指定支持max connections參數的鎖文件,默認值是/var/run/rsyncd.lock。
transfer logging
使rsync服務器使用ftp格式的文件來記錄下載和上載操作在自己單獨的日志中。
log format
通過該選項用戶在使用transfer logging可以自己定制日志文件的字段。其格式是一個包含格式定義符的字符串,可以使用的格式定義符如下所示:
%h 遠程主機名
%a 遠程IP地址
%l 文件長度字符數
%p 該次rsync會話的進程id
%o 操作類型:”send”或”recv”
%f 文件名
%P 模塊路徑
%m 模塊名
%t 當前時間
%u 認證的用戶名(匿名時是null)
%b 實際傳輸的字節數
%c 當發送文件時,該字段記錄該文件的校驗碼
默認log格式為:”%o %h [%a] %m (%u) %f %l”,一般來說,在每行的頭上會添加”%t [%p] “。在源代碼中同時發布有一個叫rsyncstats的perl腳本程序來統計這種格式的日志文件。
timeout
通過該選項可以覆蓋客戶指定的IP超時時間。通過該選項可以確保rsync服務器不會永遠等待一個崩潰的客戶端。超時單位為秒鐘,0表示沒有超時定義,這也是默認值。對于匿名rsync服務器來說,一個理想的數字是600。
refuse options
通過該選項可以定義一些不允許客戶對該模塊使用的命令參數列表。這里必須使用命令全名,而不能是簡稱。但發生拒絕某個命令的情況時服務器將報告錯誤信息然后退出。如果要防止使用壓縮,應該是:”dont compress = *”。
dont compress
用來指定那些不進行壓縮處理再傳輸的文件,默認值是*.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
(三)rsync常用命令
在對rsync服務器配置結束以后,下一步就需要在客戶端發出rsync命令來實現將服務器端的文件備份到客戶端來。rsync是一個功能非常強大的工具,其命令也有很多功能特色選項,下面就對它的選項一一進行分析說明。 Rsync的命令格式可以為以下六種:
rsync [OPTION]… SRC DEST
rsync [OPTION]… SRC [USER@]HOSTEST
rsync [OPTION]… [USER@]HOST:SRC DEST
rsync [OPTION]… [USER@]HOST::SRC DEST
rsync [OPTION]… SRC [USER@]HOST:EST
rsync [OPTION]… rsync://[USER@]HOST[ORT]/SRC [DEST]
對應于以上六種命令格式,rsync有六種不同的工作模式:
1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號”:“分隔符時就啟動這種工作模式。如:rsync -a /data /backup
2)使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包含單個冒號”:“分隔符時啟動該模式。如:rsync -avz *.c foo:src
3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑包含單個冒號”:“分隔符時啟動該模式。如:rsync -avz foo:src/bar /data
4)從遠程rsync服務器中拷貝文件到本地機。當SRC路徑信息包含”::“分隔符時啟動該模式。如:rsync -avroot@172.16.10.16::www?/databack
5)從本地機器拷貝文件到遠程rsync服務器中。當DST路徑信息包含”::“分隔符時啟動該模式。如:rsync -av /databackroot@172.16.10.16::www
6)列遠程機的文件列表。這類似于rsync傳輸,不過只要在命令中省略掉本地機信息即可。如:rsync -v rsync://172.16.10.16/www
rsync參數的具體解釋如下:
-v, –verbose 詳細模式輸出
-q, –quiet 精簡輸出模式
-c, –checksum 打開校驗開關,強制對文件傳輸進行校驗
-a, –archive 歸檔模式,表示以遞歸方式傳輸文件,并保持所有文件屬性,等于-rlptgoD
-r, –recursive 對子目錄以遞歸模式處理
-R, –relative 使用相對路徑信息
-b, –backup 創建備份,也就是對于目的已經存在有同樣的文件名時,將老的文件重新命名為~filename。可以使用–suffix選項來指定不同的備份文件前綴。
–backup-dir 將備份文件(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份文件前綴
-u, –update 僅僅進行更新,也就是跳過所有已經存在于DST,并且文件時間晚于要備份的文件。(不覆蓋更新的文件)
-l, –links 保留軟鏈結
-L, –copy-links 想對待常規文件一樣處理軟鏈結
–copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹以外的鏈結
–safe-links 忽略指向SRC路徑目錄樹以外的鏈結
-H, –hard-links 保留硬鏈結
-p, –perms 保持文件權限
-o, –owner 保持文件屬主信息
-g, –group 保持文件屬組信息
-D, –devices 保持設備文件信息
-t, –times 保持文件時間信息
-S, –sparse 對稀疏文件進行特殊處理以節省DST的空間
-n, –dry-run現實哪些文件將被傳輸
-W, –whole-file 拷貝文件,不進行增量檢測
-x, –one-file-system 不要跨越文件系統邊界
-B, –block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節
-e, –rsh=COMMAND 指定使用rsh、ssh方式進行數據同步
–rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息
-C, –cvs-exclude 使用和CVS一樣的方法自動忽略文件,用來排除那些不希望傳輸的文件
–existing 僅僅更新那些已經存在于DST的文件,而不備份那些新創建的文件
–delete 刪除那些DST中SRC沒有的文件
–delete-excluded 同樣刪除接收端那些被該選項指定排除的文件
–delete-after 傳輸結束以后再刪除
–ignore-errors 及時出現IO錯誤也進行刪除
–max-delete=NUM 最多刪除NUM個文件
–partial 保留那些因故沒有完全傳輸的文件,以是加快隨后的再次傳輸
–force 強制刪除目錄,即使不為空
–numeric-ids 不將數字的用戶和組ID匹配為用戶名和組名
–timeout=TIME IP超時時間,單位為秒
-I, –ignore-times 不跳過那些有同樣的時間和長度的文件
–size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間
–modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認為0
-T –temp-dir=DIR 在DIR中創建臨時文件
–compare-dest=DIR 同樣比較DIR中的文件來決定是否需要備份
-P 等同于 –partial
–progress 顯示備份過程
-z, –compress 對備份的文件在傳輸時進行壓縮處理
–exclude=PATTERN 指定排除不需要傳輸的文件模式
–include=PATTERN 指定不排除而需要傳輸的文件模式
–exclude-from=FILE 排除FILE中指定模式的文件
–include-from=FILE 不排除FILE指定模式匹配的文件
–version 打印版本信息
–address 綁定到特定的地址
–config=FILE 指定其他的配置文件,不使用默認的rsyncd.conf文件
–port=PORT 指定其他的rsync服務端口
–blocking-io 對遠程shell使用阻塞IO
-stats 給出某些文件的傳輸狀態
–progress 在傳輸時現實傳輸過程
–log-format=formAT 指定日志文件格式
–password-file=FILE??從FILE中得到密碼
–bwlimit=KBPS 限制I/O帶寬,KBytes per second
-h, –help 顯示幫助信息
四、Rsync實例測試
(一)測試環境:
兩臺主機H1和H2:
H1
系統 CentOS 5.5
主機名 promise
IP地址 172.16.10.189
要備份的目錄 /home/promise/tarbak/
備份的路徑 /home/www/
H2
系統 CentOS 5.5 x86_64
主機名 qqing
IP地址 172.16.10.3
要備份的目錄 /home/bak/
備份的路徑 /home/bakup
(二)測試目的:
分別采用SSH方式和后臺服務方式同步文件,測試單向、雙向文件同步功能,當文件夾或文件有更新(包括增加、刪除、移動、新建等)時手動或者自動檢測更新備份文件,并保持權限的一致性。
(三)測試方式及測試結果:
1、SSH方式(將H1中/home/promise/tarbak/的文件備份至H2的/home/bakup/)
1)在源主機(H1)上啟動ssh服務:
[root@bogon ~]# /etc/init.d/sshd start
啟動 sshd:? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?[確定]
2)在備份服務器(H2)上使用rsync備份(SSH方式是通過系統用戶來進行備份的):
[root@bogon bakup]# rsync -avz –progress -e ssh –delete?promise@172.16.10.189:/home/promise/tarbak/?/home/bakup
出現如下信息:
Address 172.16.10.189 maps to bogon, but this does not map back to the address – POSSIBLE BREAK-IN ATTEMPT!
promise@172.16.10.189′s?password:
3)測試結果:
receiving incremental file list
./
Document.tar.gz
? ???3911680 100%? ?11.44MB/s? ? 0:00:00 (xfer#1, to-check=8/10)
Document2.tar.gz
? ???4628480 100%? ? 6.13MB/s? ? 0:00:00 (xfer#2, to-check=7/10)
Document3.tar.gz
? ???4628480 100%? ? 3.96MB/s? ? 0:00:01 (xfer#3, to-check=6/10)
Images.tar.gz
? ?? ?532480 100%? ? 3.19MB/s? ? 0:00:00 (xfer#4, to-check=5/10)
QuickSync.zip
? ?? ?264979 100%? ? 1.39MB/s? ? 0:00:00 (xfer#5, to-check=4/10)
discuz.png
? ?? ?502105 100%? ? 2.12MB/s? ? 0:00:00 (xfer#6, to-check=3/10)
inotify-tools-3.14.tar.gz
? ?? ?358772 100%? ? 1.34MB/s? ? 0:00:00 (xfer#7, to-check=2/10)
rsync-3.0.8.tar.gz
? ?? ?790722 100%? ? 2.33MB/s? ? 0:00:00 (xfer#8, to-check=1/10)
src.tar.gz
? ???4854407 100%? ? 6.29MB/s? ? 0:00:00 (xfer#9, to-check=0/10)
sent 185 bytes??received 20479655 bytes??718590.88 bytes/sec
total size is 20472105??speedup is 1.00
上面的信息描述了整個的備份過程,以及總共備份數據的大小。
2、后臺服務方式(將H1中/home/promise/tarbak/的文件備份至H2的/home/bakup/)
1)啟動rsync服務,然后編輯/etc/xinetd.d/rsync文件,將其中的disable=yes改為disable=no,并重啟xinetd服務,如下:
[root@bogon ~]# vim /etc/xinetd.d/rsync
# vi /etc/xinetd.d/rsync
#default: off
# description: The rsync server is a good addition to an ftp server, as it \
#? ?? ? allows crc checksumming etc.
service rsync
{
? ?? ???disable = no
? ?? ???socket_type? ???= stream
? ?? ???wait? ?? ?? ?? ?= no
? ?? ???user? ?? ?? ?? ?= root
? ?? ???server? ?? ?? ? = /usr/bin/rsync
? ?? ???server_args? ???= –daemon
? ?? ???log_on_failure??+= USERID
}
[root@bogon ~]# /etc/init.d/xinetd restart
停止 xinetd:? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? [確定]
啟動 xinetd:? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? [確定]
2)創建配置文件,在安裝好rsync程序后,并不會自動創建rsync的主配置文件,需要手工來創建,其主配置文件為rsyncd.conf
[root@bogon ~]# mkdir /etc/rsyncd? ???
//創建rsync的配置目錄
[root@bogon ~]#
touch /etc/rsyncd/rsyncd.conf? ?
//創建rsync的主配置文件
[root@bogon ~]# touch /etc/rsyncd/rsyncd.secrets??
//創建rsync的密碼文件
[root@bogon ~]#
chmod 600 /etc/rsyncd/rsyncd.secrets??
//設置創建的密碼文件權限為600
[root@bogon ~]# touch /etc/rsyncd/rsyncd.motd? ?
//創建客戶連接服務器時顯示給客戶的消息
rsyncd.conf內容如下:
[root@bogon ~]# vim /etc/rsyncd/rsyncd.conf
pid file = /var/run/rsyncd.pid??//運行rsync時進程ID文件
port = 873? ? //默認端口為873
address = 172.16.10.189? ?//把源主機(H1)的IP寫進配置文件
uid = root? ? //傳輸文件時守護進程應該具有的uid
gid = root? ? //傳輸文件時守護進程應該具有的gid
use chroot = no? ? //不使用chroot功能
read only = no? ? //禁止只讀功能
hosts allow = 172.16.10.1/255.255.255.0 //指定哪個網段允許連接
hosts deny = *? ? //除了指定的IP或網段以外禁止連接
max connections = 5? ?//設置最大并發連接數以保護服務器
motd file = /etc/rsyncd/rsyncd.motd //指定消息文件路徑
log file = /var/log/rsync.log??//指定日志文件路徑
log format = %t %a %m %f %b??//使用transfer logging時定制日志文件的字段
transfer logging = yes? ?//使用transfer logging功能
syslog facility = local3? ?//指定發送日志消息給syslog時的消息級別
timeout = 300? ? //設置覆蓋客戶指定的IP超時時間
[bakup]? ???//認證的模塊名,在備份服務端需要指定
path = /home/bakup? ?//指定備份目錄路徑
list = yes? ? //允許列出客戶請求時可以使用的模塊列表
ignore errors? ? //指定rsyncd忽略在判斷運行傳輸時的刪除操作時源主面上的IO錯誤
auth users = promise? ?//設置允許連接的用戶
secrets file = /etc/rsyncd/rsyncd.secrets //指定密碼文件路徑
comment = bakup web? ?//描述該模塊
rsyncd.secrets內容如下:
[root@bogon ~]# vim /etc/rsyncd/rsyncd.secrets
qqing:qqing99
rsyncd.motd內容如下:
[root@bogon ~]# vim /etc/rsyncd/rsyncd.motd
Welcome to linuxrsync system!
3)以—daemon方式啟動rsync服務
[root@bogon ~]# rsync –daemon –config=/etc/rsyncd/rsyncd.conf
4)完成以上工作,現在對數據進行備份:
[root@bogon bakup]# rsync -avz –progress –delete?qqing@172.16.10.189::bakup?/home/bakup/
出現如下信息:
Welcome to linuxrsync system!
Password:
5)測試結果:
receiving incremental file list
./
Document.tar.gz
? ???3911680 100%? ?11.51MB/s? ? 0:00:00 (xfer#1, to-check=8/10)
Document2.tar.gz
? ???4628480 100%? ? 6.14MB/s? ? 0:00:00 (xfer#2, to-check=7/10)
Document3.tar.gz
? ???4628480 100%? ? 3.97MB/s? ? 0:00:01 (xfer#3, to-check=6/10)
Images.tar.gz
? ?? ?532480 100%? ? 3.21MB/s? ? 0:00:00 (xfer#4, to-check=5/10)
QuickSync.zip
? ?? ?264979 100%? ? 1.40MB/s? ? 0:00:00 (xfer#5, to-check=4/10)
discuz.png
? ?? ?502105 100%? ? 2.15MB/s? ? 0:00:00 (xfer#6, to-check=3/10)
inotify-tools-3.14.tar.gz
? ?? ?358772 100%? ? 1.35MB/s? ? 0:00:00 (xfer#7, to-check=2/10)
rsync-3.0.8.tar.gz
? ?? ?790722 100%? ? 2.35MB/s? ? 0:00:00 (xfer#8, to-check=1/10)
src.tar.gz
? ???4854407 100%? ? 6.30MB/s? ? 0:00:00 (xfer#9, to-check=0/10)
sent 230 bytes??received 20479749 bytes??4551106.44 bytes/sec
total size is 20472105??speedup is 1.00
上面的信息描述了整個的備份過程,以及總共備份數據的大小。
6)恢復備份的內容,當服務器的數據出現問題時,那么這時就需要通過客戶端的數據對服務端進行恢復,但前提是服務端允許客戶端有寫入權限,否則也不能在客戶端直接對服務端進行恢復,使用rsync對數據進行恢復的方法如下:
[root@bogon bakup]# rsync -avz –progress /home/bakup/?qqing@172.16.10.189::bakup
出現如下信息:
Welcome to linuxrsync system!
Password:
輸入密碼,按Enter后看到整個恢復過程:
sending incremental file list
./
Document.tar.gz
? ???3911680 100%? ?30.07MB/s? ? 0:00:00 (xfer#1, to-check=8/10)
Document2.tar.gz
? ???4628480 100%? ?13.84MB/s? ? 0:00:00 (xfer#2, to-check=7/10)
Document3.tar.gz
? ???4628480 100%? ? 9.33MB/s? ? 0:00:00 (xfer#3, to-check=6/10)
Images.tar.gz
? ?? ?532480 100%? ? 1.01MB/s? ? 0:00:00 (xfer#4, to-check=5/10)
QuickSync.zip
? ?? ?264979 100%??507.39kB/s? ? 0:00:00 (xfer#5, to-check=4/10)
discuz.png
? ?? ?502105 100%??928.67kB/s? ? 0:00:00 (xfer#6, to-check=3/10)
inotify-tools-3.14.tar.gz
? ?? ?358772 100%??650.02kB/s? ? 0:00:00 (xfer#7, to-check=2/10)
rsync-3.0.8.tar.gz
? ?? ?790722 100%? ? 1.31MB/s? ? 0:00:00 (xfer#8, to-check=1/10)
src.tar.gz
? ???4854407 100%? ? 6.15MB/s? ? 0:00:00 (xfer#9, to-check=0/10)
sent 20479633 bytes??received 182 bytes??561090.82 bytes/sec
total size is 20472105??speedup is 1.00
7)創建用戶密碼驗證文件,免去輸入密碼的麻煩
[root@bogon bakup]# vim /etc/rsyncd.pass
#密碼驗證文件
qqing99
保存退出
同樣要設置密碼驗證文件的權限為600
[root@bogon bakup]# chmod 600 /etc/rsyncd.pass
進行備份,無需輸入密碼驗證:
[root@bogon bakup]# rsync -avz –progress –delete –password-file=/etc/rsyncd.pass?qqing@172.16.10.189::bakup/home/bakup/
出現如下信息:
Welcome to linuxrsync system!
receiving incremental file list
./
Document.tar.gz
? ???3911680 100%? ?11.44MB/s? ? 0:00:00 (xfer#1, to-check=8/10)
Document2.tar.gz
? ???4628480 100%? ? 6.13MB/s? ? 0:00:00 (xfer#2, to-check=7/10)
Document3.tar.gz
? ???4628480 100%? ? 3.97MB/s? ? 0:00:01 (xfer#3, to-check=6/10)
Images.tar.gz
? ?? ?532480 100%? ? 3.19MB/s? ? 0:00:00 (xfer#4, to-check=5/10)
QuickSync.zip
? ?? ?264979 100%? ? 1.39MB/s? ? 0:00:00 (xfer#5, to-check=4/10)
discuz.png
? ?? ?502105 100%? ? 2.14MB/s? ? 0:00:00 (xfer#6, to-check=3/10)
inotify-tools-3.14.tar.gz
? ?? ?358772 100%? ? 1.34MB/s? ? 0:00:00 (xfer#7, to-check=2/10)
rsync-3.0.8.tar.gz
? ?? ?790722 100%? ? 2.34MB/s? ? 0:00:00 (xfer#8, to-check=1/10)
src.tar.gz
? ???4854407 100%? ? 6.29MB/s? ? 0:00:00 (xfer#9, to-check=0/10)
sent 230 bytes??received 20479749 bytes??1638398.32 bytes/sec
total size is 20472105??speedup is 1.00
可見不再提示輸入密碼
3、使用crontab建立腳本,實現定期同步備份文件
[root@bogon bakup]# crontab -e
設置每隔5分鐘自動同步備份:
*/5 * * * * rsync -avz –progress –delete –password-file=/etc/rsyncd.pass?qqing@172.16.10.189::bakup?/home/bakup/
4、與最佳搭檔inotify的合作,實現實時觸控式自動更新
Inotify的使用
查看是否支持inotify,從kernel 2.6.13開始正式并入內核,RHEL5已經支持。看看是否有 /proc/sys/fs/inotify/目錄,以確定內核是否支持inotify
[root@bogon Rsync]# ll /proc/sys/fs/inotify
total 0
-rw-r–r– 1 root root 0 Oct??9 09:36 max_queued_events
-rw-r–r– 1 root root 0 Oct??9 09:36 max_user_instances
-rw-r–r– 1 root root 0 Oct??9 09:36 max_user_watches
1)獲取Inotify:
[root@bogon]# wget?http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
2)Inotify的安裝:
[root@bogon]# cd inotify-tools-3.14
[root@bogon]# ./configure
[root@bogon]# make
[root@bogon]# make install
3)創建腳本文本:
[root@bogon]# cd /usr/local/sbin
[root@bogon]# touch rsync.sh
[root@bogon]# chmod a+x rsync.sh
[root@bogon]# vim rsync.sh
rsync.sh內容如下:
#!/bin/sh
src=/home/promise/bakup/??//需要備份的目錄
des=bakup? ? //配置文件里的模塊名
ip=172.16.10.3? ? //備份服務器的IP
user=promise
/usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format??‘%T %w%f’ -e modify,delete,create,attrib ${src} | while read file
? ?? ???do
? ?? ?? ?? ?? ???rsync -avz –delete –progress –password-file=/etc/rsyncd.pass ${src} [url=mailto%7Buser%7D@$%7Bip%7D:%7Bdes]${user}@${ip}:{des[/url]} &&
? ?? ?? ?? ?? ???echo “${src} was rsynced”
? ?? ?? ?? ?? ???echo “—————————————————–”
? ?? ???done
注:–password-file存放用戶${user}的密碼
password-file的添加如下:密碼與上文中的用戶名對應于rsync.secrets文件中的用戶名和密碼。
[root@bogon bakup]# vim /etc/rsyncd.pass
[root@bogon bakup]# chmod 600 rsyncd.pass
[root@bogon bakup]# vim rsyncd.pass
#添加密碼
qqing99
4)執行:
rsyncd.sh
PS:將rsync.sh添加到開機啟動:
sed -i ‘$i \/usr/local/sbin/rsync.sh’ /etc/rc.local
5、使用inotify+rsync實現雙向同步更新文件
網上有很多資料說rsync不能雙向同步,其實,在兩臺主機上同時安裝rsync和inotify,并按以上基礎配置就可以實現雙向同步更新備份文件。
(四)測試結果數據分析
1、測試目的
分析rsync在同步時加參數-z進行壓縮時的壓縮比例
2、測試數據來源
1)源主機(H1)上的sof.tar壓縮文件包
詳細資料:
[root@bogon www]# ls -al sof.tar
-rw-r–r– 1 root root 498739200 04-19 14:24 sof.tar
[root@bogon www]# ls -hl sof.tar
-rw-r–r– 1 root root 476M 04-19 14:24 sof.tar
2)通過實時觸控式(inotify+rsync)同步更新,將源主機(H1)上的sof.tar同步備份到備份服務器(H2)上
3、測試結果數據(三次測試結果)
第一次測試結果:
不加參數-z(即不進行壓縮同步)
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
4 files to consider
./
sof.tar
? ?498739200 100%? ? 7.87MB/s? ? 0:01:00 (xfer#1, to-check=2/4)
deleting html/
sent 498800218 bytes??received 30 bytes??6786397.93 bytes/sec
total size is 28907509760??speedup is 57.95
/home/www/ was rsyncd.
——————————————————–
加參數-z(即進行壓縮同步)
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
4 files to consider
./
sof.tar
? ?498739200 100%? ? 6.93MB/s? ? 0:01:08 (xfer#1, to-check=2/4)
deleting html/
sent 492633703 bytes??received 30 bytes??6119673.70 bytes/sec
total size is 28907509760??speedup is 58.68
/home/www/ was rsyncd.
——————————————————–
第二次測試結果:
不加參數-z
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
5 files to consider
./
sof.tar
? ?498739200 100%? ? 7.26MB/s? ? 0:01:05 (xfer#1, to-check=3/5)
htl/
sent 498800233 bytes??received 33 bytes??6274217.18 bytes/sec
total size is 28907509760??speedup is 57.95
/home/www/ was rsyncd.
——————————————————–
加參數-z
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
4 files to consider
./
sof.tar
? ?498739200 100%? ? 5.80MB/s? ? 0:01:21 (xfer#1, to-check=2/4)
deleting htl/
sent 492633703 bytes??received 30 bytes??5213055.38 bytes/sec
total size is 28907509760??speedup is 58.68
/home/www/ was rsyncd.
第三次測試結果:
不加參數-z
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
4 files to consider
./
sof.tar
? ?498739200 100%? ? 6.96MB/s? ? 0:01:08 (xfer#1, to-check=2/4)
deleting html/
sent 498800218 bytes??received 30 bytes??6196276.37 bytes/sec
total size is 28907509760??speedup is 57.95
/home/www/ was rsyncd.
——————————————————–
加參數-z
[root@bogon sbin]# rsync.sh
Welcome to linuxrsync system!
building file list …
5 files to consider
./
sof.tar
? ?498739200 100%? ? 6.95MB/s? ? 0:01:08 (xfer#1, to-check=3/5)
html/
sent 492633723 bytes??received 33 bytes??6044585.96 bytes/sec
total size is 28907509760??speedup is 58.68
/home/www/ was rsyncd.
——————————————————–
4、測試結果數據分析
由以上測試結果數據可得:
1)不加參數-z,共發送的數據大小,取三次平均值:
S1=(498800218+498800233+498800218)/3=498800223
2)加參數-z,共發送的數據大小,取三次平均值:
S2=(492633703+492633703+492633723)/3=492633710
3)sof.tar壓縮包實際大小:
T= 498739200
[root@bogon www]# ls -al sof.tar
-rw-r–r– 1 root root 498739200 04-19 14:24 sof.tar
4)壓縮比例:
P=(S1-S2)/T
??=(498800223-492633710)/498739200
??=6166513/498739200
??=0.012364204
??=1.2364204%
五、問題排除及注意事項
問題排除:
1、問題1
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(614) [receiver=2.6.8]
解決:在服務器上查看日志,看到有這么一行:
rsync: unable to open configuration file “/etc/rsyncd.conf”: No such file or directory
于是執行如下命令:
[root@bogon ~]# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
但是繼續報如下錯誤:
sending incremental file list
ERROR: module is read only
rsync error: syntax or usage error (code 1) at main.c(879) [Receiver=3.0.8]
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(760) [sender=3.0.8]
到備份服務器上修改主配置文件
[root@bogon ~]# vim /etc/rsyncd/rsyncd.conf
把
read
改為
read
問題解決,實現單向同步
2、問題2
[root@www backup]# rsync -vzrtopg –progress –delete?promise@172.16.10.189::promise?/home/bak/
Password:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.8]
解決:目錄設置錯誤
確定配置文件里的目錄正確
3、問題3
在Linux下使用rsync同步時,出現以下錯誤:
@ERROR: auth failed on module bakup
其中,bakup是rsync服務模塊名稱。
出現這種情況,先檢查了用戶名和密碼正確性,經檢查無誤,排除了這個可能性,另外可能的原因是:rsync服務器的帳戶密碼文件的權限沒有設置為600,修改/etc/rsyncd/rsyncd.secrets文件:
[root@bogon ~]#
chmod 600 /etc/rsyncd/rsyncd.secrets
問題解決。
4、問題4
在備份路徑后面一定要加“/”,否則在雙向同步時會一直循環同步。如:
src=/var/www/??//后面一定要加“/”
5、問題5
同步mysql數據時需要注意:
本地源主機(H1)是一臺web服務器,把/var/lib/mysql備份到備份服務器(H2),當web服務器上的數據受損時需要從備份服務器把數據同步到web服務器
執行腳本文件:/usr/local/sbin/rsync.sh
同步備份服務器上的/var/lib/mysql到web服務器后仍然無法訪問網站,此時需要重啟mysql
#/etc/init.d/mysqld restart
問題解決。
網上相關資料:
資料一:
最近在配置rsync服務器鏡像備份。中間遇到@ERROR: auth failed on module??這個問題,網上找了好多資料,也具體對照測試了。
? ? 1、
? ? [root@bo bin]# /usr/local/rsync/bin/rsync -vazu -progress –delete?backup@192.168.1.238::www?/tmp –password-file=/etc/backserver.pas
? ? rsync: Failed to exec ss: No such file or directory (2)
? ? rsync error: error in IPC code (code 14) at pipe.c(86) [receiver=2.6.9]
? ? rsync: writefd_unbuffered failed to write 12 bytes [receiver]: Broken pipe (32)
? ? rsync error: error in rsync protocol data stream (code 12) at io.c(1122) [receiver=2.6.9]
? ? [root@ht-store bin]#
? ? 以上錯誤可能是上面使用的參數前面的符號不正確。換成下面的就沒有上面的錯誤了
? ? [root@bo /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pasbackup@192.168.1.238:www?/tmp
? ? 2、
? ? [root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete?backup@192.168.1.238:www?/tmp/ –password-file=/etc/backserver.pas
? ??backup@192.168.1.238′s?password:
? ? Permission denied, please try again.
? ??backup@192.168.1.238′s?password:
? ? Permission denied, please try again.
? ??backup@192.168.1.238′s?password:
? ? Permission denied (publickey,gssapi-with-mic,password).
? ? rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
? ? rsync error: unexplained error (code 255) at io.c(453) [receiver=2.6.9]
? ? [root@ht-store /]#
? ? 更改成
? ? [root@ht-store /]# usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pas
backup@192.168.1.238::www?/tmp
? ? @ERROR: auth failed on module www
? ? rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
? ? [root@ht-store /]#
? ? 3、
? ? [root@ht-store /]# /usr/local/rsync/bin/rsync -vazu –progress –delete –password-file=/etc/backserver.pas
? ??backserver@192.168.1.238::www?/tmp
? ? @ERROR: auth failed on module www
? ? rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
? ? 檢查一下用戶名是否錯誤backserver 應該是backup
? ? 4、
? ? [root@ht-store /]# /usr/local/rsync/bin/rsync -vzar –progress –delete –password-file=/etc/backserver.pasbackup@192.168.1.238:www?/tmp
? ??backup@192.168.1.238′s?password:
? ? Permission denied, please try again.
? ??backup@192.168.1.238′s?password:
? ? rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [receiver=2.6.9]
? ? 是雙引號“::”backup@192.168.1.238::www??而不是單引號“:”backup@192.168.1.238:www
? ? 5、
? ? [root@ht-store /]# /usr/local/rsync/bin/rsync -vzrtopgu –progress –delete –password-file=/etc/backserver.pasbackup@192.168.1.238::www?/tmp
? ? @ERROR: Unknown module ‘www’
? ? rsync error: error starting client-server protocol (code 5) at main.c(1383) [receiver=2.6.9]
? ? 這是因為服務器端hosts allow 沒有允許該主機訪問
資料二:
1. Q: 出現以下這個訊息, 是怎麼一回事?
? ? @ERROR: auth failed on module xxxxx
? ? rsync: connection unexpectedly closed (90 bytes read so far)
? ? rsync error: error in rsync protocol data stream (code 12) at io.c(150)
? ? A: 這是因為密碼設錯了, 無法登入成功, 請再檢查一下 rsyncd.pwd ,rsyncd.sec中的密碼設定, 二端是否一致?有時服務器
? ? 端沒有起動服務也會出現這種情況
? ? 2. Q: 出現以下這個訊息, 是怎麼一回事?
? ? password file must not be other-accessible
? ? continuing without password file
? ? Password:
? ? A: 這表示 rsyncd.pwd rsyncd.sec 的檔案權限屬性不對, 應設為 600
? ? 請下 chmod 600 rsyncd.pwd rsyncd.sec
? ? 3. Q: 出現以下這個訊息, 是怎麼一回事?
? ? @ERROR: chroot failed
? ? rsync: connection unexpectedly closed (75 bytes read so far)
? ? rsync error: error in rsync protocol data stream (code 12) at io.c(150)
? ? A: 這通常是您的 rsyncd.conf 中
? ? 的 path 路徑所設的那個目錄並不存在所致.
? ? 請先用 mkdir 開設好備份目錄.
? ? 4.Q:出現一下信息是怎么回事
? ? rsync: failed to connect to 218.107.243.2: No route to host (113)
? ? rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
? ? 所以情況會是:
? ? 1. 對方主機沒開機
? ? 2. 對方有 firewall 阻擋
? ? 3. 所通過的網路上有 firewall 阻擋
? ? shell>setup? ?#關閉防火墻,其實最重要的也就是把tcp udp 的873端口打開。
? ? 5.Q:出現一下信息是怎么回事
? ? 當
? ? /server/rsync/bin/rsync -vzrutogp –progress –password-file=/etc/rsyncclien
? ? t.pwd?backup@60.173.7.35:backup?/var/www/backup
? ? 出現
? ??backup@60.173.7.6′s?password
? ? Permission denied, please try again.
最有可能的情況是,系統沒有認出來,你現在的通信是rsync,所以請仔細檢查一下上面加紅的那句話中ip地址后面是否為兩個:也就是說::為rsync通信,:為ssh通信
? ? 6.Q:
? ? 當/server/rsync/bin/rsync rsync://backup@61.156.238.119/backup時,出現如下
? ? rsync: link_stat “/.” (in backup) failed: Permission denied (13)
? ? rsync error: some files could not be transferred (code 23) at main.c(1385) [receiver=2.6.9]
? ? 解決方法:更改你的rsyncd.conf中的
? ? uid=nobody
? ? gid=nobody
? ? 為
? ? uid=root
? ? gid=root
解決方法2
? ? 出現這樣問題的原因是我要同步的這個root目錄在掩位的最后一位
? ? 不是r-x或以上的權限,也就是說root這個目錄一定要等于或大于drwxr-xr-x才能同步。
轉載于:https://blog.51cto.com/522480647/722148
總結
以上是生活随笔為你收集整理的Rsync的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: H3C交换机S5500系列恢复控制台登录
- 下一篇: DIV+CSS布局参考站点