1、爲何要用Rsync+sersync架構?
一、sersync是基於Inotify開發的,相似於Inotify-tools的工具
二、sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字,而後使用rsync同步的時候,只同步發生變化的這個文件或者這個目錄。
2、Rsync+Inotify-tools與Rsync+sersync這兩種架構有什麼區別?
一、Rsync+Inotify-tools
(1):Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增長、刪除、修改),並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;
(2):rsync在同步的時候,並不知道具體是哪一個文件或者哪一個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以,效率很低。
二、Rsync+sersync
(1):sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或某一個目錄的名字;
(2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來講是很小的,rsync在遍歷查找比對文件時,速度很快),所以,效率很高。
小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件不少時,建議使用Rsync+sersync。
準備
操做系統 CentOS 7
sersync2.5.4_64bit
源服務器 192.168.0.248
目標服務器 192.168.0.249
把A機器上的一個目錄下文件的變化實時同步到B機器上,兩邊文件保持一致;
兩臺服務器的selinux與iptables自行設置好,rsync的端口兩邊要配置一致,默認是873
PS:本文中附上了目標服務器與源服務器的一鍵部署腳本(親測可用),若有須要可直接運行腳本進行環境部署,部署完成後只需修改你須要的參數便可使用(install_des.sh目標服務器腳本,install_source.sh源服務器腳本)
操做
目標服務器
1.安裝rsync服務與xinetd服務php
2.rsync配置(千萬注意:本人親自入坑,下面的配置信息,請在添加到配置文件中後千萬記得刪除掉後面#部分的備註信息,不然同步過程會失敗,不知緣由,刪除後便可正常同步,超級大坑...測試了一個早上才找到緣由)
linux
3.建立用於存放同步的用戶及密碼的文件/etc/rsync.pas,並將此文件權限修改成600
git
4.建立同步的目錄/data
github
5.啓動rsync與xinetd服務
正則表達式
6.配置rsync開機自啓動
express
源服務器vim
一、查看服務器內核是否支持inotifybash
ll /proc/sys/fs/inotify #列出文件目錄,出現下面的內容,說明服務器內核支持inotify服務器
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events架構
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
備註:Linux下支持inotify的內核最小爲2.6.13,能夠輸入命令:uname -a查看內核
CentOS 5.X 內核爲2.6.18,默認已經支持inotify
二、修改inotify默認參數(inotify默認內核參數值過小)
查看系統默認參數值:
sysctl -a | grep max_queued_events
結果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
結果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
結果是:fs.inotify.max_user_instances = 128
修改參數:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
vi /etc/sysctl.conf #添加如下代碼
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出
參數說明:
max_queued_events:
inotify隊列最大長度,若是值過小,會出現"** Event Queue Overflow **"錯誤,致使監控文件不許確
max_user_watches:
要同步的文件包含多少目錄,能夠用:find /home/www.osyunwei.com -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/home/www.osyunwei.com爲同步文件目錄)
max_user_instances:
每一個用戶建立inotify實例最大值
1.下載sersync源碼包並安裝sersync服務
2.建立密碼文件跟目標服務器同樣,不過這個文件只要保存密碼便可,不須要保存用戶名,權限也是600
3.配置sersync服務
4.建立同步目錄
5.設置環境變量
6.啓動、重啓、中止sersync服務
7.設置開機啓動sersync
最後啓動完源服務器上的sersync服務後重啓下目標服務器上的xinetd服務
vi sersync.sh
#!/bin/bash
/usr/local/sersync/bin/sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
chmod+x sersync.sh
將 sersync.sh加入 /etc/rc.local 開機啓動
測試:
在源服務器上的/data目錄新建文件或目錄再查看目標服務器上的/data目錄狀態
實例
vi /usr/local/sersync/conf/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="/data/lin">
<remote ip="192.168.30.171" name="data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="lgsync" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="true" port="878"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
參數說明:
localpath watch="/home/www.osyunwei.com":#源服務器同步目錄
192.168.21.127,192.168.21.128:#目標服務器IP地址
name="home_www.osyunwei.com": #目標服務器rsync同步目錄模塊名稱
users="home_www.osyunwei.com_user": #目標服務器rsync同步用戶名
passwordfile="/etc/passwd.txt": #目標服務器rsync同步用戶的密碼在源服務器的存放路徑
remote ip="192.168.21.127": #目標服務器ip,每行一個
remote ip="192.168.21.128": #目標服務器ip,每行一個
failLog path="/tmp/rsync_fail_log.sh" #腳本運行失敗日誌記錄
start="true" #設置爲true,每隔600分鐘執行一次全盤同步
五、設置sersync監控開機自動執行
vi /etc/rc.d/rc.local #編輯,在最後添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #設置開機自動運行腳本
:wq! #保存退出
六、添加腳本監控sersync是否正常運行
vi /home/crontab/check_sersync.sh #編輯,添加如下代碼
#!/bin/sh
sersync="/usr/local/sersync/bin/sersync2"
confxml="/usr/local/sersync/conf/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
:wq! #保存退出
chmod +x /home/crontab/check_sersync.sh #添加腳本執行權限
vi /etc/crontab #編輯,在最後添加下面一行
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1 #每隔5分鐘執行一次腳本
service crond reload #從新加載服務