centos6.6配置rsync+sersync實現實時同步分佈式多客戶端分發同步

一、sersync項目:
sersync項目利用inotify與rsync技術實現對服務器數據實時同步到解決方案,其中inotify用於監控sersync所在服務器上文件系統的事件變化,rsync是目前普遍使用的本地及異地數據同步工具,其優勢是隻對變化的目錄數據操做。
sersync項目的優勢:
1)使用C++編寫,對linux系統文件產生的臨時文件和重複的文件操做會進行過濾,再結合rsync同步到時候,會減小網絡資源,所以速度更快。
3)使用多線程進行同步,尤爲在同步較大文件時,可以保證多個服務器實時保持同步狀態。
4)sersync自帶出錯處理機制,經過失敗隊列對出錯的文件從新同步,若是扔失敗,則每10個小時對同步失敗的文件再從新同步。
5)sersync自帶crontab功能,只需在xml配置文件中開啓,便可按預先的配置,隔一段時間整理同步一次。
6)sersync自帶socket與http的協議擴展,能夠知足有特須要去掉公司二次開發。
inotify識別事件參考:
二、配置前準備
1)環境準備說明:
192.168.233.129 rsync server(rsync服務端)
192.168.233.132 sersync
2)配置前檢查
cat /etc/redhat-release
CentOS release 6.6 (Final)
uname -r
2.6.32-504.el6.x86_64
uname -m
x86_64
3)配置rsync服務端
#yum安裝:
yum install -y rsync
# 下載編譯安裝很簡單,tar==>configure --ferfix=/usr/local/rsync ==>make && make install 不詳細列出步驟了,我這選擇的是YUM安裝,注意在編譯安裝後注意路徑環境變量就能夠了,其餘都同樣。
#檢查安裝
rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
#創建密碼文件,並修改權限
echo "rsyncback: 123.com " >/etc/rsync.password
chmod 600 /etc/rsync.password
#創建配置文件:
cat >>/etc/rsyncd.conf<<EOF
#/etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 200
hosts allow = *
timeout = 600
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
ignore errors
read only = false
list = false
host deny = 0.0.0.0/32
auth users = rsyncback
secrets file = /etc/rsync.password
transfer logging = yes
#Module definitions
[data]
comment = data by pjy
path = /data/
####################################
[www]
comment = www by pjy
path = /data/www/
###################################
[bbs]
comment = bbs by pjy
path = /data/bbs/
##################################
[blog]
comment = blog by pjy
path = /data/blog/
##################################
EOF
#建立模塊監控目錄,我這用的是root用戶因此沒有配置權限,若是指定的用戶不一樣時,須要給目錄配置用戶權限。
mkdir -p /data/{www,bbs,blog}
tree /data
/data
├── bbs
├── blog
└── www
#啓動rsync服務:
/usr/bin/rsync --damon
ps -ef|grep rsync|grep -v grep
root 7858 1 0 20:09 ? 00:00:00 rsync --daemon
netstat -lntp|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7858/rsync
tcp 0 0 :::873 :::* LISTEN 7858/rsync
lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 7858 root 3u IPv4 224418 0t0 TCP *:rsync (LISTEN)
rsync 7858 root 5u IPv6 224419 0t0 TCP *:rsync (LISTEN
#在sersync端創建密碼文件,並測試同步是否正常
echo " 123.com " >/etc/rsync.password
chmod 600 /etc/rsync.password
rsync -zvaP --password-file=/etc/rsync.password rsyncback@192.168.233.129::data /data/
tree data/
./
├── bbs
├── blog
└── www
三、安裝sersync服務
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
cp -rf GNU-Linux-x86/ /usr/local/sersync/
cd /usr/local/sersync/
mkdir bin logs conf
mv confxml.xml conf/
mv sersync2 bin/sersync
tree
.
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── logs
#備份配置文件
cp conf/confxml.xml conf/confxml.xml.bak$(date +%F)
#配置環境變量
echo "export PATH=$PATH:/usr/local/sersync/bin" >>/etc/profile
tail -1 /etc/profile
source /etc/profile
which sersync
1)單實例配置:
#修改sersync配置文件:(有註釋說明的地方是須要修改的)
<?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>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data"> #指定本地要同步到目錄
<remote ip="192.168.233.129" name="data"/> #指定rsync服務端的ip地址和模塊名
</localpath>
<rsync>
<commonParams params="-aruz"/> #指定rsync同步選項
<auth start="true" users="rsyncback" passwordfile="/etc/rsync.password"/> #開啓指定密碼,對應rsync同時的用戶名和密碼文件
#如:rsync -zvaP --password-file=/etc/rsync.password rsyncback@192.168.233.129::data /data/
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 --> #指定超時100秒
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/www_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>
#配置英文變量,以防亂碼:
LANG=EN
#啓動監控服務(參數說明在後面)
sersync -r -d -o /usr/local/sersync/conf/confxml.xml
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
option: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsyncback
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www && rsync -aruz -R --delete ./ rsyncback@192.168.233.129::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data/www
#單實例監控完成,測試成功!
2)多實例配置(這次只用了一臺rsync服務器模擬多臺rsync服務端實時同步配置,換成多臺服務器只需修改ip地址和模塊便可)
#靠譜3個配置文件,模擬www,bbs,blog代碼同步分發
cd /usr/local/sersync/conf
cp confxml.xml www_confxml.xml
cp confxml.xml bbs_confxml.xml
cp confxml.xml blog_confxml.xml
#修改配置文件:(這裏只列出修改部分)
cat www_confxml.xml
<sersync>
<localpath watch="/data/www">
<remote ip="192.168.233.129" name="www"/>
</localpath>
<rsync>
<commonParams params="-aruz"/>
<auth start="true" users="rsyncback" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/www_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
cat bbs_confxml.xml
<sersync>
<localpath watch="/data/bbs">
<remote ip="192.168.233.129" name="bbs"/>
</localpath>
<rsync>
<commonParams params="-aruz"/>
<auth start="true" users="rsyncback" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/bbs_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
cat blog_confxml.xml
<sersync>
<localpath watch="/data/blog">
<remote ip="192.168.233.129" name="blog"/>
</localpath>
<rsync>
<commonParams params="-aruz"/>
<auth start="true" users="rsyncback" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/blog_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
#修改註明:(版本號爲sersync2.5.4_64)
1.修改默認配置的23到28行,指定本地同步目錄,對端ip和模塊
2.修改默認配置的30到33行,指定rsync同步時的參數,開啓密碼文件,指定用戶,密碼文件路徑。
3.修改默認配置的36行,指定消息隊列文件路徑。
#重啓動sersync服務:
ps -ef|grep sersync
root 14270 1 0 21:44 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xml
root 14445 13674 0 22:00 pts/0 00:00:00 grep sersync
pkill sersync
ps -ef|grep sersync
root 14459 13674 0 22:01 pts/0 00:00:00 grep sersync
sersync -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
#查看進程
ps -ef|grep sersync
root 14464 1 0 22:02 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/www_confxml.xml
root 14482 1 0 22:03 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
root 14500 1 0 22:03 ? 00:00:00 sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
#測試文件同步
cd /data
for i in `ls`;do echo ddd >>$i/$i$i.sh;done
tree /
./
├── bbs
│   ├── bbsbbs.sh
│   └── bbs.log
├── blog
│   ├── blogblog.sh
│   └── blog.log
└── www
├── www.log
└── wwwwww.sh
#rsync服務端,三個模塊實時同步成功!
#將sersync服務加入到開啓自啓動
cat >>/etc/rc.local<<EOF
#sync data to 192.168.233.192
sersync -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
EOF
tail -4 /etc/rc.local
#sync data to 192.168.233.192
sersync -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
#查看實時監控進程,應該是三個sersync守護進程,
ps -ef |grep rsync
root 14464 1 0 22:02 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/www_confxml.xml
root 14482 1 0 22:03 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
root 14500 1 0 22:03 ? 00:00:02 sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
參數說明:
-r 開啓實時監控以前對主服務器目錄與遠程目錄目錄進行一次總體同步,若是設置了過濾器,即在xml文件中,filter爲true,則暫時不能使用-r參數進行總體同步.
-o 指定配置文件,如不指定sersync會使用sersync可執行文件目錄下的默認配置文件confxml.xml.
-n 指定默認的線程池的線程總數,如不指定默認啓動線程池數量是10個
-d 後臺啓動服務
-m 不進行同步,只運行插件,如:sersync -m command 則在監控到事件後,不對遠程目錄服務器進行同步,而是直接運行command插件
#後續配置文件改進更新。。。。。。。
相關文章
相關標籤/搜索