1. 程式人生 > >rsync+inotify實現資料實時同步

rsync+inotify實現資料實時同步

主伺服器192.168.222.4   CentOS7
從伺服器192.168.222.5   CentOS6
實時同步/home/data1目錄到從伺服器
從伺服器192.168.222.5操作
# yum -y install rsync*
建立rsyncd.conf檔案
vim /etc/rsyncd.conf

uid = root    #以root使用者執行rsync服務
gid = root    #以root使用者執行rsync服務
use chroot = no  #增加對目錄檔案軟連線的備份
max connections = 1200  #最大連線數
timeout = 800    #超時時間
pid file = /var/run/rsyncd.pid   #PID存放位置
lockfile = /var/run/rsyncd.lock  #鎖檔案存放位置
log file = /var/log/rsyncd.log  #日誌存放位置
[synchronization]  #認證模組名
path = /home/data1   #同步A伺服器的檔案路徑
ignore errors = yes  #忽略無關錯誤資訊
hosts allow = 192.168.222.5   #允許訪問IP
hosts deny = * #除了從主機外拒絕所有
read only = no  #允許上傳
write only = no #允許下載
list = yes   #允許列出同步目錄
auth users = root   #同步的使用者
secrets file = /etc/rsync.pass  #存放使用者密碼的檔案
###########可以配置多個同步模組
[download]
path = /home/download
ignore errors = yes
hosts allow = 192.168.222.5
hosts deny = *
read only = no
list = yes
auth users = root
secrets file = /etc/rsync.pass
注:/etc/rsync.pass 檔案格式為username:password檔案許可權必須為600否則服務不正常,hosts allow 定義可為單獨IP也可為網段,網段格式為192.168.222.0/24也可為192.168.222.0/255.255.255.0
建立rsync.pass檔案
# echo “root:password” >> /etc/rsync.pass
# chmod 600 /etc/rsync.pass
啟動服務
# rsync --daemon –v
開機自啟動
# echo “rsync --daemon –v” >> /etc/rc.local
主伺服器192.168.222.4操作
下載軟體包
inotify-tools下載:wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
sersync下載:wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
解壓
tar xf inotify-tools-3.14.tar.gz –C /usr/local
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz–C /usr/local
cd /usr/local
mv inotify-tools-3.14inotify-tools
mv GNU-Linux-x86 sersync
編譯安裝inotify-tools
cd inotify-tools
./configure  make  make install

2.3服務配置
配置sersync
cd sersync
cp confxml.xml confxml.xml_bak
vi confxml.xml
<?xml version="1.0"encoding="ISO-8859-1"?>
<head version="2.5"> <host hostip="localhost"port="8008"></host> <debug start="false"/> <fileSystem xfs="false"/> <filter start="false"> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="false"/> <closeWrite start="true"
/>
<moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="false"/> </inotify> <sersync> <localpath watch="/usr/local/tongbu"><!—與從伺服器同步的檔案路徑--> <remote ip="192.168.222.5(從伺服器地址)" name="data1"/><!—與從伺服器的模組名必須相同--> </localpath> <rsync> <commonParams params="-artuzlpog"/><!—rsync引數設定--> <auth start="true"users="root" passwordfile="/usr/local/sersync/user.pass"/> <userDefinedPort start="false"port="873"/><!-- port=874 --> <timeout start="false" time="100"/><!--timeout=100 --> <ssh start="false"/> </rsync> <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once--> <crontab start="false"schedule="600"><!--600mins--> <crontabfilter start="false"> <excludeexpression="*.php"></exclude> <excludeexpression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync> </head> ########如果是多個目錄同步需要起兩個sersync程序同時需要兩個啟動配置檔案 cp /usr/local/sersync/confxml.xml /usr/local/sersync/downxml.xml vi /usr/local/sersync/downxml.xml <?xml version="1.0"encoding="ISO-8859-1"?> <head version="2.5"> <host hostip="localhost"port="8008"></host> <debug start="false"/> <fileSystem xfs="false"/> <filter start="false"> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="false"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="false"/> </inotify> <sersync> <localpath watch="/download"><!—與從伺服器同步的檔案路徑--> <remote ip="192.168.222.5(從伺服器地址)"name="download"/><!—與從伺服器的模組名必須相同--> </localpath> <rsync> <commonParams params="-artuzlpog"/><!—rsync引數設定--> <auth start="true"users="root" passwordfile="/usr/local/sersync/user.pass"/> <userDefinedPort start="false"port="873"/><!-- port=874 --> <timeout start="false" time="100"/><!--timeout=100 --> <ssh start="false"/> </rsync> <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once--> <crontab start="false"schedule="600"><!--600mins--> <crontabfilterstart="false"> <excludeexpression="*.php"></exclude> <excludeexpression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync> </head> 配置密碼檔案 echo “password” > /usr/local/sersync/user.pass 注:檔案許可權必須為600否則啟動異常 chmod 600 /usr/local/sersync/user.pass 2.4服務啟動 nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /usr/local/sersync/rsync.log 2>1 #######如果是多個目錄同步需要啟動兩個程序 nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/downxml.xml > /usr/local/sersync/rsync.log 2>1 -d:啟用守護程序模式 -r:在監控前,將監控目錄與遠端主機用rsync命令推送一遍 -n:指定開啟守護執行緒的數量,預設為10個 -o:指定配置檔案,預設使用confxml.xml檔案 開機啟動寫入/etc/rc.local檔案中