阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Sersync+Rsync实现触发式文件同步

133次阅读
没有评论

共计 3631 个字符,预计需要花费 10 分钟才能阅读完成。

背景

通常我们在服务器上使用 rsync 加上 crontab 来定时地完成一些同步、备份文件的任务。随着业务和应用需求的不断扩大、实时性要求越来越高。一般 rsync 是通过校验所有文件后,进行差量同步,如果文件量十分庞大,那么 rsync 进行校验的过程也是十分耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync 不能实时的去监测、同步数据,虽然它可以通过 crontab 方式进行触 发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。而 Sersync+Rsync 的组合能够较好地解决这种问题。

Sersync 介绍

1、sersync 是使用 c ++ 编写,而且对 linux 系统文 件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合 rsync 同步的时候,节省了运行时耗和网络资源。因此更快。
2、sersync 配置起来很简单,其中 bin 目录下已经有基本上静态编译的 2 进制文件,配合 bin 目录下的 xml 配置文件直接使用即可。
3、另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状 态。
4、本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每 10 个小时对同步失败的文件重新同步。
5、本项目自带 crontab 功能,只需在 xml 配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置 crontab 功能。
6、本项目 socket 与 http 插件扩展,满足您二次开发的需要。

实战过程

一、服务器环境

服务端:172.16.57.26 CentOS6.7 rsync-server 接收文件
客户端:172.16.57.25 centos6.7 sersync+rsync-client 发送文件

二、服务端安装 rsync-server

1、安装 rsync

# rpm -qa | grep rsync #查看 rsync 是否已经安装,如果没有安装,yum install 直接安装即可

2、使用 xinetd 方式启动 rsync

# vim /etc/xinetd.d/rsync #修改 disable = no,flags = IPv4

3、修改 rsync 配置文件

# mkdir /etc/rsyncd
# vim /etc/rsyncd/rsyncd.conf #修改配置文件如下

# GLOBAL OPTIONS
motd file=/etc/motd
port=873
pid file=/var/run/rsyncd.pid
lock file = /var/lock/rsyncd
log file=/var/log/rsyncd
transfer logging = yes
log format = [op]:%o [ip]:%a [module]:%m [path]:%P [file]:%f [size]:%l
syslog facility=daemon
max connections=100

[recv]
comment = "recv data from 57.25"
path = /opt/rsync_data/recv # 这边的目录的宿主要改为 apprun,在这里同步过程中使用的是普通账户 apprun
list = yes
use chroot = yes
uid = apprun
gid = apprun
read only = no
write only = no
exclude =
include =
auth users = rsync
secrets file = /etc/rsyncd/rsyncd.secrets
strict modes = yes
hosts allow = 172.16.57.25
hosts deny = *

# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf

4、建立用户认证文件

# vim /etc/rsyncd/rsyncd.secrets
rsync:111111     # 格式   用户名: 口令
#chmod 600 /etc/rsyncd/rsyncd.secrets #权限设为 600,否则启动会报错

5、启动 rsync

# /etc/init.d/xinetd start
# netstat -tpln | grep 873 #查看 873 端口是否已经在监听了

三、客户端安装 sersync+rsync-client

1、安装 rsync,和服务端一样,没有安装的话 yum install 安装
2、安装 sersync

# tar xzvf sersync2.5_64bit_binary_stable_final.tar.gz
# mv GNU-Linux-x86 /opt/programs/sersync #解压并拷贝到安装目录

3、配置 sersync

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

    <sersync>
    <localpath watch="/opt/rsync_data/send"> # 监控目录,一旦本地目录有文件变化,将同步到服务端
        <remote ip="172.16.57.26" name="recv"/># 服务端 ip 和同步模块
    </localpath>
    <rsync>
        <commonParams params="-artuz"/> #rsync 同步参数
        <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/> #服务端认证密码
        <userDefinedPort start="false" 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>
</head>

4、服务端密码认证

# vim /etc/rsync.pas #在相应的目录下配置身份验证文件,里面输入服务端的密码,并 chmod 600
# chmod 600 /etc/rsync.pas

5、启动 sersync

# ./sersync2 -d -o confxml.xml

四、测试认证

在客户端下监控目录 /opt/rsync_data/send 下添加文件或者删除,服务端的接受目录都会实时地进行更新。

在此例中,服务器 iptables 和 selinux 均处于关闭状态。

note: 这种方法同步文件的时候,同步文件的数量如果很多,可能会有部分文件在同步过程中缺失。查阅相关资料后,找到了如下的解决方案。由于本例中,使用的是 xinetd 方式启动的 rsync 服务,在 xinetd 的配置文件中,修改几个参数如下:

# vim /etc/xinetd.conf
修改几个参数:cps             = 500 30
 instances       = UNLIMITED
 per_source      = UNLIMITED

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-09/146876.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3631字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中