先介绍一下rsync与inotify。

1、rsync

与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

2、inotify
 Inotify
是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
具体大家可以参照http://www.ibm.com/developerworks/cn/linux/l-ubuntu-inotify/index.html来进行学习。

接下面我们来开始进行rsync与inotify的安装、配置、测试。

下面是2个服务器的结构,分别为主机名、ip、同步的目录,并将2台服务器均是CentOS 6.5发行版本。

Host IP Docoment
Server 192.168.10.58 /tmp
Client 192.168.10.57 /tmp

一、主服务器(Server)

其中主服务器需要安装rsync与inotify,主服务器作为server,向备份服务器client传输文件

1、安装rsync

#安装rsync和xinetd,并配置:
yum install rsync xinetd #配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no 启动xinetd服务:
service xinetd start

2、建立密码认证文件

代码如下:

[root@Server ]# echo "rsync-pwd" >/etc/rsync.passwd
#其中rsync-pwd可以自己设置密码,rsync.passwd名字也可以自己设置 [root@Server]# chmod 600 /etc/rsync.passwd
#无论是为了安全,还是为了避免出现以下错误,密码文件都需要给600权限

3、安装inotify

[root@Server]# cd /usr/src/
[root@Server src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@Server src]# tar zxvf inotify-tools-3.14.tar.gz
[root@Server src]# cd inotify-tools-3.14
[root@Server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@Server inotify-tools-3.14]# make
[root@Server inotify-tools-3.14]# make install

4、创建rsync复制脚本

此项功能主要是将server端的目录/tmp里的内容,如果修改了(无论是添加、修改、删除文件)能够通过inotify监控到,并通过rsync实时的同步给client的/tmp里,下面是通过shell脚本实现的。

#!/bin/bash
host=192.168.1.22
src=/tmp/
des=web
user=webuser
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

注意:建议各位吧rsync的日志放到其他的目录下(非备份目录)。
其中host是client的ip,src是server端要实时监控的目录,des是认证的模块名,需要与client一致,user是建立密码文件里的认证用户。
把这个脚本命名为rsync.sh,放到监控的目录里,比如我的就放到/tmp下面,并给予764权限

[root@Server tmp]# chmod 764 rsync.sh

然后运行这个脚本

[root@Server tmp]# sh /tmp/rsync.sh &

请记住,只有在备份服务器client端的rsync安装并启动rsync之后,在启动rsync.sh脚本,否则有时候会满屏出现:

rsync: failed to connect to 192.168.1.22: Connection refused (111)  
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]

我们还可以把rsync.sh脚本加入到开机启动项里

[root@Server tmp]# echo "/tmp/rsync.sh" >> /etc/rc.local 

二、备份服务器(Client)

1、安装rsync(备份服务器只安装rsync)

#安装rsync和xinetd,并创建目录:
yum install rsync xinetd #配置xinetd:
vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no 启动xinetd服务:
service xinetd start

2、建立用户与密码认证文件

[root@Client]# echo "webuser:rsync-pwd" > /etc/rsync.passwd
#请记住,在server端建立的密码文件,只有密码,没有用户名;而在备份服务端client里建立的密码文件,用户名与密码都有。 [root@Client]# chmod 600 /etc/rsync.passwd
#需要给密码文件600权限

3、建立rsync配置文件

vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web]
path = /tmp/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.10.220
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /usr/local/rsync/rsync.passwd

其中web是server服务端里的认证模块名称,需要与主服务器里的一致,以上的配置我的自己服务器里的配置,以供参考。

4、启动rsync

service xinetd start
chkconfig xinetd on #设置开机自启动 rsync --daemon

三、测试

现在rsync与inotify在server端安装完成,rsync在备份服务器client端也安装完成。接下来就可以来测试一下:

在server里创建个test-rsync文件,看看client是否能收到

[root@Server tmp]# touch test-rsync

再看client端是否有test-rsync文件,同时client端的tmp目录文件是否与server端的文件完全一致。如果一致,则表示已经设置成功。


linux下rsync+inotify实现服务器之间文件实时同步的更多相关文章

  1. linux系统中rsync+inotify实现服务器之间文件实时同步

    最近需要对服务器上的文件实施动态备份,我又不想每次都手动来进行备份,在网上找了挺多资料,发现使用rsync就可以实现,如果想要实现实时同步,还可以使用rsync+inotify组合,本文就是以组合方式 ...

  2. rsync+inotify实现服务器之间文件实时同步--转

    之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行中,由于主服务器在给备份服务器传输的时候,我们的主服务器需要备份的文件是实时.不停的产生的,造成不知道 ...

  3. centos6.5 rsync+inotify实现服务器之间文件实时同步

    1. rsync的优点与不足 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据 ...

  4. rsync+inotify实现主机之间目录实时同步

    原理: rsync:用于跨主机目录同步 inotify:用于监测目录变化 再编写一个触发脚本,一旦inotify检测到目录中内容发生变化,则调用rsync执行同步. rsync服务器的的配置: 因为r ...

  5. Linux服务器间文件实时同步的实现

    使用场景 现有服务器A和服务器B,如果服务器A的指定目录(例如 /home/paul/rsync/ )中的内容发生变更(增删改和属性变更),实时将这些变更同步到服务器B的目标目录中(例如 /home/ ...

  6. rsync+inotify 实现服务器之间目录文件实时同步(转)

    软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...

  7. rsync+inotify实现多台服务器之间数据实时同步

    配置环境 1.操作系统:CentOS6.5-X86_64 2.rsync客户端(rsync+inotify):192.168.200.82 3.rsync服务端:192.168.200.80,192. ...

  8. inotify用法简介及结合rsync实现主机间的文件实时同步

    一.inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系 ...

  9. linux下使用scp在服务器之间拷贝文件 (转载)

    CentOS, 本地服务器,ip: 192.168.1.111Ubuntu, 远程服务器,ip: 192.168.1.112 1.拷贝远程服务器的目录到本地服务器远程服务器192.168.1.112上 ...

随机推荐

  1. DOTWeen 使用

    using UnityEngine; using System.Collections; using DG.Tweening; using UnityEngine.UI; public class T ...

  2. LeetCode OJ 2. Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  3. Android进阶AIDL - 2018年4月14日

    参考:慕课网 --- 最后三集.Android开发艺术探索 1.在AS中创建aidl文件后,要编译一下才会在gen下生成debug文件: 2.AIDL 不支持short类型,常用的数据类型: 3.AI ...

  4. WEB前端问题——img标签的onclick事件无法响应问题【转载】

    一个纠结了一下午的问题,img标签里面的onclick事件无法响应.最终找到了错误原因,是因为img标签的id与onclick事件的方法名相同. 于是接着又测试了一下,发现name名和方法名相同也会导 ...

  5. JAVAWEB 一一 框架整合(SSH,Spring+Struts2+Hibernate IOC/DI AOP声明式事务处理 定时任务)

    package org.springframework.orm.hibernate3; import java.io.Serializable; import java.util.List; impo ...

  6. (转)JPA + SpringData

    jpa + spring data 约定优于配置 convention over configuration http://www.cnblogs.com/crawl/p/7703679.html 原 ...

  7. winform clickonce在线安装

    转 http://swanmsg.blog.sohu.com/162994305.html

  8. 吴裕雄 python 机器学习-KNN算法(1)

    import numpy as np import operator as op from os import listdir def classify0(inX, dataSet, labels, ...

  9. ValueError: too many values to unpack tensorflow

    使用tensorflow 的时候报错: result ,_= sess.run(ops)ValueError: too many values to unpack 其实是ops里面不止一个,返回解压成 ...

  10. 编译模块的Makefile解析

    Makefile # if not defined KERNELRELEASE, command is running from command line,need invoke kbuild sys ...