免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1334 | 回复: 0
打印 上一主题 下一主题

inotify + rsync实现linux文件实时同步,使用触发同步机制 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-13 15:42 |只看该作者 |倒序浏览

                                                                                                                                                        公司一套系统的同步使用的donotify,不能实现子目录的实时同步,通过查资料,发现inotify可以实现子目录的实时同步,以下为笔记。
一、Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
inotify 可以监视的文件系统事件包括:IN_ACCESS,即文件被访问IN_MODIFY,文件被 writeIN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等IN_CLOSE_WRITE,可写文件被 closeIN_CLOSE_NOWRITE,不可写文件被 closeIN_OPEN,文件被 openIN_MOVED_FROM,文件被移走,如 mvIN_MOVED_TO,文件被移来,如 mv、cpIN_CREATE,创建新文件IN_DELETE,文件被删除,如 rmIN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己IN_UNMOUNT,宿主文件系统被 umountIN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)注:上面所说的文件也包括目录。 
二、为能在shell下使用inotify特性,需要安装inotify-tools1、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/
编译安装./configuremakemake install完成后,注意查看manpage,man inotify 、 man inotifywait
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  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):inotifywaitThis 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  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               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.  If omitted, 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/inotifytotal 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.关于递归:inotifywaitThis 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.
3.使用:#!/bin/shsrc=/opt/webmaildes=/tmpip=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 "${file} was rsynced"                echo "---------------------------------------------------------------------------"        done
说明:        当文件系统发现指定目录下有如上的条件的时候就触发相应的指令,是一种主动告之的而非我用循环比较目录下的文件的异动,该程序在运行时,更改目录内的文件时系统内核会发送一个信号,这个信号会触发运行rsync命令,这时会同步源目录和目标目录。        --timefmt:指定输出时的输出格式          --format:  '%T %w%f'指定输出的格式,上面的输出类似于:12/10/08 06:34 /opt/webmail/dovecot-1.1.2/src/test/1
脚本,同步到多台主机:

       
        文件:rsync-inotify.tar.gz
        大小:1KB
        下载:
下载
       
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/32831/showart_1289758.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP