rsync + inotify 實現數據實時同步

rsync + inotify 實現數據實時同步linux

 
1、系統環境:
CentOS-5.6 -x86_64

更新源服務器:192.168.0.240git

目的服務器:192.168.0.241
  
 
2、更新源服務器配置:192.168.0.240(客戶端)
 
一、安裝inotify-tools
在安裝inotify-tools前請先確認你的linux內核是否打到了2.6.13,而且在編譯時開啓了CONFIG_INOTIFY選項,也能夠經過如下命令檢測 ls /proc/sys/fs/inotify
 
# lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:  CentOS release 5.6 (Final)
Release:        5.6
Codename:   Final
 
# uname -r
2.6.18-238.el5
 
# ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches      若是有此三個文件,代表支持。
 
# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz      下載inotify-tools工具包
# tar xvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14       
# ./configure  
# make
# make install
 
二、編寫rsync監控腳本
 
# vi /root/rsync.sh
 
#!/bin/sh
host1=192.168.0.241
src=/usr/local/resin-pro-4.0.25/webapps
des1=web1
user1=www1
/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 -vzrtopg --delete --progress ${src} ${user1}@${host1}::${des1} --password-file=/etc/www1.pwd &&
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
 
# vi /root/rsync1.sh
#!/bin/sh
host1=192.168.0.241
src=/data/www1
des2=web2
user1=www1
/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 -vzrtopg --delete --progress ${src} ${user1}@${host1}::${des2} --password-file=/etc/www1.pwd &&
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
 
三、創建認證文件 ()
echo "741852" >> /etc/www1.pwd  
chmod 600 /etc/www1.pwd
 
/bin/sh -n /root/rsync.sh  //語法檢查  
chmod +x /root/rsync.sh  
nohup sh /root/rsync.sh &  
echo "nohup sh /root/rsync.sh &" >> /etc/rc.local
 
 
3、目的服務器配置:192.168.0.241 (服務端)
 
一、檢查rsync是否安裝
rpm -qa|grep rsync 
 
二、若是沒有發裝,執如下命令進行安裝
yum -y install rsync 
 
三、定義rsync配置文件/etc/rsyncd.conf
 
# vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 100
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
 
[web1]      設置認證的模塊名
path = /usr/local/resin/          待同步文件的位置
ignore errors
read only = no
list = no
hosts allow = 192.168.0.0/255.255.255.0
auth users = www1     認證用戶名,若是沒有表示匿名
secrets file = /etc/www1.pwd    認證密碼文件放置位置
 
[web2]
path = /data/
ignore errors
read only = no
list = no
hosts allow = 192.168.0.0/255.255.255.0
auth users = www1
secrets file = /etc/www1.pwd
 
四、創建認證文件/etc/www1.pwd ,此文件須與配置文件中指定文件名保持一致
 
此處格式爲:username:password,安全問題,並不建議實際使用中使用root用戶
192.168.0.241:
echo "www1:741852" >> /etc/www1.pwd
而且咱們須要設置此文件的權限爲600
chmod 600 /etc/www1.pwd  
chmod 600 /etc/rsyncd.conf
 
五、創建motd文件(無關緊要)
 
rsyncd.motd記錄了rsync服務的歡迎信息,你能夠在其中輸入任何文本信息,如:
echo "Welcome to use the rsync services!" >> /var/rsyncd.motd 
 
六、啓動rsync
/usr/bin/rsync --daemon   
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
 
 
4、測試
 
在更新源服務器上新建一個文件,運行如下的命令,看文件是否能夠正常同步,看有無報錯信息
rsync -vzrtopg --delete --progress /data/www1/ www1@192.168.0.241::web1 --password-file=/etc/www1.pwd 
將要更新的文件提交到更新源服務器中,這樣就經過inotify+rsync批量的將更新文件同步到全部的目的服務器中
測試數據同步成功後,在客戶端執行如下腳本保證數據實時更新。
nohup sh /root/rsync.sh & 
相關文章
相關標籤/搜索