Linux下Rsync+sersync實現數據雙向實時同步

   剛好前面用rsync+sersync做了主從數據同步,現在有新的需求了,P2P系統兩臺服務器做負載均衡,但是由於P2P會生成N多的合同,N多的數字證書,會上傳文件,所以根據現實情況需要兩臺相互同步。

   感覺sersync應該能夠實現這個功能,感覺應該是能滿足要求的,接下來就部署起來了。

   安裝就參照前一篇文章了,下面是具體步驟:

   步驟一:規劃好需要同步的目錄

   爲方便起見,兩臺服務器要同步的目錄我建成一樣的結構,/home/p2padm/

   步驟二:配置好兩臺服務器的rsync(兩臺都要配置)

   Centos缺省是安裝了rsync的,只是缺省沒有配置和啓動後臺進程而已,做下配置,以下是Server1的配置過程,Server2的配置過程類似,只是hosts allow要換成Server1的。

vi /etc/rsyncd.conf

uid = p2padm
gid = p2p
read only = no
use chroot = true
transfer logging = true
log format = %h %o %f %l %b
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#hosts allow = trusted.hosts
#slp refresh = 300
max connections=30
[p2padm]
path = /home/p2padm/
comment = p2padm rsync
auth users = p2padm                    同步用的用戶名
secrets file = /etc/rsyncd.secrets
hosts allow = 172.16.17.11               對方機器IP
hosts deny = 0.0.0.0/32
exclude=tmp/ log/ trc/ backup/ dump/ jboss5/   排隊哪些文件夾下的內容不需要同步

vi /etc/rsyncd.secrets   p2padm:p2padm  chmod 666 rsyncd.secrets

步驟三:下載 sersync 並配置(兩臺都要配置)

以下是Server1的配置過程,Server2的配置過程類似,只是remote allow要換成Server1的,安裝參照前一篇,配置文件見下:

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="true">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^data/*"></exclude>
<exclude expression="^data/*/*"></exclude>
<exclude expression="^trc/*/*"></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="/home/p2padm">
   <remote ip="172.16.17.11" name="p2padm"/>
</localpath>
<rsync>
   <commonParams params="-artuz"/>
   <auth start="true" users="p2padm" passwordfile="/etc/rsync.pas"/> <!--這裏一定要設置爲true,纔會自動同步-->
   <userDefinedPort start="false" port="874"/><!-- 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>

步驟四:啓動sersync後臺daemon程序,開始進入監控(兩臺都要運行)

啓動兩臺的rsync

/usr/bin/rsync --daemon--config=/etc/rsyncd.conf

再啓動兩臺的sersync

/usr/local/sersync/sersync2-d -r -o /usr/local/sersync/confxml.xml

查看同步日誌tail -f 100 /var/log/rsync.log

完成,收工,在兩臺服務器的這個目錄下建目錄,加文件,刪除文件看,是不是同步了。

當然我這配置的都是手動啓動的,可以添加到系統自動啓動運行,我就先不弄了,留點後門。

發佈了32 篇原創文章 · 獲贊 14 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章