linux系统使用Rsync+sersync实现文件实时同步

linux系统使用Rsync+sersync实现文件实时同步

源端:centos 6.5
目标端:centos 7

1、源断和目标端关闭SELINUX和防火墙

2、根据源端的组和用户在目标端创建组、用户
注意:uid和gid源端、目标端必须一致

一、以下步骤在目标端操作:

1、检查rsync软件是否安装

   rpm -qa | grep rsync

2、软件包组成中没有提供配置文件和相应的文件,因此需要自行添加如下:

[root@node1 ~]# mkdir /etc/rsyncd  ##创建配置目录 
[root@node1 ~]# touch /etc/rsyncd/rsyncd.conf  ##创建主配置文件 
[root@node1 ~]# touch /etc/rsyncd/rsyncd.secrets  ##创建用户密码文件 
[root@node1 ~]# chmod 600 /etc/rsyncd/rsyncd.secrets ##修改用户密码文件 
[root@node1 ~]# touch /etc/rsyncd/rsyncd.motd   ##创建定义服务器信息的文件 

3、编辑配置文件
编辑配置rsyncd.conf

[root@localhost ~]# cat /etc/rsyncd/rsyncd.conf 
# Minimal configuration file for rsync daemon 
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script 
pid file = /var/run/rsyncd.pid 
port = 873  ##监听端口 
address = 10.9.141.233  ##本地监听地址 
#uid = nobody 
#gid = nobody   
uid = root 
gid = root
use chroot = yes    ##是否限制在指定目录,为了安装,一般需要启用 
read only = no

#limit access to private LANs 
hosts allow=10.9.141.0/255.255.255.0    ##允许网段 
hosts deny=*

max connections = 5 
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file 
#log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync 
#transfer logging = yes
log format = %t %a %m %f %b 
syslog facility = local3 
timeout = 300
##定义一个同步目录
[appdata] 
path = /home/appdata
list=yes 
ignore errors 
auth users = limis 
secrets file = /etc/rsyncd/rsyncd.secrets   ##指定上述账号密码文件 
comment = appdata
#exclude =   data/   ##排除目录

4、添加账号密码文件

[root@node1 ~]# vi /etc/rsyncd/rsyncd.secrets
limis:limis

注意,这个账号是系统账号,但没有使用系统账号的密码,而是自定义密码

5、让rsync以守护进程运行

[root@node1 ~]# /usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf

6、开机自动启动

[root@node1 ~]# echo "/usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf" >> /etc/rc.d/rc.local

二、以下步骤在源端操作:

1、检查rsync和xinetd是否安装(CentOS6.5中是以xinetd来管理Rsync服务的)

[root@FN-bdc-qzj ~]# rpm -qa | grep rsync
rsync-3.0.6-9.el6_4.1.x86_64
[root@FN-bdc-qzj ~]# rpm -qa | grep xinetd
xinetd-2.3.14-39.el6_4.x86_64
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
:wq! #保存退出
/etc/init.d/xinetd start #启动

2、创建密码文件

vi /etc/passwd.txt #编辑文件,添加以下内容
limis #对应上面源端的密码
:wq! #保存退出
chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可

3、测试同步

rsync -avH --port=873 --progress /opt/appdata/ limis@10.9.141.233::appdata --password-file=/etc/passwd.txt

三、以下步骤为安装sersync工具,实时触发rsync进行同步

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

    ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
    -rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
    -rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
    -rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches

备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

2、修改inotify默认参数(inotify默认内核参数值太小)

vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出

sysctl -p #使参数立即生效

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现”* Event Queue Overflow *“错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值

3、安装sersync

上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下
cd /usr/local/src
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz #解压
mv GNU-Linux-x86/* /usr/local/sersync #移动目录到/usr/local/sersync

4、配置sersync
cd /usr/local/sersync #进入sersync安装目录
cp confxml.xml confxml.xml-bak #备份原文件
vi confxml.xml #编辑,修改下面的代码

[root@FN-bdc-qzj ~]# cat /usr/local/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="/opt/appdata">
            <remote ip="10.9.141.233" name="appdata"/>
            <!--<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="limis" passwordfile="/etc/passwd.txt"/>
            <userDefinedPort start="false" port="873"/><!-- port=873 -->
            <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>
[root@FN-bdc-qzj ~]# cat /usr/local/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="/opt/appdata">
            <remote ip="10.9.141.233" name="appdata"/>
            <!--<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="limis" passwordfile="/etc/passwd.txt"/>
            <userDefinedPort start="false" port="873"/><!-- port=873 -->
            <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>
[root@FN-bdc-qzj ~]# 
需修改的参数说明:
localpath watch="/opt/appdata"#源服务器同步目录
remote ip="10.9.141.233"#目标服务器IP地址
name="appdata"#目标服务器rsync同步目录模块名称
users="limis"#目标服务器rsync同步用户名
passwordfile="/etc/passwd.txt"#目标服务器rsync同步用户的密码在源服务器的存放路径
failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录
crontab start="false" schedule="600" #如设置为true,则每隔600分钟执行一次全盘同步

5、设置sersync监控开机自动执行

vi /etc/rc.d/rc.local #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本
:wq! #保存退出

6、添加脚本监控sersync是否正常运行

vi /home/crontab/check_sersync.sh #编辑,添加以下代码
#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限
vi /etc/crontab #编辑,在最后添加下面一行
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1 #每隔5分钟执行一次脚本

service crond reload #重新加载服务

7、开启实时同步
执行脚本check_sersync.sh

可以在源端touch一个文件,并在目标端进行查看,然后更新、删除,并观察目标端是否都可以正常实时同步。

至此,Linux下Rsync+sersync实现数据实时同步完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值