zoukankan      html  css  js  c++  java
  • rsync+inotify 实现实时同步

    inotify:这个可以监控文件系统中的添加,修改,删除,移动等事件

    inotify的特性需要linux内核2.6.13以上的支持

    [root@test1 inotify-tools-3.13]# uname -r
    2.6.32-71.el6.i686  #支持 还需要看看是否安装了inotify
    [root@test1 inotify-tools-3.13]# ll /proc/sys/fs/inotify/
    total 0#这个里面的东西是inotify的接口用于限制内存大小
    -rw-r--r-- 1 root root 0 Aug 18 19:57 max_queued_events  #表示调用init时分配到instance中排队的event的最大数目,超出就会被丢弃
    -rw-r--r-- 1 root root 0 Aug 18 19:57 max_user_instances #表示一个real id user 可创建instance的最大数目
    -rw-r--r-- 1 root root 0 Aug 18 19:57 max_user_watches  #表示inotify可监控的最大目录文件上限,如果目录文件过多可以把这个值调大点

    光有inotify还不够,还要安装inotify-tools

    [root@test1 ~]# wget http://sourceforge.net/projects/inotify-tools/files/latest/download/inotify-tools-3.13.tar.gz
    [root@test1 ~]# tar zxf inotify-tools-3.13.tar.gz
    [root@test1 ~]# cd inotify-tools-3.13
    [root@test1 ~]#./configure
    [root@test1 ~]#make && make install
    [root@test1 inotify-tools-3.13]# ll /usr/local/bin/inotifywa*
    -rwxr-xr-x. 1 root root 32010 Aug 18 19:50 /usr/local/bin/inotifywait #用于等待文件或者文件集上的特定事件
    -rwxr-xr-x. 1 root root 33971 Aug 18 19:50 /usr/local/bin/inotifywatch #收集监控的文件系统统计数据,包括inotify事件发生多少次

    /usr/local/bin/inotifywait  

    -m:始终保持事件监控状态

    -r:递归查询目录

    -q:打印监控事件

    -e:指定要监控的事件,常见事件有,modify,delete,create和attrib

    这个还可以一次同步到多台服务器,用多台rsync服务端作为客户端,在另外一台服务器上监控一个目录,当目录里面有文件发生状态和属性改变事件,就会自动触发同步到多台服务器上

    rsync_inotify.sh

    #!/bin/bash
    #需要同步的服务器地址
    Dst_Host=192.168.1.20
    
    #源文件目录
    Src_Dir=/webserver/
    
    #需要同步的远程服务器上的模块名
    Dst_Module=WEBSERVER
    
    #远程服务器上的模块认证用户
    Module_UserName=sheng 
    
    #rsync命令的绝对路径
    Rsync_Pwd=`whereis rsync | awk '{print $2}'`
    
    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' 
    --format '%T %w%f %e' 
    -e modify,delete,create,attrib $Src_Dir 
    | while read file
    do 
        ${Rsync_Pwd} -vzrtopg --delete  --password-file=/etc/rsyncd.pass $Src_Dir $Module_UserName@$Dst_Host::$Dst_Module
        echo "${file} was rsyncd!" >>  /var/log/rsync_inotify.log
    done

    同样也可以用这个命令扑捉到文件改变的事件,运行别的程序,比如nginx   nginx.conf文件一改变就重新加载

  • 相关阅读:
    instancetype
    字典转模型
    类前缀
    当你学不进去的时候 不妨看看大脑是怎么想的
    当你学不进去的时候 不妨看看大脑是怎么想的
    你属于开源性格测试六大分类中的哪一类呢
    你属于开源性格测试六大分类中的哪一类呢
    程序员会被淘汰吗?
    程序员会被淘汰吗?
    Java基础学习总结(49)——Excel导入导出工具类
  • 原文地址:https://www.cnblogs.com/pping/p/3883920.html
Copyright © 2011-2022 走看看