centos6.6配置rsync+sersync实现实时同步分布式多客户端分发同步

时间:2022-02-21 01:30:50
1、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识别事件参考:
2、配置前准备
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
3、安装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"/>
</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插件
#后续配置文件改进更新。。。。。。。