在上篇文章ssh无password登陆server的基础之上。能够利用rsync + Inotify 在多server间实现文件自己主动同步。

例如以下測试机基于三台server做的。内网IP分别例如以下:

172.16.3.91    (主机)

172.16.3.92 (备份机1)

172.16.3.89 (备份机2)

如今想对主机上的/opt/sites/yutian_project文件夹下相关文件的不论什么操作同步到2台备份机上。

1.安装rsync

在三台机器上分别检查是否安装了rsync

[root@rs-1 ~]# rsync --version

rsync  version 2.6.8  protocol version 29

Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.

<http://rsync.samba.org/>

Capabilities: 64-bit files, socketpairs, hard links, ACLs, xattrs, symlinks, batchfiles,

inplace, IPv6, 64-bit system inums, 64-bit internal inums

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you

are welcome to redistribute it under certain conditions.  See the GNU

General Public Licence for details.

若没有安装,安装下。因为安装过程比較简单,就不介绍了。

1.查看内核是否支持Inotify特性.

Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持。通过Inotify能够监控文件系统中加入、删除。改动、移动等各种细微事件,利用这个内核接口,第三方软件就能够监控文件系统下文件的各种变化情况,而inotify-tools就是这种一个第三方软件。

[root@rs-1 ~]# ll /proc/sys/fs/inotify

total 0

-rw-r--r-- 1 root root 0 May  8 08:20 max_queued_events

-rw-r--r-- 1 root root 0 May  8 08:20 max_user_instances

-rw-r--r-- 1 root root 0 May  8 08:20 max_user_watches

能看到这个三个文件,说明是默认支持lnotify特性的。

2.安装inotify-tools

下载地址:http://sourceforge.net/projects/inotify-tools/

下载之后编译安装

[vagrant@rs-1 download]$ tar -zxvf inotify-tools-3.13.tar.gz

[vagrant@rs-1 inotify-tools-3.13]$ ./configure

[vagrant@rs-1 inotify-tools-3.13]$ make

[vagrant@rs-1 inotify-tools-3.13]$ sudo make install


安装之后生成例如以下2个命令

[vagrant@rs-1 inotify-tools-3.13]$ inotifywa

inotifywait   inotifywatch

如今编写同步shell脚本

[vagrant@rs-1 work]$ vi inotify_rsync_multl.sh

#!/bin/sh

#set -x

#var

src="/opt/sites/yutian_project/apps /opt/sites/yutian_project/statics /opt/sites/yutian_project/templates"

des_ip="172.16.3.92 172.16.3.89"

#function

inotify_fun ()

{

/usr/local/bin/inotifywait -mrq -e modify,delete,create,move $1 | while read time file

do

for ip in $des_ip

do

echo "`date +%Y%m%d-%T`: rsync -avzq --delete --progress $1 $ip:/opt/sites/yutian_project"

rsync -avzq --exclude=logs/* --delete --progress $1 $ip:/opt/sites/yutian_project/

echo

done

done

}

#main

for a in $src

do

inotify_fun $a &

done

运行inotify_rsync_multi.sh就能够了。

如今主机上的相应文件夹上有不论什么操作,就会同步到备份机上。

rsync + inotify 打造多server间文件实时同步的更多相关文章

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

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

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

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

  3. rsync+inotify-tools文件实时同步

    rsync+inotify-tools文件实时同步案例 全量备份 Linux下Rsync+sersync实现数据实时同步完成. 增量备份 纯粹的使用rsync做单向同步时,rsync的守护进程是运行在 ...

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

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

  5. Centos 6.5 rsync+inotify 两台服务器文件实时同步

    rsync和inotify是什么我这里就不在介绍了,有专门的文章介绍这两个工具. 1.两台服务器IP地址分别为: 源服务器:192.168.1.2 目标服务器:192.168.1.3 @todo:从源 ...

  6. (转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

    Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80 ...

  7. rsync+inotify百万级文件实时同步

    实验环境:Centos7.4 目的:将源服务器的文件实时同步至目标服务器 源服务器:10.11.1.107 目标服务器:10.11.1.106 分别在两个节点安装rsync yum -y instal ...

  8. centos文件实时同步inotify+rsync

    我的应用场景是重要文件备份 端口:873,备份端打开即可 下载地址:https://rsync.samba.org/ftp/rsync/src/ 服务端和客户端要保持版本一致 网盘链接:https:/ ...

  9. sersync+rsync实现服务器文件实时同步

    sersync+rsync实现服务器文件实时同步 一.为什么要用rsync+sersync架构? 1.sersync是基于inotify开发的,类似于inotify-tools的工具 2.sersyn ...

随机推荐

  1. ASP.NET MVC5 之路由器

    这篇博客介绍的很详细 http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_route_1.html

  2. [转]Linux finger命令

    转自:http://os.51cto.com/art/201003/186354.htm Linux finger命令是系统管理员的必备命令之一,他可以清楚的告诉管理员有多少用户在同时使用他所管理的L ...

  3. 解决Sql中DIstinct与Order By共同使用的冲突问题

    1.需求场景: 需要把最新更新文章的前五名作者展示出来. 2.解决问题第一步: select top 5 creator from table order by updateDate desc 结果: ...

  4. 初学layer

    canvas是支持图层layer渲染这种技术的,canvas默认就有一个layer,当我们平时调用canvas的各种drawXXX()方法时,其实是把所有的东西都绘制到canvas这个默认的layer ...

  5. PHP面相对象中的重载与重写

    重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现.Overloaded的方法是可以改变返回值的类型.也就是说,重载的返回值类型可以相同也可 ...

  6. JS——缓动动画

    核心思想: (1)相对于匀速移动,盒子每次移动的步长都是变化的,公式:盒子位置=盒子本身位置+(目标位置-盒子本身位置)/10 (2)在盒子位置与目标距离小于10px时,其步长必然是小数,又由于off ...

  7. CSS——border

    表格细线: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. SSH整合框架+mysql简单的实现

    SSH整合框架+mysql简单的实现 1. 框架整合原理: struts2整合Spring 两种: 一种struts2自己创建Action,自动装配Service : 一种 将Action交给Spri ...

  9. 15个最受欢迎的Python开源框架(转)

    原文地址:http://blog.jobbole.com/72306/ Django: Python Web应用开发框架 Django 应该是最出名的Python框架,GAE甚至Erlang都有框架受 ...

  10. Linux内存压力测试-memtester工具

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...