rsync+sersync2文件同步


环境:centos6.5


软件:rsync+sersync2


源服务器:192.168.10.109

同步服务器:192.168.10.107,192.168.10.108


前提工作:


关闭SELINUX

vi /etc/selinux/config


#SELINUX=enforcing

修改

SELINUX=disabled


重启系统生效


iptables打开端口


vi /etc/sysconfig/iptables

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT


service iptables restart



同步服务器环境配置


安装:

yum -y install rsync

配置

vi /etc/rsyncd.conf


log file = /var/log/rsyncd.log

pidfile = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

secrets file = /etc/rsync.passwd

motd file = /etc/rsyncd.Motd

[web]

path = /web

comment = web

uid = www

gid = www

port=873

use chroot = no

read only = no

list = no

max connections = 200

timeout = 600

auth users = sam

hosts allow = 192.168.10.109


创建密码文件

vi /etc/rsync.passwd


sam:tcg2015


设置文件权限


chmod 600 /etc/rsync.passwd


chmod 600 /etc/rsyncd.conf


启动服务:

rsync --daemon --config=/etc/rsyncd.conf


两台同步服务器都是一样配置



源服务器环境配置:


安装:

yum -y install rsync


查看服务器内核是否支持inotify

ll /proc/sys/fs/inotify


-rw-r--r-- 1 root root 0 Aug 12 11:20 max_queued_events

-rw-r--r-- 1 root root 0 Aug 12 11:41 max_user_instances

-rw-r--r-- 1 root root 0 Aug 12 11:20 max_user_watches


修改系统默认参数

vi /etc/sysctl.conf

在最后添加:

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535


立刻生效

sysctl -p


下载sersync


https://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz


tar zxvf sersync2.5_64bit_binary_stable_final.tar.gz


mkdir /usr/local/sersync

mkdir /usr/local/sersync/log

mkdir /usr/local/sersync/bin

mkdir /usr/local/sersync/conf


cd GNU-Linux-x86


cp confxml.xml /usr/local/sersync/conf/

cp sersync2 /usr/local/sersync/conf/


编辑配置文件

vi /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="/web">

            <remote ip="192.168.10.107" name="web"/>

            <remote ip="192.168.10.108" name="web"/>

        </localpath>

        <rsync>

            <commonParams params="-auvzP"/>

            <auth start="true" users="sam" passwordfile="/etc/rsync.passwd"/>

            <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/p_w_picpaths"/>

        </localpath>

    </plugin>

</head>


创建验证密码文件:

vi /etc/rsync.passwd

tcg2015


修改文件权限


chmod 600 /etc/rsync.passwd


完全同步一次文件:

/usr/local/sersync/bin/sersync2 -r -o /usr/local/sersync/conf/confxml.xml


同步完成后,kill掉sersync2进程


启动进程

/usr/local/sersync/bin/sersync2 -d -o /usr/local/sersync/conf/confxml.xml