inotify+rsync实现实时同步

1.1 什么是实时同步:如何实现实时同步

  1. 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化
  2. 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上

1.2 实现实时同步的方法

   inotify+rsync 方式实现数据同步

   sersync 方式实现实时数据同步 详情参照:http://www.cnblogs.com/clsn/p/7707828.html

1.2.1 实时同步原理介绍

 

1.3 inotify+rsync 方式实现数据同步

1.3.1 Inotify简介

  Inotify是一种强大的,细粒度的。异步的文件系统事件监控机制,linux内核从2.6.13起,加入了 Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 正是实施这样监控的软件。国人周洋在金山公司也开发了类似的实时同步软件sersync。

提示信息:

sersync软件实际上就是在 inotify软件基础上进行开发的,功能要更加强大些 ,多了定时重传机制,过滤机制了提供接口做 CDN,支持多线程橾作。

  Inotify实际是一种事件驱动机制,它为应用程序监控文件系统事件提供了实时响应事件的机制,而无须通过诸如cron等的轮询机制来获取事件。cron等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界事件机制相符合。

  inotify的实现有几款软件:

  inotify-tools,sersync,lrsyncd

1.3.2 inotify+rsync使用方式

  inotify 对同步数据目录信息的监控

  rsync  完成对数据信息的实时同步

  利用脚本进行结合

1.4 部署inotify软件的前提

[root@server01 ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 1月  13 15:58 max_queued_events
-rw-r--r-- 1 root root 0 1月  13 15:58 max_user_instances
-rw-r--r-- 1 root root 0 1月  13 15:58 max_user_watches

1.4.1 三个重要文件的说明 

文件

默认值

作用说明

max_user_watches

8192

设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)

max_user_instances

128

设置每个用户可以运行的inotifywait或inotifywatch命令的进程数

max_queued_events

16384

设置inotify实例事件(event)队列可容纳的事件数量

1.5 inotify软件介绍及参数说明

1.5.1 两种安装方式

  1) yum install -y inotify-tools

  2) 手工编译安装

注:

    YUM 安装需要有epel源

  http://mirrors.aliyun.com

手工编译安装方式需要到github上进行下载软件包

    inotify软件的参考资料链接:

  https://github.com/rvoicilas/inotify-tools/wiki

1.5.2 inotify主要安装的两个软件

inotifywait: (主要)

   在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用

inotifywatch:

  收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。

  说明:在实时实时同步的时候,主要是利用inotifywait对目录进行监控

1.5.3 inotifywait命令参数说明 

参数

含义

-m, --monitor

(重要参数)

Keep listening for events forever.  Without this option, inotifywait will exit after one event is received.

始终保持事件监听。

-d, --daemon

111

-r, --recursive

(重要参数)

Watch all subdirectories of any directories passed as arguments.

递归监控目录数据信息变化

-o, --outfile <file>

Print events to <file> rather than stdout.

打印事件到文件中,相当于标准正确输出

-s, --syslog

Output errors to syslog(3) system log module rather than stderr.

发送错误到syslog相当于标准错误输出

-q, --quiet

(重要参数)

If specified once, the program will be less verbose.  Specifically, it will not state  when  it  has  completed establishing all inotify watches.

输出信息少(只打印事件信息)

--exclude <pattern>

Exclude all events on files matching the extended regular expression <pattern>.

排除文件或目录

--excludei <pattern>

Like --exclude but case insensitive.

排除文件或目录时,不区分大小写

--timefmt <fmt>

(重要参数)

Print using a specified printf-like format string; read the man page for more details.

指定时间输出格式

--format <fmt>

(重要参数)

Print using a specified printf-like formatstring; read the man page for more details.

打印使用指定的输出类似格式字符串;即实际监控输出内容

-e

(重要参数)

Listen for specific event(s).  If omitted, all events are listened for.

指定监听指定的事件,如果省略,表示所有事件都进行监听

以上的信息可以通过 inotifywait --help  获得

1.5.4 -e[参数]  可以指定的事件类型 

事件名称

事件说明

access

file or directory contents were read

文件或目录内容被读取

modify

file or directory contents were writterv

文件或目录内容被写入

attrib

file or directory attributes changed

文件或目录属性改变

close_write

(重要参数)

file or directory closed, after being opened in writeable mode.

文件或目录关闭,在写入模式打开之后关闭的。

close_nowrite

file or directory closed, after being opened in read-only mode.

文件或目录关闭,在只读模式打开之后关闭的

close

file or directory closed, regardless of read/write mode

文件或目录关闭,不管读或是写模式

open

file or directory opened

文件或目录被打开

moved_to

file or directory moved to watched directory

文件或目录被移动到监控的目录中

moved_from

file or directory moved from watched directory

文件或目录被移动从监控的目录中

move

(重要参数)

file or directory moved to or from watched directory

文件或目录不管移动到或是移出监控目录都触发事件

create

(重要参数)

file or directory created within watched directory

文件或目录创建在监控目录中

delete

(重要参数)

file or directory deleted within watched directory

文件或目录被删除在监控目录中

delete_self

file or directory was deleted

文件或目录被删除,目录本身被删除

unmount

file system containing file or directory unmounted

以上的信息可以通过 inotifywait --help  获得

原文地址:https://www.cnblogs.com/liujunjun/p/14272612.html