首页 > 代码库 > sersync基于rsync+inotify实现数据实时同步

sersync基于rsync+inotify实现数据实时同步

一、环境描述

需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步

主服务器A:192.168.1.23

从服务器B:192.168.1.243

实时同步/var/atlassian目录到从服务器。

二、实施

1.从服务器192.168.1.243 rsync服务搭建

1.1安装软件包

wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
tar xf rsync-3.1.1.tar.gz –C /opt/
mv /opt/rsync-3.1.1 /opt/rsync
cd /opt/rsync
./configure
make && make install
创建rsyncd.conf文件
vi /etc/rsyncd.conf
vim /etc/rsyncd.conf
#以root用户运行rsync服务
uid = root
#以root用户运行rsync服务
gid = root
#增加对目录文件软连接的备份
use chroot = no
#最大连接数
max connections = 1200
#超时时间
timeout = 800
#PID存放位置
pid file = /var/run/rsyncd.pid
#锁文件存放位置
lockfile = /var/run/rsyncd.lock
#日志存放位置
log file = /var/log/rsyncd.log
#认证模块名
[atlassian]
path = /var/atlassian
ignore errors = yes
hosts allow = 192.168.1.23
hosts deny = * 
read only = no
write only = no
list = yes
#主服务器的同步用户 auth users = ihome secrets file = /etc/rsync.pass

注:/etc/rsync.pass 文件格式为username:password

文件权限必须为600否则服务不正常

 

hosts allow 定义可为单独IP也可为网段,网段格式为172.26.7.0/24

也可为172.26.7.0/255.255.255.0

创建rsync.pass文件

echo “root:password”>>/etc/rsync.pass
chmod 600 /etc/rsync.pass

1.3启动服务

[root@ha-db atlassian]#rsync --daemon –v
开机自启动
[root@ha-db atlassian]#echo "rsync --daemon –v">>/etc/rc.local

 

2.主服务器192.168.1.23配置

2.1软件安装

下载
wget
http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz 解压 tar xf rsync-3.1.1.tar.gz –C /opt tar xf inotify-tools-3.14.tar.gz –C /opt tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz –C /opt cd /opt mv rsync-3.1.1 rsync mv inotify-tools-3.14 inotify-tools mv GNU-Linux-x86 sersync 编译安装rsync cd rsync ./configure && make && make install 编译安装inotify-tools cd inotify-tools ./configure && make && make install

2.3 sersync服务配置

vim /opt/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="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>

    <sersync>
        <localpath watch="/var/atlassian">
            <remote ip="192.168.1.243" name="atlassian"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuzlpog"/>
            <auth start="true" users="ihome" passwordfile="/opt/sersync/user.pass"/>
            <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="/var/atlassian">
            <deshost ip="192.168.1.243" 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>

配置密码文件

echo “password”>>/opt/sersync/user.pass

注:文件权限必须为600否则启动异常

2.4服务启动

nohup /opt/sersync/sersync2  -r -d -o/opt/sersync/confxml.xml >/opt/sersync/rsync.log  2>&1 &
#######如果是多个目录同步需要启动两个进程
nohup /opt/sersync/sersync2  -r -d -o/opt/sersync/downxml.xml >/opt/sersync/downrsync.log  2>&1
-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件
开机启动
echo "
nohup /opt/sersync/sersync2 -r -d -o/opt/sersync/confxml.xml >/opt/sersync/rsync.log 2>&1 & ">>/etc/rc.local

三、验证

在主服务器上的/var/atlassian目录下创建文件查看从服务器192.168.1.243上是否有同步。

参考:http://9402618.blog.51cto.com/9392618/1728794

sersync基于rsync+inotify实现数据实时同步