快速业务通道

Inotify Rsync实现linux文件实时同步

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-04-24
公司一套系统的同步使用的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

   1)inotifywait 仅执行阻塞,等待 inotify 事件.您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等).在 shell 脚本中使用 inotifywait.

   2)inotifywatch 收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次.

  2、inotify的系统相关参数:

Empire CMS,phome.net

  /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 the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit 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 entire directory 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 unl

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号