`

[转]inotify + rsync实现linux文件实时同步

 
阅读更多
公司一套系统的同步使用的donotify,不能实现子目录的实时同步,通过查资料,发现inotify可以实现子目录的实时同步,以下为笔记。

一、介绍
Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。

inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。


二、为能在shell下使用inotify特性,需要安装inotify-tools

1、inotify-tools:The general purpose of this package is to allow inotify's features to be used from within shell scripts.

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

编译安装
./configure
make
make install
完成后,注意查看manpage,man inotify 、 man inotifywait


  • inotifywait仅执行阻塞,等待 inotify 事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在 shell 脚本中使用inotifywait
  • inotifywatch收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。

  • 2、inotify的系统相关参数:
    /proc interfaces
    The following interfaces can be used to limit the amount of kernel memory consumed by inotify:

    /proc/sys/fs/inotify/max_queued_events
    The value in this file is used when an application calls inotify_init(2) to set an upper limit on thenumber of events that can be queued to the corresponding inotify instance. Events in excess of thislimit are dropped, but an IN_Q_OVERFLOW event is always generated.

    /proc/sys/fs/inotify/max_user_instances
    This specifies an upper limit on the number of inotify instances that can be created per real user ID.

    /proc/sys/fs/inotify/max_user_watches
    This specifies a limit on the number of watches that can be associated with each inotify instance.


    3、inotifywait 相关的参数(更多,查看manpage):
    inotifywait
    This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entiredirectory trees.
    -m, --monitor
    Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs.
    -r, --recursive
    Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not

    traversed. Newly created subdirectories will also be watched.
    -q, --quiet
    If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
    -e <event>, --event <event>
    Listen for specific event(s) only. The events which can be listened for are listed in the EVENTS section. This option can be specified more than once. Ifomitted, all events are listened for. use“,”separate multi events


    三、使用
    1.查看是否支持inotify,从kernel 2.6.13开始正式并入内核,RHEL5已经支持。
    看看是否有 /proc/sys/fs/inotify/目录,以确定内核是否支持inotify
    [root@RHEL5 Rsync]# ll /proc/sys/fs/inotify
    total 0
    -rw-r--r-- 1 root root 0 Oct 9 09:36 max_queued_events
    -rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_instances
    -rw-r--r-- 1 root root 0 Oct 9 09:36 max_user_watches

    2.关于递归:
    inotifywait
    This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entiredirectory trees.


    3.使用:
    #!/bin/sh
    src=/opt/webmail
    des=/tmp
    ip=192.168.7.192

    /usr/local/bin/inotifywait-mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' /
    -e modify,delete,create,attrib /
    ${src} /
    | while read file
    do
    rsync -avz --delete --progress ${src} root@${ip}:${des} &&
    echo "${src} was rsynced"
    echo "---------------------------------------------------------------------------"
    done
    注:
    当要排出同步某个目录时,为rsync添加--exculde=PATTERN参数,注意,路径是相对路径。详细查看man rsync
    当要排除都某个目录的事件监控的处理时,为inotifywait添加--exclude或--excludei参数。详细查看man inotifywait

    另:
    /usr/local/bin/inotifywait-mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' /
    -e modify,delete,create,attrib /
    ${src} /
    上面的命令返回的值类似于:
    10/03/09 15:31 /wwwpic/1
    这3个返回值做为参数传给read,关于此处,有人是这样写的:
    inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
    细化了返回值。



    说明: 当文件系统发现指定目录下有如上的条件的时候就触发相应的指令,是一种主动告之的而非我用循环比较目录下的文件的异动,该程序在运行时,更改目录内的文件时系统内核会发送一个信号,这个信号会触发运行rsync命令,这时会同步源目录和目标目录。
    --timefmt:指定输出时的输出格式
    --format: '%T %w%f'指定输出的格式,上面的输出类似于:12/10/08 06:34 /opt/webmail/dovecot-1.1.2/src/test/1


    小脚本,同步到多台主机:
    文件: inotify_rsync.tar.gz
    大小: 1KB
    下载: 下载



    参考:




    关于减少rsync的遍历,未完:
    考虑到被监测的目录每次有一下时间时都会触发rsync,
    modify,delete,create,move
    每次rsync都会遍历源目录,当被监测目录内文件特别多时,会造成系统资源的严重消耗,所以,
    让rsync每次只同步修改的文件。

    因为,如果从监控目录mv走一个目录,那么rsync只会报告找不到你移走的目录而无法删除备份机的应该删除的目录。

    所以,对于删除这个事件,没有办法了,只能同步源目录了。

    将事件分为两部分,modify,create,move事件,触发rsync,只同步修改了的文件。

    delete事件,同步整个源目录。

    关于脚本内容的一些说明:
    rsync.conf里的目录格式一定要注意,没有最后的“/"

    boot.sh
    对于rsync命令的目标地址,是由两部分组成的:
    1、rsync.conf里的dest
    des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 `
    2、被修改了的文件的完全路径,去掉源目录部分,去掉被修改的文件的文件名。
    mb=`echo $file|awk -F "/" '{NF=NF-1;OFS="/";print $0}'|sed "s#$src##g"`




    boot.sh
    #######################################################################################3
    basedir=/EBS/rsync
    destNum=`grep -c '^dest' ${basedir}/rsync.conf`
    src=`grep 'local directory=' ${basedir}/rsync.conf|cut -d '=' -f 2`
    des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 `
    #src_fin=`grep 'local directory=' ${basedir}/rsync.conf|awk -F "/" '{print $NF}'`
    #des_fin=$des/$src_fin


    #
    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' /
    -e modify,create,move ${src} | while read date time file
    do
    for i in $des
    do
    #echo $src
    echo $file
    mb=`echo $file|awk -F "/" '{NF=NF-1;OFS="/";print $0}'|sed "s#$src##g"`

    des_fin=$i$mb
    echo $des_fin
    echo rsync -avz --delete --progress $file $des_fin
    done
    done

    ############################################################################################

    rsync。conf
    local directory=/EBS/www/projects
    #dest_here


    相关参考:
    分享到:
    评论

    相关推荐

      Inotify+Rsync实现linux文件实时同步(网上copy他人文章)

      **Inotify+Rsync实现文件同步** 结合Inotify和Rsync,我们可以构建一个实时文件同步系统。当Inotify检测到文件变化时,会触发Rsync进行同步操作。以下是实现步骤: 1. **安装Inotify-tools和Rsync** 在Linux系统中...

      inotify+rsync实时远程同步包

      本文将深入探讨inotify和rsync这两个工具,以及它们如何协同工作实现文件系统的实时远程同步。 首先,让我们了解inotify。inotify是Linux内核提供的一种文件系统事件监控机制,能够实时跟踪文件系统的变化。它允许...

      linux下Rsync+sersync实现文件数据实时同步

      `sersync`是一个基于`inotify`的文件同步工具,专为Linux设计,它能监听文件系统的变动并实时同步。与Rsync相比,sersync更专注于实时性和自动化,尤其适合监控大量小文件的变化。 1. **实时性**:sersync通过监听...

      rsync+inotify实现linux服务器之间文件实时+双向同步.docx

      rsync+inotify是Linux系统中一种常用的实时文件同步方案,尤其适用于服务器之间的数据备份和同步。rsync是一个高效的数据同步工具,它可以快速地比较并传输文件和目录,而inotify则是Linux内核提供的一种文件系统...

      inotify+rsync、sersync 实时备份1

      本文主要讨论如何利用Linux下的inotify和rsync或sersync工具实现文件系统的实时备份,以克服定时备份的不足。 **1. inotify介绍** inotify是Linux内核从2.6.13版本开始引入的一个文件系统事件监控机制,它提供了一...

      inotify+rsync 远程备份.7z

      标题 "inotify+rsync 远程备份.7z" 提及了两个关键工具:inotify和rsync,它们在Linux系统中常用于文件监控和数据同步。在远程备份场景中,这两个工具结合使用可以实现高效、实时的备份策略。 **inotify简介:** ...

      用inotify+rsrnc实现linux文件及目录的监控和同步

      结合`rsync`工具,我们可以实现文件和目录的实时同步,以达到数据备份或分布式系统中的数据一致性。本篇将详细讲解如何使用`inotify`和`rsync`来实现这一功能。 首先,`inotify`提供了多种事件类型,包括文件的创建...

      rsync+inotify实现实时同步

      rsync+inotify实现实时同步 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同 步数据时,需要扫描所有文件后进行比对,进行差...

      3、安装Inotify+rsync并测试.doc

      标题中的“3、安装Inotify+rsync并测试.doc”指的是一个关于在Linux系统中集成Inotify和rsync工具的教程或文档,可能是为了实现文件系统的实时同步和备份。Inotify是Linux内核提供的一种文件系统事件监控机制,而...

      CentOS下用rsync+inotify实现实时同步

      CentOS 7下用rsync+inotify实现实时同步可以实现文件之间的同步备份。下面是相关知识点的详细解释: 1. rsync简介 rsync是一种快速、可靠、安全的文件同步工具。它可以在本地或远程主机之间同步文件,具有高效、...

      linux rsync及inotify实时同步

      在 Linux 系统中,进行数据备份和实时同步是非常重要的,rsync 及 inotify 是两种常用的技术,能够实现实时同步。本文将详细介绍如何使用 rsync 及 inotify 实现实时同步。 rsync 概述 rsync 是一种快速的本地和...

      rsync+inotify实现服务器之间目录文件实时同步

      ### rsync+inotify 实现服务器之间目录文件实时同步 #### 软件简介与应用场景 **rsync** 是一种高效且广泛使用的文件同步工具,主要用于远程数据备份、镜像和迁移场景。相较于传统的 `cp` 和 `tar` 备份方式,...

      rsync+inotify实时同步

      标题《rsync+inotify实时同步》涉及的内容主要是如何通过组合rsync和inotify两个工具,实现文件或目录的实时同步功能。rsync是一个功能强大的文件传输工具,支持文件的快速同步、更新、备份等操作,而inotify则是...

      rsync+inotify开机自启动实时同步增量备份

      rsync+inotify开机自启动实时同步增量备份是通过使用rsync与inotify结合来实现Linux系统中文件的实时同步和增量备份。rsync是一个快速且灵活的文件传输工具,而inotify则是一个内核特性,它可以监控文件系统的变化。...

      rsync+inotify.zip

      将rsync与inotify结合使用,可以实现高效且实时的文件同步策略。例如,可以在一个目录上设置inotify监听,一旦有文件变动,就触发rsync进行同步。这样的组合在Web开发中特别有用,可以实现实时编译、部署静态资源,...

      rsync+inotify 实时远程同步

      Inotify 是一个 Linux 内核提供的文件系统事件监控机制,它可以实时监控文件系统的变化,从而实现实时同步。 本文将详细介绍如何使用 Rsync + inotify 实现实时远程同步。我们将在服务器之间建立 Rsync 服务器,...

    Global site tag (gtag.js) - Google Analytics