1.sersync介绍

sersync类似于inotify,同样用于监控,但它克服了inotify的缺点.

inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。比如:vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的

官网: GitHub – wsgzao/sersync: rsync+inotify-tools

2.实时同步工具选择

inotify+rsync

inotify 监控 目录中文件变化rsync 进行推送,推送到rsync服务端(备份服务器)

sersync+rsync

sersync inotify升级版,推送效率,速度更高。无需书写脚本,直接运行sersync服务,自动监控与推送下载软件包(绿色软件,下载解压既用)

lsyncd

实时同步工具yum安装

3.sersync工作原理和同步原理

在同步服务器(Master)上开启sersync服务,sersync负载监控配置路径中的文件系统事件变化
调用rsync命令把更新的文件同步到目标服务器(01 和 02)
需要在主服务器配置sersync,在同步目标服务器配置rsync server(注意:是rsync服务)

用户实时的往sersync服务器(M)上写入更新文件数据
此时需要在同步主服务器(M)上配置sersync服务
在01 和02上开启rsync守护进程服务,以同步拉取来自sersync服务器(M)上的数据
通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器(S1 和S2),如此简单

4.rsync+inotify-tools与rsync+sersync架构的区别?

rsync+inotify-tools

inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来
rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低

rsync+sersync

sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字
rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高

5.sersync应用

准备两台机器

rsync服务端

[root@backup-rsync ~]$ hostname -I | cut -f2 -d ‘ ‘
172.16.1.116

sersync

[root@nfs ~]$ hostname -I | cut -f2 -d ‘ ‘
172.16.1.16

环境准备

上传sersync包

[root@nfs ~]$ ll
-rw-r–r– 1 root root 727290 Oct 10 11:18 sersync2.5.4_64bit_binary_stable_final.tar.gz

解压

[root@nfs ~]$ tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]$ ll
drwxr-xr-x 2 root root 41 Oct 26 2011 GNU-Linux-x86

[root@nfs ~]$ tree GNU-Linux-x86/
GNU-Linux-x86/
├── confxml.xml   (配置文件)
└── sersync2 (命令)

0 directories, 2 files

移动目录并修改名字

[root@nfs ~]$ mv GNU-Linux-x86/ /usr/local/sersync

创建目录放相对应的命令配置文件

[root@nfs ~]$ mkdir -p /usr/local/sersync/{conf,bin,logs}

[root@nfs ~]$ cd /usr/local/sersync/
[root@nfs /usr/local/sersync]$ ll
total 1772
drwxr-xr-x 2 root root 6 Oct 10 12:18 bin
drwxr-xr-x 2 root root 6 Oct 10 12:18 conf
-rwxr-xr-x 1 root root 2214 Oct 26 2011 confxml.xml
drwxr-xr-x 2 root root 6 Oct 10 12:18 logs
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 sersync2

[root@nfs /usr/local/sersync]$ mv confxml.xml conf
[root@nfs /usr/local/sersync]$ mv sersync2 bin/
[root@nfs /usr/local/sersync]$ tree -F
.
├── bin/
│   └── sersync2*
├── conf/
│   └── confxml.xml*
└── logs/

3 directories, 2 files

创建软连接(如果不创建使用命令时需要加目录/usr/..)

[root@nfs ~]$ ln -s /usr/local/sersync/bin/sersync2 /sbin/sersync

测试软连接是否成功

[root@nfs ~]$ sersync -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参数,则默认执行同步程序
________________________________________________________________

-h 查看帮助
-d 以守护进程方式启动
-r 开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步
-o 指定配置文件
-n 指定默认的线程池的线程总数
-m 不进行同步,只运行插件

配置文件

[root@nfs ~]$cat /usr/local/sersync/conf/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=”false”/>
<closeWrite start=”true”/>
<moveFrom start=”true”/>
<moveTo start=”true”/>
<attrib start=”false”/>
<modify start=”false”/>
</inotify>

<sersync>
<localpath watch=”/opt/tongbu”>                                   <!–监控那个目录–>
<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>
<commonParams params=”-artuz”/>                            <!–执行的命令参数–>
<auth start=”false” users=”root” passwordfile=”/etc/rsync.pas”/>    <!–是否开启认证,认证的用户及密码文件–>
<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>

检查 rsync客户端与服务端是否 正常访问

[root@nfs ~]$ rsync -avPz /all-backup/ rsync_backup@172.16.1.116::backup –password-file=/etc/rsync.passwd
sending incremental file list

sent 230 bytes received 21 bytes 502.00 bytes/sec
total size is 476 speedup is 1.90

修改配置文件(先备份一个配置文件)

[root@nfs ~]$ cp /usr/local/sersync/conf/confxml.xml{,.bak}

[root@nfs ~]$ ll /usr/local/sersync/conf/
total 8
-rwxr-xr-x 1 root root 2214 Oct 26 2011 confxml.xml
-rwxr-xr-x 1 root root 2214 Oct 10 12:28 confxml.xml.bak

[root@nfs ~]$ vim /usr/local/sersync/conf/confxml.xml

[root@nfs ~]$ diff /usr/local/sersync/conf/confxml.xml /usr/local/sersync/conf/confxml.xml.bak
24,25c24,25
< <localpath watch=”/dmxsp”>
< <remote ip=”172.16.1.116″ name=”backup”/>

> <localpath watch=”/opt/tongbu”>
> <remote ip=”127.0.0.1″ name=”tongbu1″/>
30,31c30,31
< <commonParams params=”-az “/>
< <auth start=”true” users=”rsync_backup” passwordfile=”/etc/rsync.passwd”/>

> <commonParams params=”-artuz”/>
> <auth start=”false” users=”root” passwordfile=”/etc/rsync.pas”/>
36c36
< <failLog path=”/var/log/sersync.log” timeToExecute=”60″/><!–default every 60mins execute once–>

> <failLog path=”/tmp/rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>

配置密码文件,修改权限

[root@nfs ~]$ echo 1>/etc/rsync.passwd
[root@nfs ~]$ chmod 600 /etc/rsync.passwd
[root@nfs ~]$ ll /etc/rsync.passwd
-rw——- 1 root root 1 Sep 27 19:20 /etc/rsync.passwd

启动

[root@nfs ~]$ sersync -d -r -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: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
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 rsync_backup
passwordfile is /etc/rsync.passwd
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 /dmxsp && rsync -az –delete -R –delete ./ rsync_backup@172.16.1.116::backup –password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /dmxsp

sersync实时同步功能

作者 dmxsp

发表回复

您的电子邮箱地址不会被公开。