使用sersync 目的,实时备份重要服务器上的数据到 rsync服务器。

sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。

sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。

sersync自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。


sersync一般是一台服务器向多台服务器同步

【环境】

sersync 推送端:192.168.1.133
rsync 接收端:192.168.1.244
rsync 接收端:192.168.1.241

【rsync+web服务器端的配置:】

安装相关软件
jie1 ~]# yum -y install rsync xinetd
#rsync服务通常基于超级守护进程xinetd管理的方式来实现,因此需要事先安装rysnc和xinetd


【192.168.1.244】

[root@jie1 ~]# vim /etc/rsyncd.conf
############vim /etc/rsyncd.conf###############
uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file= /var/run/rsyncd.pid
log file= /var/log/rsyncd.log
[htdocs]
path = /web/htdocs
ignore errors = yes
readonly = no
write only = no
hosts allow = 192.168.1.244
list = false
uid = root
gid = root
auth users= backuper
secrets file= /etc/rsyncd.pwd
##############################################
[root@jie1 ~]#vim /etc/rsyncd.pwd
backuper:pwd123
[root@jie1 ~]# chmod 600 /etc/rsyncd.pwd
[root@jie1 ~]# chkconfig rsync on
[root@jie1 ~]# chkconfig xinetd on
后台启动rsync
rsync --daemon

[root@jie1 ~]# service xinetd start

Starting xinetd: [ OK ]

[root@jie1 ~]# netstat -pant | grep 873

tcp 0 0 :::873 :::* LISTEN 19876/xinetd


【sersync+web客户端的配置:】

1)、先下载安装sersync软件,做初始设置
[root@jie3 ~]#wget --no-check-certificate https://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
[root@jie3 ~]# ls
anaconda-ks.cfg install.log.syslog
install.log sersync2.5_64bit_binary_stable_final.tar.gz
 mkdir /usr/local/sersync
[root@jie3 ~]#mkdir -pv /usr/local/sersync/{conf,bin,log}
mkdir: created directory `/usr/local/sersync'
mkdir: created directory `/usr/local/sersync/conf'
mkdir: created directory `/usr/local/sersync/bin'
mkdir: created directory `/usr/local/sersync/log'
[root@jie3 ~]# tar xf sersync2.5_64bit_binary_stable_final.tar.gz
[root@jie3 ~]# cd GNU-Linux-x86/
[root@jie3 GNU-Linux-x86]# ls
confxml.xml sersync2
[root@jie3 GNU-Linux-x86]# mv confxml.xml /usr/local/sersync/conf/
[root@jie3 GNU-Linux-x86]# mv sersync2 /usr/local/sersync/bin/
[root@jie3 GNU-Linux-x86]# cd /usr/local/sersync/
[root@jie3 sersync]# echo "PATH=/usr/local/sersync/bin:$PATH" >>/etc/profile.d/sersync.sh
[root@jie3 sersync]# source /etc/profile.d/sersync.sh
[root@jie3 sersync]# echo "pwd123" >/usr/local/sersync/sersync.pwd
[root@jie3 sersync]# chmod 600 /usr/local/sersync/sersync.pwd


2)、修改sersync的配置文件

[root@jie3 sersync]# vim /usr/local/sersync/conf/confxml.xml

其实只要修改2个地方就好了

1、 #同步的路径,本地的目录

<localpath watch="/website">

#rsync服务器的ip地址和rsync配置文件里面定义的模块

<remote ip="192.168.1.244" name="htdocs"/> --此处ip写rsync 服务器ip

<remote ip="192.168.1.241" name="htdocs"/>

2、<auth start="true" users="backuper" passwordfile="/usr/local/sersync/sersync.pwd"/>

这里所写的信息,完全跟rsync服务器上所写的一样。auth状态由 false改为 true



3、测试:在sersync服务端新建新的东西,然后再rsync客户端看看会不会更新

[root@jie3 website]# sersync2 -r -d & --启动服务

启动成功后,会有这样显示:

run the sersync:

watch path is: /web/htdocs


然后再sersync服务端website目录下新建东西,看看rsync客户端会不会更新到

[root@jie3 ~]# cd /website/

[root@jie3 website]# touch index.html testdb.php test.html test.php

###rsync服务器端,查看可以来着sersync客户端的同步文件

[root@jie1 ~]# cd /web/htdocs/

[root@jie1 htdocs]# ls

index.html testdb.php test.html test.php

[root@jie1 htdocs]#


#########################config.xml配置文件详解#############

<head version="2.5">
 #设置本地的ip地址和监听的端口
 <host hostip="localhost" port="8008"></host>
 #debug模式是否开启
 <debug start="false"/>
#设置为true,开启debug模式,会在sersync正在运行的控制台,打印inotify事件与rsync同步命令
 #xfs文件系统是否开启
 <fileSystem xfs="false"/>
#对于xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作.
 #filter文件过滤功能
 #对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。
 #同步时,是否支持正则表达式,默认关闭。
 <filter start="false">
 <exclude expression="(.*)\.svn"></exclude>
 <exclude expression="(.*)\.gz"></exclude>
 <exclude expression="^info/*"></exclude>
 <exclude expression="^static/*"></exclude>
 </filter>
 # 设置要监控的事件
 #对于inotify监控参数可以进行设置,根据您项目的特点优化srsync。
<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>
#对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事 件close_write,同样可以实现文件完整同步。
#注意:强将createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控。
 #同步的设置,文件监控与远程同步设置
 <sersync>
 #同步的路径,本地的目录
 <localpath watch="/website">
 #rsync服务器的ip地址和rsync配置文件里面定义的模块
 <remote ip="172.16.22.1" name="htdocs"/>
 #<!-- -->括起来表示注释
 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync>
 #rsync指令参数
 <commonParams params="-artuz"/>
 #rsync同步认证设置的内容,user指定用户名,password指定存放密码的文件路径
 <auth start="true" users="backuper" passwordfile="/usr/local/sersync/sersync.pwd"/>
#auth start=”false” 设置为true的时候,使用rsync的认证模式传送,需要配置user与passwrodfile(–password-file=/etc /rsync.pas),来使用。userDefinedPort 当远程同步目标服务器的rsync端口不是默认端口的时候使用(–port=874)。timeout设置rsync的timeout时间 (–timeout=100)。ssh 使用rsync -e ssh的方式进行传输
 #设置rsync远程服务端口
 <userDefinedPort start="false" port="874"/><!-- port=874 -->
 #设置超时时间
 <timeout start="true" time="100"/><!-- timeout=100 -->
 #设置ssh加密传输模式,默认关闭
 <ssh start="false"/>
 </rsync>
 #设置sersync传输失败日志脚本路径
 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->#对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。
 #设置rsync+crontab定时传输,默认关闭
 <crontab start="false" schedule="600"><!--600mins-->
 <crontabfilter start="false">
 <exclude expression="*.php"></exclude>
 <exclude expression=\'#\'" /*"></exclude>
 </crontabfilter>
 </crontab>
#crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了crontab功 能,还可以进一步保证各个服务器文件一致,如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销。schedule这个参数是设置crontab的时间间隔,默认是600分钟
#如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对 应与过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与 filter的过滤一一对应。