Sersync+Rsync实现触发式文件同步
背景
通常我们在服务器上使用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
Sersync+Rsync实现触发式文件同步的更多相关文章
- Sersync实现触发式文件同步 替代inotify和rsync
Sersync实现触发式文件同步 替代inotify和rsync Pyinotify是一个Python模块,用来监测文件系统的变化. Pyinotify依赖于Linux内核的功能—inotify(内核 ...
- [sersync+rsync] centos6.5 远程文件同步部署记录
针对本地文件的修改,自动同步到远程文件夹,远程备份很方面.研究了下大家的主流同步方案一般是 rsync+inotify和rsync+sersync, 我这里使用sersync的方案,当然大部分都是参照 ...
- Rsync+lsync实现触发式实时同步
使用rsync+lsync实现触发式实时同步 服务器信息 centos6.5 主:192.168.5.4 搭建lsync 从:192.168.5.3 搭建rsync 1.1 从服务器设置 # yum ...
- rsync+sersync+inotify实现服务器间文件同步之一
rsync+sersync+inotify实现服务器间文件同步之一:rsync安装配置 2013年12月14日 ⁄ Linux管理, 服务器集群技术 ⁄ 共 4925字 ⁄ rsync+sersync ...
- Linux 之 rsync实现服务器的文件同步
rsync实现服务器的文件同步 参考文献链接: 一.rsync实现负载均衡集群文件同步,搭建线上测试部署环境 二.rsync. 三.rsync常见错误. 四.rsync 安装使用详解. 环境部署: 服 ...
- 文件触发式实时同步 Rsync+Sersync Rsync+Inotify-tools
一.概述 1.Rsync+Sersync 是什么? 1)Sersync使用c++编写基于inotify开发的触发机制: 2)Sersync可以监控所监听的目录发生的变化(包括新建.修改.删除),具体到 ...
- 使用inotify+rsync实现服务器间文件同步
1. rsync 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.它使用所谓的“Rsync演算法”来使本地和远程两个主机之间的文件达到 ...
- Linux下利用rsync实现多服务器文件同步
windows做为文件服务器,使用rsync的windows服务版本,然后配置好就可以了.需要的朋友可以参考下. windows做为文件服务器,使用rsync的windows服务版本:cwRsyncS ...
- Inotify+Rsync实现Linux服务器文件同步
做这个功能的时候遇到了好多坑,在此感谢一下这篇博客 http://kerry.blog.51cto.com/172631/734087/ ,大家参照这篇博客就能实现该功能. 另外如果想详细了解一下的 ...
随机推荐
- python实战第一天-paramiko模块并练习
操作系统 Ubuntu 15.10 IDE & editor JetBrains PyCharm 5.0.2 ipython3 Python版本 python-3.4.3 安装paramiko ...
- NYOJ--STL--擅长排列的小明(强大的string :: iterator 和next_permutation)
NYOJ--STL--擅长排列的小明 #include <iostream> #include <string> #include <algorithm> usin ...
- Java常用文件操作-2
上篇文章记录了常用的文件操作,这里记录下通过SSH服务器操作Linux服务器的指定路径下的文件. 这里用到了第三方jar包 jsch-0.1.53.jar, jsch-api 1.删除服务器上指定路径 ...
- gitbook初体验
前言 在早些天看sec-wiki的时候,看到有同学做了一个ctf-wiki来介绍CTF,让CTF新手能够快速入门,做成了电子书的样子,放在github上面.页面精美.简洁,让人爱不惜手. 想着自己也能 ...
- UUID.randomUUID().toString() 的作用
public static String createNewId(){ return UUID.randomUUID().toString() ; } UUID.randomUUID().toStri ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- ArrayList 和 LinkedList的执行效率比较
一.概念: 一般我们都知道ArrayList* 由一个数组后推得到的 List.作为一个常规用途的对象容器使用,用于替换原先的 Vector.允许我们快速访问元素,但在从列表中部插入和删除元素时,速度 ...
- C语言库函数探究
1.strlen()求字符串长度 //模拟实现strlen函数 #include<stdio.h> #include<stdlib.h> #include<string. ...
- DVWA笔记之三:CSRF
CSRF与XSS不同,它称为跨站请求伪造,它是利用其他页面的恶意脚本来加载访问或操作存在CSRF的漏洞的可信网站. 1.Low级别 核心代码如下: <?php if( isset( $_GET ...
- MSPointerEvent属性
MSPointerEvent属性 属性 描述 hwTimestamp 创建事件的时间(ms) isPrimary 标识该指针是不是主指针 pointerId 指针的唯一ID(类似于触摸事件的标识符) ...