rsync+sersync2实现服务器文件镜像

快速搭建镜像服务器,用于cms发布,服务器资料备份等,采用国人开发的开源serrsync比以前繁琐的inotify+rsync的配置简单很多,当然serrsync也是采用的inotify+rsync

一.在镜像目标服务器部署rsync服务

目标服务器ip:192.168.113.132
1.1

新建/etc/rsyncd.conf 内容如下:
uid=root
gid=root
max connections=4
use chroot=no
strict modes=yes
port=873
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[backup]
path=/home/backup/
comment=testrsync
read only=no
list=no
auth users=root
secrets file=/etc/rsync.pas
hosts allow = 192.168.113.132,192.168.113.131,127.0.0.1
1.2
建立密码文件
echo "root:abc" >/etc/rsync.pas 此处的密码abc与root用户的系统密码无关
chmod 600 /etc/rsync.pas
1.3
启动rsync
rsync --daemon

 

二:源服务器配置:

服务器地址:192.168.113.131

2.0
配置rsync客户端
echo "abc" > /etc/rsync.pas
chmod 600 /etc/rsync.pas
注意此处的rsync.pas格式与rsync服务端的格式不同,本人最初在此处耗费了很多时间。
另外一种方式,不用建立密码文件,export RSYNC_PASSWORD=abc
2.1

安装serrsync2
下载最新版serrsync2 http://code.google.com/p/sersync/downloads/detail?name=sersync2.5_32bit_binary_stable_final.tar.gz
2.2
tar -zxvf sersync2.5_32bit_binary_stable_final.tar.gz
cd GNU-Linux-x86
cp confxml.xml a.xml
vi a.xml,录入以下内容
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="true"/>
    <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="/home/backup">
            <remote ip="192.168.113.132" name="backup"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" 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>

2.3 启动监听服务器 ./sersync2 -o a.xml -r -d

 

注:有可能需要配置防火墙,打开指定ip访问873端口的限制

iptables -A INPUT  -s 192.168.113.131 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT

iptables -A INPUT  -s 192.168.113.131 -p udp -m state --state NEW -m udp --dport 873 -j ACCEPT

 

测试环境

rsync服务器:centerOS5.4,rsync客户端:RedHatAS5

参考

rsync资料:http://www.linuxsir.org/main/?q=node/256
serrsync资料:http://hi.baidu.com/johntech/blog/category/%BF%AA%D4%B4%CF%EE%C4%BFsersync

 

你可能感兴趣的:(xml,linux,cms,PHP,ssh)