vps双机热备方案之:sersync2实现服务器文件实时同步

项目地址:http://code.google.com/p/sersync/

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:

sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
本项目socket与http插件扩展,满足您二次开发的需要。

Sersync安装和配置具体步骤:

虾爷的环境是centos 5.x 32位 所以这里就用centos做演示了,其他系统请大家自己补完

1. 从服务器_0x配置(可以多台,01,02,03):

首先,在两台服务器A和B同时装上rsync:

centos:yum install rsync -y

然后定义rsync的配置文件:

vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock

[tongbu] #这里填写同步名称
path=/wwwroot/tongbu.com/ #这里填写同步路径
comment  = tongbu
ignore errors = yes
read only = no
hosts allow =  192.168.0.100/24 #这里填主服务器ip
hosts deny = *

上面配置文件,表明允许主服务器(假设ip为192.168.0.100)访问,rsync同步模块名为[tongbu],将同步过来的文件放入path指定的目录/wwwroot/tongbu.com/
如果有多台从服务器,则每一台都需要进行类似的配置。

配置好之后,使用如下命令,开启rsync守护进程:

rsync --daemon

2. 在主服务器上安装配置sresync

# wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz 
# tar xf sersync2.5_32bit_binary_stable_final.tar.gz 
# cd GNU-Linux-x86/

配置xml文件:

vi confxml.xml

修改如下位置:

<sersync>
	<localpath watch="/wwwroot/tongbu.com/">
		<remote ip="192.168.0.100" name="tongbu"/>
		<remote ip="192.168.0.105" name="tongbu"/>
	</localpath>

表明要将主服务器上本地的 /wwwroot/tongbu.com/ 路径下的文件,同步到远程服务器 192.168.0.104 与 192.168.0.105 上的tongbu 模块。

具体文件详细解释:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<head version="2.5"> 
    <host hostip="localhost" port="8008"></host>    #针对插件保留的,默认配置即可
    <debug start="false"/>  #开户debug信息,会在sersync当前运行台,打印Debug信息
    <fileSystem xfs="false"/>   #是否支持xfs文件系统
    <filter start="false">      # 是否开户文件过滤,可以在下面添加过滤类型
    <exclude expression="(.*)\.svn"></exclude> 
    <exclude expression="(.*)\.gz"></exclude> 
    <exclude expression="^info/*"></exclude> 
    <exclude expression="^static/*"></exclude> 
    </filter> 
    <inotify>                   # inotify监控的事件
    <delete start="false"/>    #是否保持Sersync和同步端两端文件完全一致
    <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="/data/mp3/">   #这里定义要监控的本地目录,这个很重要
        <remote ip="1.1.1.100" name="mp3"/>   #要同步到哪台服务器,Rsync标签是什么
        <!--<remote ip="192.168.8.39" name="tongbu"/>--> 
        <!--<remote ip="192.168.8.40" name="tongbu"/>--> 
    </localpath> 
    <rsync>                #配置Rsync信息
        <commonParams params="-artuz"/>   #rsync的参数
        <auth start="true" users="syncuser" passwordfile="/etc/rsyncd/rsyncd.pass"/> 
        <userDefinedPort start="false" port="874"/><!-- port=874 -->  # 定义rsync端口
        <timeout start="false" time="100"/><!-- timeout=100 -->  #定义传输超时时间
        <ssh start="false"/>        #Rsync的时候,是否使用ssh加密
    </rsync> 
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> 
    <crontab start="false" schedule="600"><!--600mins-->   #定义crontab定期完全同步两端文件
        <crontabfilter start="false">            #crontab同步时候的过滤条件,上面的过滤部开头要开
        <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> 

接着执行:

./sersync2 -r -d

通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。

3.还可以下载sersync 进程监控脚本:http://sersync.googlecode.com/files/checksersync.sh

使用crontab命令来设置监控脚本,监测sersync运行:

crontab -e

*/5 * * * * /root/GNU-Linux-x86/checksersync.sh

执行前请先设置修改 checksersync.sh 中目录参数.

遇到的问题:
启动时提示:

failed to create pid file /var/run/rsyncd.pid: File exists

使用指令:

rm -rf /var/run/rsyncd.pid

重启已经在运行的rsync:

[root@mail video]# ps -ef | grep rsync 
root     27284     1  0 10:26 ?        00:00:00 rsync –daemon –config=/etc/rsyncd.conf 
root     30516 29986  0 18:35 pts/3    00:00:00 grep rsync 
[root@mail video]# kill -9 27284 
[root@mail video]# rsync –daemon –config=/etc/rsyncd.conf

小 虾

哦也,我是小虾

You may also like...