rsync+sersync实现服务器文件双向同步

目的

在192.168.50.23与192.168.50.22之间同步/root/test文件夹

安装xinetd

yum install xinetd

安装rsync

yum -y install rsync

配置rsync

因为我们是双向同步,所以每台主机都是服务器,也都是主机,所以rsyncd的配置基本上是相同的

在CentOS中,可能会没有/etc/rsyncd.conf文件,需要手动创建

配置内容例子

log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
motd file = /etc/rsyncd.Motd
port = 873
[rsynctest]
path = /root/test
uid = root
gid = root
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = rsync
secrets file = /etc/rsync.pas
hosts allow = *
# hosts deny = 0.0.0.0

[rsynctest]为模块名

path为文件目录

max connections为最大连接数

auth users为用户名

secrets file为储存用户名和密码的文件

hosts allow为允许连接的IP,多个用逗号分隔

hosts deny为不允许连接的IP,多个用逗号分隔

/etc/rsync.pas中格式为

username:password

另外需要创建一个只储存密码的文件,如/etc/rsync1.pas

password

注意:/etc/rsync.pas和etc/rsync1.pas的权限必须为600,不然会出错,而且,如果在root下执行操作,这两个文件的owner必须为root

配置xinetd

我们使用xinetd来管理rsync服务,也可以设置xinetd开机启动,从而使rsync也开机启动

vi /etc/xinetd.d/rsync

找到disable = yes这一行,改为disable = no

重启xinetd

/etc/init.d/xinetd restart

设置开机启动rsync的daemon模式

echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

其实这里配置xinetd开机启动就行。。。

安装sersync

这里用的是github上的一个备份,不过不确定是不是最新的(本文写完的时候还是最新的)

wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

如果可以科学上网,则可去 https://code.google.com/archive/p/sersync/ 下载

mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mkdir /usr/local/sersync/log
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
cp confxml.xml /usr/local/sersync/conf
cp sersync2 /usr/local/sersync/bin
chmod 777 /usr/local/sersync/bin/sersync2

当然sersync2和confxml.xml,放哪里都可以,记得设置环境变量就行

配置sersync

以192.168.50.22为例打开confxml.xml文件,然后配置

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host><!-- 设置本地IP和端口 -->
    <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="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>

    <sersync>
	    <localpath watch="/root/test"><!-- 同步文件路径 -->
	        <remote ip="192.168.50.23" name="rsynctest"/><!-- 要同步的主机IP和模块名 -->
	    </localpath>
        <rsync>
            <commonParams params="-auvzL"/><!-- rsync指令参数 -->
            <auth start="true" users="rsync" passwordfile="/etc/rsync1.pas"/><!-- 用户名和只储存密码的密保文件 -->
            <userDefinedPort start="false" port="874"/><!-- 设置rsync远程服务端口,远程非默认端口则需打开自定义 -->
            <timeout start="true" time="100"/><!-- 设置超时时间 -->
            <ssh start="false"/><!-- 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书 -->
        </rsync>
        <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!-- sersync传输失败日志脚本路径,每隔60分钟会重新执行该脚本,执行完毕会自动清空 -->
        <crontab start="false" schedule="600"><!-- 设置rsync+crontab定时传输,默认关闭 -->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/><!-- 设置sersync传输后调用name指定的插件脚本,默认关闭 -->
    </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>

设置环境变量

echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
source /etc/profile

设置sersync开机自动启动

编辑/etc/rc.d/rc.local

vim /etc/rc.d/rc.local

最后一行添加

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

启动rsync

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

启动sersync

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

定时检测维护sersync脚本

创建/root/start_sersync.sh文件

. /etc/profile

FolderPath="/etc/sersync/conf/"
for fileName in ${FolderPath}/*.xml; do
    tempFile=`basename $fileName` 
    /etc/sersync/bin/sersync2 -r -d -o $FolderPath$tempFile
done

创建/root/keep_sersync.sh文件

#!/bin/sh
. /etc/profile

status=$(ps aux | grep 'sersync2' | grep -v 'grep' | wc -l)
sersync_path="/etc/sersync/bin/sersync2"
xml_path="/etc/sersync/conf/"
i=0

for fileName in ${xml_path}/*.xml; do
    i=$[$i+1]
done

if [ $status -ne $i ];then
    killall sersync2
    /root/start_sersync.sh
fi

这里在脚本中执行其他脚本或非系统默认的应用的时候,切记要用绝对路径,不然,在crond执行脚本会失败。

给与/root/keep_sersync.sh和/root/start_sersync.sh可执行权限

chmod 777 /root/keep_sersync.sh
chmod 777 /root/start_sersync.sh
ps -ef | grep crond

查看crond有没有启动,没有启动则

crond

设置crond开机启动

chkconfig --level 35 crond on

编辑/etc/crontab,加上

*/10 * * * * root /root/keep_sersync.sh > /dev/null 2>&1

这里是每10分钟调用一次/root/keep_sersync.sh脚本,时间可以自己定义,下面是格式,更多语法自行百度。

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

重启或者重载crond

/etc/init.d/crond restart

/etc/init.d/crond reload

其他

重启sersync

killall sersync2 && sersync2 -r -d -o /etc/sersync/conf/confxml.xml

查看同步日志

tailf /var/log/rsyncd.log

多服务器同步

如果要同步多台主机,则每台主机上需要创建对应其他每台主机的sersync的配置文件,且sersync需要建立同样数量相对应的进程。

例如,要实现三台主机之间互相同步,则要在每台上都创建对应其他两台主机的配置文件,以其中一台为例:

创建/usr/local/sersync/conf/confxml1.xml、/usr/local/sersync/conf/confxml2.xml文件

执行

sersync2 -d -r -o /usr/local/sersync/conf/confxml1.xml
sersync2 -d -r -o /usr/local/sersync/conf/confxml2.xml

同步多个模块也差不多

posted @ 2018-07-22 23:28  EmrysChe  阅读(3064)  评论(0编辑  收藏  举报