用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
heiyeshuwu的公告
<br /> 联系方式:<br /><img src="http://heiyeshuwu.blogchina.com/inc/heiyeluren_gmail_com.png" title='Gmail联系我' /><br /><br /> 访问统计: <!-- Start of StatCounter Code --> <a href="http://www.statcounter.com/" target="_blank"><img src="http://c8.statcounter.com/counter.php?sc_project=928741&amp;java=0&amp;security=b86c0c15&amp;invisible=0" alt="free hit counter code" border="0"></a> <!-- End of StatCounter Code --> <br> FeedSky订阅:<br /> <a href="http://feed.feedsky.com/heiyeluren" target="_blank"><image src="http://www.feedsky.com/images/logo.gif" alt="FeedSky订阅" width="80" heigth="24" border="0"/a><br />
文章分类
::eYou::
kevin world
lewis - 老吕
qyb - BT的花
Realzay的blog
叶金荣
天堂地狱鬼-dulao5's Blog
沙漠之周
狐狸糊涂
與子觀化
鞋门
::Yahoo::
glemir’s blog
happy_fish - 分布式文件系统FastDFS
LinZi's Blog
Rainx
stauren
互联网,请记住我 - 162同学的技术博客
冰的河
北冥之鱼-张彪同学
天韵之星
小蚂蚁同学滴测试博客
随网之舞 - kaven的DHTML博客
风雪之隅
::朋友::
【推荐】中文分类网
DDR的博客
kevin world
miky
PHPCup.cn论坛
俺兄弟的blog
冰河的技术博客:心随风动
好旅网
小少的技术博客
无尘居
晋陵路人的Blog
有米啦
李天华同学滴技术博客
沙狐部落
轻量级的editor
::网友::
blankyao同学
Code & Stock.
Hello, Willko
LionD8的Blog
magiclab.cn
MooPHP - 轻量级PHP框架
Phzzy
Xhttpd.cn
张贺同学的博客
技术大牛老余的博客
抚琴居
旋木木同学滴博客
流水孟春
矛盾网
程序人生
花荣老师
邢红瑞的blog
阿健的博客
::友情链接::
世纪软件测试
:PHP博客:
.: Easy style :.
[琴剑楼]
CoolCode.cn
Haohappy的Blog
Hightman
iwind的blog
Javascript开发站
JD Space
Nio's Weblog
Open Source PHP
PHP面对对象
SourceForge.net
trip的专栏
UGIA.cn
windix's blog
Windix's Weblog
一个藏袍
俊麟 Michael`s blog
偶然的blog
刘敏的blog
大龄青年的Blog
廖宇雷的blog
懒猫开始新生活blog
某人的栖息地
王春生的博客
神仙
:牛人blog:
DBA notes
http://blog.csdn.net/tingya/
侯捷网站
孟岩
搜索引擎研究
方舟
王咏刚的BLOG
竹笋炒肉
荣耀
车东[Blog^2]
透明思考
陈硕的Blog
DHTML
DHTMLGoodies
FCKEditor
Google Code
Google Web Toolkit
HTML Goodies
HTML.it
HTMLAre
HTMLdog
JavaScript Kit
jQuery
KindEditor
Prototype
TinyMCE
W3 Schools
Yahoo JavaScript Developer Center
Yahoo! Developer Network
Yahoo! UI Library (YUI)
网页设计师Web标准
Java国内站
ChinaJavaWorld.com技术论坛
IBM developerWorks 中国: Java
Java中文站
Java开源大全
Java爱好者
JR - Java翻译站
J道-JDON
Matrix: 与Java共舞
中国Java开发网
中文java技术网
PHP国内站点
CSDN PHP论坛
Discuz!
FleaPHP
Google--PHP用户组
IBM DeveloperWorks
JavsScript技术讨论
Nirvana Studio
OpenPHP.cn
PHPChina
TiM Club
中文 PFC 1.0 手册--PHP5的开发包
中文 PFC 1.0 手册--PHP5的开发包
中文PHP网
太平洋--PHP开发区
爱MySQL
超越PHP
PHP国外站点
ADOdb
Agavi Framework
Cake PHP
MySQL Performance Blog
MySQL Performance Blog
Nonaweb
PEAR
PECL
PECL Windows
PHP Builder
PHP Classes
PHP Classes
PHP New Download
PHP Security Consortium
php.MVC
php.MVC
PHPkitchen(OO & MVC)
phpPatterns
PHP国外图书下载
smart template
Smarty
SourceForge.net
Symfony Framework
Zend
Zend Framework
Unix C/C++
Free Gentux
周立发的blog(Linux C)
Unix/Linux
BSD智库
ChinaUnix
FreeBSDChina
FreeLAMP
IBM开发者Linux专区
Linux Byte
LinuxKit
LinuxTS
Linux伊甸园
Linux技术中坚站
Linux非常空间
Love Unix
NetBSD&OpenBSD中文用户组
NetBSD中国社区
Oracle中国用户讨论组
OurLinux
Unix中文
Unix中文
Unix中文宝库
中国Linux公社
中国Unix用户技术论坛
中文FreeBSD用户组
永远的Unix
炎黄角马
程序设计
CSDN
IBM开发者中心
Microsoft TechNet: 主页
MSDN 中文网站
PHP中文站
Sun技术社区
中国IT认证实验室--企业应用技术
中国协议分析网
喜悦国际村
太平洋电脑网---开发特区
实用网站
veBook(国外大量免费图书下载网站)
Whois.net
中国Web信息博物馆
中国互联网络信息中心whois查询
服务器系统信息查看
网络安全
AnySide.com
CGI Secutiry
K-OTik Security Monitoring
Linux Security
Packet Storm Security
PHP Secure
RFC中文文档索引
Safemode.org
SecuriTeam.com
Security Corporation
SecurityFocus
SecurityTracker
Zone-h (区域黑客,每天公布各国被黑的网站)
中华安全网
中国信息安全组织
国家计算机网络应急处理中心
安全天使
安全焦点
幻影旅团
绿盟科技
网络安全评估中心(cnns )
在线手册
Apache2.0中文文档
Beyond Linux From Scratch
Debian参考手册
FreeBSD Porter 手册
FreeBSD使用手册
Linux C函数中文参考手册
MySQL 4.1.0 中文参考手册
NetBSD在线手册
OpenBSD在线FAQ
PHP ADODB 1.99版手册中文翻译(Tripc)
PHP中文手册(国内)
PHP中文手册(国外)
PostgreSQL中文文档
Red Hat Linux 9入门指南
Red Hat Linux 9安装指南
Red Hat Linux 9定制手册
中国OSS技术手册中心
技术文档手册中心-ChinaUnix
存档

转载  [转]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  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  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.  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/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 entire directory 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


    相关参考:

    发表于 @ 2009年05月06日 22:57:00 | 评论( loading... ) | 编辑| 举报| 收藏

    旧一篇:[转]Facebook 如何管理150亿张照片 | 新一篇:[分享]Gearman - 分布式远程过程处理框架

    • 发表评论
    • 评论内容:
    •  
    Copyright © heiyeshuwu
    Powered by CSDN Blog