[toc] 實時同步sersync實戰 什麼是實時同步 實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發後會將變化的目錄同步至遠程伺服器。 sersync和rsync+inotify對比 提到數據同步就必然會談到 ,一般簡單的伺服器數據傳輸會使用 等方式,但是這樣的方式效率不高,不支持差 ...
目錄
實時同步sersync實戰
什麼是實時同步
實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發後會將變化的目錄同步至遠程伺服器。
sersync和rsync+inotify對比
提到數據同步就必然會談到rsync
,一般簡單的伺服器數據傳輸會使用ftp/sftp
等方式,但是這樣的方式效率不高,不支持差異化增量同步也不支持實時傳輸。針對數據實時同步需求大多數人會選擇rsync+inotify-tools
的解決方案,但是這樣的方案也存在一些缺陷(文章中會具體指出),sersync
是國人基於前兩者開發的工具,不僅保留了優點同時還強化了實時監控,文件過濾,簡化配置等功能,幫助用戶提高運行效率,節省時間和網路資源。
sersync項目實戰
1)環境準備
角色 | 外網IP(NAT) | 內網IP(LAN) | 安裝工具 |
---|---|---|---|
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | 部署代碼(提交作業) |
nfs-server | eth0:10.0.0.31 | eth1:172.16.1.31 | rsync+inotify+sersync |
backup | eth0:10.0.0.41 | eth1:172.16.1.41 | rsync-server |
1.實時同步哪台伺服器的目錄,那麼就在哪台伺服器上安裝sersync
2.只要安裝sersync
就必須安裝rsync
和inotify
安裝rsync的服務端(backup)
1)安裝rsync服務
[root@backup ~]# yum install -y rsync
2)配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[zls]
comment = welcome to oldboyedu backup!
path = /backup
[nfs]
comment = welcome to oldboyedu backup!
path = /data
3)創建用戶
[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
4)創建目錄並授權
[root@backup ~]# mkdir /data /backup
[root@backup ~]# chown -R www.www /data/ /backup/
5)創建虛擬用戶的密碼文件並授權
[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
6)啟動rsync服務
[root@backup ~]# systemctl start rsyncd
NFS服務端部署sersync
1)安裝sersync需要依賴rsync
和inotify
[root@nfs ~]# yum install -y rsync inotify-tools
2)下載sersync
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
3)部署sersync
源碼包:解壓 生成 編譯 安裝
解壓:
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
4)移動並改名
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
5)編輯配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
-----------------------------------------------------------------------------------------
<sersync>
#監控的目錄,改成/data
<localpath watch="/opt/tongbu">
#推送的IP(backup服務的IP)172.16.1.41 ,name是模塊名
<remote ip="127.0.0.1" name="tongbu1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
#執行rsync的參數改成 -az
<commonParams params="-artuz"/>
#虛擬用戶的用戶名和密碼文件,開啟認證start=true rsync_backup /etc/rsync.pass
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
#設置超時時間
<timeout start="true" 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>
#完整配置文件
[root@nfs ~]# cat /usr/local/sersync/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="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="nfs"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" 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>
6)創建虛擬用戶的密碼文件,並授權
[root@nfs sersync]# echo '123' > /etc/rsync.pass
[root@nfs sersync]# chmod 600 /etc/rsync.pass
7)查看幫助
[root@nfs sersync]# /usr/local/sersync/sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
參數-d:啟用守護進程模式
參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
c參數-n: 指定開啟守護線程的數量,預設為10個
參數-o:指定配置文件,預設使用confxml.xml文件
參數-m:單獨啟用其他模塊,使用 -m refreshCDN 開啟刷新CDN模塊
參數-m:單獨啟用其他模塊,使用 -m socket 開啟socket模塊
參數-m:單獨啟用其他模塊,使用 -m http 開啟http模塊
不加-m參數,則預設執行同步程式
________________________________________________________________
8)啟動sersync
[root@nfs data]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml
------------------------------------------------rsync服務端--------------------------------------------------
1)安裝
[root@backup ~]# yum install -y rsync
2)改配置
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[zls]
comment = welcome to oldboyedu backup!
path = /backup
3)創建系統用戶
[root@backup ~]# useradd rsync -s /sbin/nologin -M
4)創建虛擬用戶及密碼文件並授權
[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
5)創建目錄並授權
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync.rsync /backup
6)啟動服務並加入開機自啟
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd
----------------------------------------------------------rsync的客戶端(nfs)------------------------------------------------------------------
1)安裝sersync(rsync+inotify)
[root@nfs ~]# yum install -y rsync inotify-tools
2)安裝sersync
下載:
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
解壓:
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
移動並改名:
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
3)修改配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/zls">
<remote ip="172.16.1.41" name="zls"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
4)創建目錄
[root@nfs ~]# mkdir /zls
5)創建密碼文件並授權
[root@nfs ~]# echo '123' > /etc/rsync.pas
[root@nfs ~]# chmod 600 /etc/rsync.pas
6)啟動sersync
[root@nfs ~]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml
sersync 就是rsync的客戶端
底層調用:rsync和inotify