rsync+intotify實現數據同步

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


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


# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44319 4月   6 18:51 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41409 4月   6 18:51 /usr/local/bin/inotifywatch
inotifywait用於等待文件或文件集上的一個特定事件,它可以監控任何文件和目錄設置,並且可以遞歸地監控整個目錄樹
inotifywatch用於收集被監控的文件系統統計數據,包括每個inotify事件發生多少次等信息

  
  




三、	企業案例WEB內容版本發佈管理
3.1、環境描述
隨着企業的業務增多,爲了維持所有WEB層的WEB程序保持一致。發版本的通過一臺內容服務器向外進行分佈和輻射所有的更新的WEB程序,模擬環境如下:
WEB1 節點 IP:10.0.0.202
WEB2節點內容發佈Server IP:10.0.0.201

3.2、安裝WEB1節點
# yum –y install rsync
# vim /etc/web1.pass
gongda:123
# chmod 600 /etc/web1.pass

#: yum –y install rsync
# mkdir /web1/wwwroot –p
# vim /etc/rsyncd.conf   #此配置文件沒有。需要重新編輯一個
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /web1/wwwroot/
comment = web1 file
ignore errors
read only = no
write only = no
hosts allow = 10.0.0.201
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass

啓動rsync服務
# rsync --daemon




3.2、安裝WEB2內容管理節點
# yum –y install rsync


在WEB內容發佈服務器安裝inotify.  
(1)、inotify安裝
# tar –xvf inotify-tools-3.14.tar.gz
# ./configure
# make && make install

# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44319 4月   6 18:51 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41409 4月   6 18:51 /usr/local/bin/inotifywatch
inotifywait用於等待文件或文件集上的一個特定事件,它可以監控任何文件和目錄設置,並且可以遞歸地監控整個目錄樹
inotifywatch用於收集被監控的文件系統統計數據,包括每個inotify事件發生多少次等信息


(2)同步的密碼,注意此處只需要寫密碼,和web1.pass裏面的密碼要以致
# vim /etc/server.pass
123
#chmod 600 /etc/server.pass

(3)編寫一個啓動腳本
# vim opt/inotify-rsync.sh 
#!/bin/bash
host1=10.0.0.202
#======================
src=/web/wwwroot/
dst1=web1
user1=gongda
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src \
| while read files
        do
  /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1
                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
         done

(4)啓動腳本
#cd /opt
#chmod +x inotify-rsync.sh
# nohup bash –x inotify-rsync.sh &


3.2、常見錯誤
問題一:
@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。掠過軟連接文件。


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