一、为什么要用sersync+rsync架构?
1、sersync是基于inotify开发的,类似于inotify-tools的工具。
2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然
后使用rsync同步的时候,只同步发生变化的文件或者目录。
二、rsync+inotify-tools与sersync+rsync架构的区别?
1、rsync+inotify-tools
a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低。
2、sersync+rsync
a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;
b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在
遍历查找对比文件时,速度很快),因此效率很高。
总结:
1)、当同步的目录数据量不大时,建议使用rsync+inotify
2)、当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用sersync+rsync
三、sersync+rsync架构图

原理步骤:
1)在同步服务器(Master)上开启sersync服务,sersync负责监控配置路径中的文件系统事件变化(增删改);
2)调用rsync命令把更新的文件同步到目标服务器(S1 和 S2);
3)需要在主服务器配置sersync,在同步目标服务器配置rsync 。
通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客
户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器(S1 和S2)上相应的目录文件中。
四、Sersync介绍
sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使
用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify-
tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
1)sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在
结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
2)相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配
合bin目录下的xml配置文件直接使用即可。
3)另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服
务器实时保持同步状态。
4)本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失
败的文件重新同步。
5)本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。
无需再额外配置crontab功能。
6)本项目socket与http插件扩展,满足您二次开发的需要。
五、Sersync安装配置
5.1安装环境准备
角色
|
系统环境 |
IP |
名称 |
sersync服务器 |
CentOS 7.3 x86_64 |
192.168.10.20 |
Master主服务器 |
rsync服务器 |
CentOS 7.3 x86_64 |
192.168.10.150 |
S1目标服务器 |
rsync服务器 |
CentOS 7.3 x86_64 |
192.168.10.151 |
S2目标服务器 |
5.2 配置rsync目标服务器
1. 关闭SELINUX
[root@S1 ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
|
[root@S1 ~]#
setenforce 0
2. 关闭防火墙IPTABLES
[root@S1 ~]#
iptables -F
[root@S1 ~]#
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
|
3. 部署rsync服务
确定S1和S2目标服务器版本是最新的,配置如下:
[root@S1 ~]#
yum install rsync -y
[root@S1 ~]#
vim /etc/rsyncd.conf
# Rsync configuration:
uid = root
gid = root
use chroot = no
port = 873
max connections = 2000
timeout = 200
log file = /var/run/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b
#################################################
[rsync]
path = /opt/rsync/
commemt = rsync
list = yes
read only = no
auth users = test
secrets file = /etc/rsyncd.secret
ignore errors = yes
hosts allow = 192.168.10.20
hosts deny = 192.168.10.254
|
特别提示: S1,S2的机器同时部署上述服务;上面rsync服务的配置文件,表面允许sersync主服务器(ip:192.168.10.20)访问rsync同步模块名为[rsync],将同步过来的文件放入对应的path指定的目录/opt/rsync/下面; 如果有多台目标服务器,则每一台都需要进行类似的rsync服务配置,上面的uid、gid要换成您服务器的相应的同步用户;注意,rsync服务账户(本文用的是root)要有对被同步目录(/data/)的写入更新权限。
4. 创建rsync同步密码文件,并设置权限为600
[root@S1 ~]# echo "test:test123" > /etc/rsyncd.secret
[root@S1 ~]#
chmod 600 /etc/rsyncd.secret
[root@S1 ~]#
ll /etc/rsync.password
-rw-------. 1 root root 13 4月 8 10:14 /etc/rsyncd.secret |
[root@Master ~]# cat /etc/rsyncd.secret
5. 启动rsync守护进程,并写入开机自启动
[root@S1 ~]# rsync --daemon
[root@S1 ~]#
ps -ef | grep rsync
root 5735 1 0 14:45 ? 00:00:00 rsync --daemon
root 5737 5216 0 14:45 pts/3 00:00:00 grep --color=auto rsync
|
[root@S1 ~]# netstat -nulpt| grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 5588/rsync
tcp6 0 0 :::873 :::* LISTEN 5588/rsync
|
设置开机自启动【写入到/etc/rc.local里面】
[root@S1 ~]#
vim /etc/rc.local
# rsync server progress
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
|
6. 创建相关待同步的目录/opt/rsync/
[root@S1 ~]#
mkdir -p /opt/rsync/
[root@S1 ~]#
tree /opt
提示: 此步骤在S1,S2都要执行,否则rsync服务会因为没有PATH路径而无法启动。
5.3 配置sersync主服务器
1. 关闭SELINUX,关闭防火墙IPTABLES
2. 安装rsync服务器并配置rsync客户端相关权限认证
[root@Master ~]#
yum install rsync -y
[root@Master ~]#
echo "test123" > /etc/rsyncd.secret
[root@Master ~]#
chmod 600 /etc/rsyncd.secret
-rw-------. 1 root root 13 4月 8 10:14 /etc/rsyncd.secret |
[root@Master ~]#
ll /etc/rsyncd.secret
注意:Master主服务器的密码文件可以和目标服务器的密码文件名称不一样,但是在xml文件中指定的密码文件必须是
Master主服务器的密码文件。
3. Master主服务器上手工测试rsync同步情况
特别提示:此步非常关键,如果测试不成功,后面的sersync配好了也不会同步数据。
1)在Master上创建待同步数据目录/opt/tongbu/及数据
[root@Master ~]#
mkdir -p /opt/tongbu/
[root@Master ~]#
touch /opt/tongbu/test{1,2,3}{a,b,c}
[root@Master ~]#
ls /opt/tongbu/
test1a test1b test1c test2a test2b test2c test3a test3b test3c |
2)在Master上执行同步命令
[root@Master ~]# rsync -avzP /opt/tongbu/ test@192.168.10.150::rsync --password-file=/etc/rsyncd.secret
[root@Master ~]# rsync -avzP /opt/tongbu/ test@192.168.10.151::rsync --password-file=/etc/rsyncd.secret
提示:在后面进行部署sersync之前,sersync主服务器上必须要确保手工可以把文件推送到S1,S2上,这样后续sersync才能调用这些命令来自动推送。
3)在Master上执行推送命令后,在S1,S2上查看同步目录/opt/rsync/中的内容,如果内容同步完成,进行sersync的配置;未完成,检查rsync配置及认证用户密码信息。
4. Mster上开始部署sersync服务
1)下载sersync
[root@Master ~]# wget -c
https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@Master ~]# tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@Master ~]# mv GNU-Linux-x86 /usr/local/sersync
2) 配置sersync
[root@Master local]#cp sersync/confxml.xml sersync/confxml.xml.$(date +%F)
[root@Master local]# ll sersync/confxml.xml
-rwxr-xr-x. 1 root root 2215 Apr 8 19:07 confxml.xml
|
[root@Master local]# ll sersync/confxml.xml*
-rwxr-xr-x. 1 root root 2215 Apr 8 19:07 confxml.xml
-rwxr-xr-x. 1 root root 2215 Apr 8 23:19 confxml.xml.2017-04-08
|
3)修改配置文件confxml.xml
修改涂色部分
[root@Master local]# vim 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="/opt/tongbu">
<remote ip="192.168.10.150" name="rsync"/>
<!--<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="test" passwordfile="/etc/rsyncd.secret"/>
<userDefinedPort start="true" port="873"/><!-- port=874 -->
<timeout start="true" time="300"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/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>
|
5. 开启sersync守护进程同步数据,启动命令
[root@Master ~]#/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
配置sersync环境变量
[root@Master ~]#echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
[root@Master ~]#source /etc/profile
[root@Master ~]#sersync2 -d -r -o /usr/local/sersync/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
daemon thread num: 10
parse xml config file
host ip : localhosthost port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user istest
passwordfile is /etc/rsyncd.secret
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 /opt/tongbu && rsync -artuz -R --delete ./ --port=873 --timeout=300 test@192.168.10.150::rsync --password-file=/etc/rsyncd.secret >/dev/null 2>&1
run the sersync:
watch path is: /opt/tongbu
|
以上就是我实现的Sersync服务器数据同步配置,大家可以进行相互学习借鉴······
本文转自MQ_douer 51CTO博客,原文链接:http://blog.51cto.com/douer/1914098,如需转载请自行联系原作者