rsync+inotify實現多臺web數據動態同步

ps: 最新的可以從http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz下載

#wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz命令就可以咯


背景:由於無存儲共享設備,web集羣中的代碼均存放在本地,最終導致web節點之間的數據無法一致。
解決辦法:
採用rsync+inotify,實現多臺web數據動態同步

解決思路:比如有a、b、c、d四臺web,爲解決哪臺服務器爲源數據服務器,我們在a服務器上安裝rsync+inotify,然後將一個二級域名指向a服務器,這樣以後網站編輯、開發人員之間訪問二級域名進行日常網站更新,a服務器在檢測到本地有數據更新時,便動態(觸發式)向其它服務器發送更新數據

選擇rsync+inotify的理由:在常規的數據同步應用案例中,大多數人會選擇使用rsync來完成數據同步,筆者選擇rsync+inotify的理由如下
1、服務器性能:rsync只能實現定時更新,無論網站有無文件更新,rsync都會按着定時任務去檢查文件是否有更新,當數據文件較大時會使服務器性能下降;而rsync+inotify爲觸發式更新,也就是說只有當某個文件發生改動時纔會更新,這樣一來對服務器性能影響較小
2、數據實時性:如果選擇rsync,每隔多長時間同步一次數據是個問題,時間越短,對性能影響就越大。時間太長,用戶/編輯無法接受。採用rsync+inotify可實現實時更新,當a服務器文件有更新時,其它服務器立即更新


環境拓撲

a:192.168.1.101
b:192.168.1.102
c:192.168.1.103
d:192.168.1.104
注:數據源服務器爲a,目標服務器爲b、c、d


一、目標服務器安裝rsync (在b、c、d服務器上操作,安裝配置均一樣)

安裝rsync
#tar zxvf rsync-3.0.8.tar.gz
#cd rsync-3.0.8
#./configure && make && make install


配置rsync

#vi /etc/rsync.conf    加入如下內容

uid = root
gid = root
use chroot = no
max connections = 20
strict modes = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log format = %t %a %m %f %b
[web]
path = /usr/local/nginx/html/
auth users = rsync
read only = no
hosts allow = 192.168.1.0/24
list = no
uid = root
gid = root
secrets file = /etc/rsync.passwd
ignore errors = yes

創建認證

#vi /etc/rsync.passwd
rsync:rsync

#chmod 600 /etc/rsync.passwd

啓動rsync,啓動後使用netstat查看,會發現系統已啓動873端口
# rsync –daemon –config=/etc/rsync.conf  

加入開機啓動
# echo "rsync –daemon –config=/etc/rsync.conf" >>/etc/rc.local

二、源服務器安裝rsync+inotify (在a服務器上操作)
安裝rsync(僅安裝即可,不需配置)

#tar zxvf rsync-3.0.8.tar.gz
#cd rsync-3.0.8
#./configure && make && make install
#echo "rsync" > /etc/rsync-client.passwd
#chmod 600 /etc/rsync-client.passwd

安裝inotify
#tar zxvf inotify-tools-3.13.tar.gz
#cd inotify-tools-3.13
#./configure && make && make install

#vi /etc/rsync-web.sh   加入如下內容
#!/bin/sh
SRC=/usr/local/nginx/html/
DES=web
WEB2=192.168.1.102
WEB3=192.168.1.103
WEB4=192.168.1.104
USER=rsync
/usr/local/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F
        do
rsync -ahqzt –password-file=/etc/rsync-client.passwd  –delete $SRC $USER@$WEB2::$DES
rsync -ahqzt –password-file=/etc/rsync-client.passwd  –delete $SRC $USER@$WEB3::$DES
rsync -ahqzt –password-file=/etc/rsync-client.passwd  –delete $SRC $USER@$WEB4::$DES
        done

#chmod +x /etc/rsync-web.sh
#nohup /etc/rsync-web.sh &       //必須使用nohup放入後臺執行,否則關閉終端後此腳本進程會自動結束


三、測試

在a服務器/usr/local/nginx/html目錄下進行增、添、改、刪文件,看b、c、d服務器是否能得到同步

參考文獻:
http://bbs.linuxtone.org/thread-2681-1-1.html


來源:http://blog.luwenju.com/160.html


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