關於rsync+inotify-tools實時同步模式

介紹同步模式

  • 左邊是原來的,一般的rsync的cs架構(client & server)同步模式,數據源服務器上安裝rsync server,由server統一控制可以傳輸的數據的內容,例如權限,目錄,文件數等,發起傳輸的是rsync client,即看起來就是將數據從源服務器拉取到備份服務器。

  • 右邊的是加上inotify-tools的同步模式,在數據源服務器上安裝rsync client,在備份源服務器上安裝rsync server,也是由server統一控制傳輸的數據內容,但是這裏是數據源服務器作爲了client端,因爲發起傳輸的是rsync client,所以這裏看起來就是將數據從源服務器推送到備份服務器。

  • 從邏輯上cs的架構 c 和 s 變成了相反位置,但是傳輸的模式依然是從 s 傳輸到 c 。

  • 數據源服務器ip暫定爲10.111.111.112 ,數據備份服務器ip暫定爲10.111.111.111。

  • 我需要做同步的目錄是/app/www/uploads,源服務器和備份服務器都存在,並且路徑相同,權限也一致(權限一致是非常重要的,因爲同步數據涉及到權限,而因爲我不是使用root進行同步,所以要確保傳輸雙方的權限是可寫或者可讀才行)

  • 操作系統是centos 6.5 x64

blog_rsync_(bs)_introduce_20150206.jpg


開始配置

在數據源服務器上安裝rsync client 和inotify-tools

yum -y install rsync inotify-tools

Loaded plugins: security
Setting up Install Process
Resolving Dependencies--> Running transaction check---> Package inotify-tools.i686 0:3.14-1.el6 will be installed---> Package rsync.i686 0:3.0.6-9.el6_4.1 will be updated---> Package rsync.i686 0:3.0.6-12.el6 will be an update--> Finished Dependency ResolutionDependencies Resolved

=============================================================================================================================================================================================================================================
 Package   Arch Version   RepositorySize
=============================================================================================================================================================================================================================================
Installing:
 inotify-tools i686 3.14-1.el6epel  45 k
Updating:
 rsync i686 3.0.6-12.el6  base 329 kTransaction Summary
=============================================================================================================================================================================================================================================Install   1 Package(s)Upgrade   1 Package(s)

Total download size: 375 k
Downloading Packages:
(1/2): inotify-tools-3.14-1.el6.i686.rpm  |  45 kB 00:00 (2/2): rsync-3.0.6-12.el6.i686.rpm| 329 kB 00:00 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total509 kB/s | 375 kB 00:00 Running rpm_check_debug
Running Transaction TestTransaction Test Succeeded
Running Transaction
  Updating   : rsync-3.0.6-12.el6.i686   1/3 
  Installing : inotify-tools-3.14-1.el6.i686 2/3 
  Cleanup: rsync-3.0.6-9.el6_4.1.i6863/3 
  Verifying  : inotify-tools-3.14-1.el6.i686 1/3 
  Verifying  : rsync-3.0.6-12.el6.i686   2/3 
  Verifying  : rsync-3.0.6-9.el6_4.1.i6863/3 Installed:
  inotify-tools.i686 0:3.14-1.el6

Updated:
  rsync.i686 0:3.0.6-12.el6  

Complete!

rsync client和server是同一個軟件,而inotify是linux 內核2.6以上纔有的特性。

創建rsync client 和inotify 合體腳本

mkdir /app/rsync

vi /app/rsync/rsync_do.sh

#!/bin/bashsrc='/app/www/Uploads/'passwordfile='/app/rsync/rsync.passwd'user='www'host='10.111.111.111'rsync_module='uploads'/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib ${src} | while read DATE TIME DIR filedo/usr/bin/rsync -vzrtopg --delete --progress ${src} ${user}@${host}::${rsync_module} --password-file=${passwordfile}echo "${file} was rsynced at ${DATE}_${TIME} in ${DIR}" >> /var/log/rsync.log 2>&1done

1.inotifywait 是inotify的命令工具

2.rsync的src目錄需要注意,源目錄也跟rsync一樣,對於文件夾是區分有/和沒有/的

3.需要注意rsync的同步的模塊名字要匹配

4.inotifywait命令可以監聽操作,輸出信息,並且是一直輸出,所以跟read 搭配起來,將inotifywait的輸出通過read捕獲到變量
強制記錄了同步日誌輸出到/var/log/rsync.log

5.--delete參數需要注意,這個會刪除源目錄沒有的東西,所以在沒有確定和測試好腳本之前可以先暫時不加這個參數

6.腳本中的rsync解析出來就是/usr/bin/rsync -vzrtopg --delete --progress /app/www/uploads/ www@$10.111.111.111::uploads --password-file=/app/rsync/rsync.passwd, 同步的目錄是/app/www/uploads/,使用www這個用戶進行同步,目標服務器是10.111.111.111,rsync的模塊是uploads,這樣就可以將源服務器的/app/www/uploads/下的所有數據(uploads目錄本身)同步到備份服務器的/app/www/uploads/上,有delete參數,保持源和備份一致。

需要創建/app/rsync/rsync.passwd

echo "123456" > /app/rsync/rsync.passwd

這裏只需要寫密碼就好了,因爲賬號已經在命令行指明瞭。

nohup運行作爲監聽服務

nohup /app/rsync/rsync_do.sh &

使用nohup + & 運行是爲了將程序放置在後臺運行,主要是爲了忽略系統發出的hangup信號而導致關閉


在數據備份服務器上安裝rsync服務端

yum install rsync

默認安裝後,在/etc目錄下,並不存在rsyncd目錄,需要手動創建配置文件目錄

mkdir /etc/rsyncd

但是因爲我想將所有配置文件放在特定的目錄,所以我創建了自己的目錄和rsync server的配置文件rsync.conf

mkdir /app/rsync

vi /app/rsync/rsync.conf

uid = root
gid = rootuse chroot = nomax connections = 3timeout = 800pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.locklog file = /var/log/rsyncd.log[uploads]
path = /app/www/Uploadsignore errors = yeshosts allow = 10.111.111.112hosts deny = *read only = nowrite only = nolist = yes
uid = www
gid = www
auth users = www
secrets file = /app/rsync/rsync.passwd

創建secrets file驗證文件,並且設置權限爲600

echo "www:123456" > /app/rsync/rsync.passwd

chmod 600 /app/rsync/rsync.passwd

這個驗證文件需要賬號和密碼,用冒號分隔開,用來給客戶端client驗證使用的。

運行demon模式

rsync --daemon --config=/app/rsync/rsync.conf

daemon 模式可以保持進程一直運行而不被系統強制關閉,可能會有系統信號hangup影響。


科普時間:

1.rsync

與傳統的 cp、tar 備份方式相比,rsync 具有安全性高、備份迅速、支持增量備份等優點,通過 rsync 可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等,隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync 在高端業務系統中也逐漸暴露出了很多不足,首先,rsync 同步數據時,需要掃描所有文件後進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync 不能實時的去監測、同步數據,雖然它可以通過 linux 守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync+inotify 組合出現了!

2.inotify

Inotify 是一種強大的、細粒度的、異步的文件系統事件監控機制,linux 內核從 2.6.13 起,加入了 Inotify支持,通過 Inotify 可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而 inotify-tools 就是這樣的一個第三方軟件。 在上面章節中,我們講到,rsync 可以實現觸發式的文件同步,但是通過 crontab 守護進程方式進行觸發,同步的數據和實際數據會有差異,而 inotify 可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync 同步,這樣剛好解決了同步數據的實時性問題。

3.rsync.conf的配置解釋

uid = root  # rsync server 的進程運行用戶gid = root  # rsync server 的進程運行用戶組use chroot = no #不打開chrootmax connections = 3 #最大rsync連接數timeout = 800   #超時時間pid file = /var/run/rsyncd.pid  #進程號會寫進去lockfile = /var/run/rsyncd.lock     
log file = /var/log/rsyncd.log  #同步日誌[uploads]   #rsync同步模塊名字,在同步的時候會使用模塊名字而不是具體目錄名字,名字可自定義path = /app/www/Uploads #同步目錄的路徑ignore errors = yes #是否忽略錯誤hosts allow = 10.111.111.112    #只允許某ip同步,多個ip用逗號分隔hosts deny = *  #不允許其他ip同步read only = no  #不只讀write only = no #不只寫list = yes  #允許列出模塊裏的內容uid = www   #同步目錄的所屬用戶gid = www   #同步目錄的所屬組auth users = www    #同步驗證的賬號secrets file = /app/rsync/rsync.passwd  #同步驗證的驗證配置文件

4.rsync命令

a) rsync -av --progress <src-dir>/ <dst-dir> *** 注意(/) ***b) rsync -av --progress <src-dir> <dst-dir>c) rsync -avu --progress --delete <src-dir>/ <dst-dir>d) rsync -av --progress --temp-dir=/tmp <src-dir>/ <dst-dir>命令說明---------a) 同步src-dir目錄下所有文件到dst-dir目錄下
b) 同步src-dir目錄下所有文件到dst-dir/src-dir目錄下
c) 對src-dir目錄內容向dst-dir目錄下進行差異更新,有增加/更新則添加替換,有減少則對其刪減
d) 比a)多了--temp-dir=/tmp,即指定/tmp爲臨時交換區,這樣可以避免因目標目錄空間不夠引起的無法同步文件的錯誤。  對應於以上六種命令格式,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 -av [email protected]::www /databack
  5)從本地機器拷貝文件到遠程rsync服務器中。當DST路徑信息包含"::"分隔符時啓動該模式。如:rsync -av /databack [email protected]::www6)列遠程機的文件列表。這類似於rsync傳輸,不過只要在命令中省略掉本地機信息即可。如:rsync -v rsync://172.16.78.192/www

參數

--progress 顯示備份過程--delete 刪除那些DST中SRC沒有的文件-z, --compress 對備份的文件在傳輸時進行壓縮處理-v, ––verbose   詳細輸出模式-r, ––recursive 對子目錄以遞歸模式處理-t, ––times 保持文件時間信息-o, ––owner 保持文件屬主信息-g, ––group 保持文件屬組信息-p 保持權限信息

5.inotifywait

-m, --monitor 監視模式,保持一直監視狀態,不退出,可以保持一直輸出數據
-r, --recursive 目錄遞歸
-e <event>, --event <event>  事件,事件內容可以是創建,修改等等,參看官網
Listen for specific event(s) only. The events which can be listened for are listed in the EVENTS section. This option can be specified more than once. If omitted, all events are listened for.
-q, --quiet 安靜模式,減少不必要的輸出
If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
--timefmt <fmt> 時間格式
Set a time format string as accepted by strftime(3) for use with the '%T' conversion in the --format option.
--format <fmt>  日誌格式
Output in a user-specified format, using printf-like syntax. The event strings output are limited to around 4000 characters and will be truncated to this length. The following conversions are supported:
%w
This will be replaced with the name of the Watched file on which an event occurred.
%f
When an event occurs within a directory, this will be replaced with the name of the File which caused the event to occur. Otherwise, this will be replaced with an empty string.
%e
Replaced with the Event(s) which occurred, comma-separated.
%Xe
Replaced with the Event(s) which occurred, separated by whichever character is in the place of 'X'.
%T
Replaced with the current Time in the format specified by the --timefmt option, which should be a format string suitable for passing to strftime(3).

troubleshooting

問題一:@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端的目錄不存在或無權限。創建目錄並修正權限可解決問題。

問題二:@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端該模塊(tee)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。提供正確的用戶名密碼解決此問題。

問題三:@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題。

問題四:
password file must not be other-accessible
continuing without password file
Password:
原因:
這是因爲rsyncd.pwd rsyncd.secrets的權限不對,應該設置爲600。如:chmod 600 rsyncd.pwd

問題五:
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]
原因:
對方沒開機、防火牆阻擋、通過的網絡上有防火牆阻擋,都有可能。關閉防火牆,其實就是把tcp udp的873端口打開。

問題六:
rsync error: error starting client-server protocol (code 5) at main.c(1524) [Receiver=3.0.7]
原因:
/etc/rsyncd.conf配置文件內容有錯誤。請正確覈對配置文件。

問題七:
rsync: chown "" failed: Invalid argument (22)
原因:
權限無法複製。去掉同步權限的參數即可。(這種情況多見於Linux向Windows的時候)
 問題八:@ERROR: daemon security issue -- contact admin
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
原因:
同步的目錄裏面有軟連接文件,需要服務器端的/etc/rsyncd.conf打開use chroot = yes。掠過軟連接文件。

參考:

1.http://linux.die.net/man/1/inotifywait

2.http://www.ibm.com/developerworks/cn/linux/l-ubuntu-inotify/index.html

3.http://loveyan.blog.51cto.com/829079/713816


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