• rsync+inotify


    前提条件:已配置好rsync客户端和rsync --daemon服务端,数据可以正常同步,linux内核必须是2.6.13以上,如图:

    [root@A-linux ~]# rsync -avzP /tmp/ rsync_backup@10.0.0.4::data --password-file=/etc/rsync.password  #<===首先检查同步是否正常
    sending incremental file list
    ./
    hosts
    166 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
    ssh-hBbdsD1500/
    ssh-hBbdsD1500/agent.1500
    
    sent 250 bytes received 38 bytes 576.00 bytes/sec
    total size is 166 speedup is 0.58
    
    [root@C-linux data]# ll   #<===服务端查看同步结果
    总用量 8
    -rw-r--r-- 1 rsync rsync 166 1月 31 10:36 hosts
    drwx------ 2 rsync rsync 4096 1月 31 13:27 ssh-hBbdsD1500
    

    接着,在客户端安装inotify软件(这里是源码编译安装inotify-tools-3.14.tar.gz)

    [root@A-linux tools]# tar xf inotify-tools-3.14.tar.gz 
    [root@A-linux tools]# ./configure --prefix=/usr/local/inotify-tools-3.14 
    [root@A-linux tools]# make && make install 
    [root@A-linux tools]# ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
    [root@A-linux tools]# cd ../

    inotify几个主要的内核参数(可根据实际调整参数)

    [root@A-linux inotify]# ll -l /proc/sys/fs/inotify/            #<===以下文件中的默认值需要调大
    total 0
    -rw-r--r-- 1 root root 0 Sep 27 17:57 max_queued_events        #<===监控时件最大数量,需调整此文件默认大小
    -rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_instances       #<===用户的实例
    -rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_watches         #<===用户实例可监控的最大目录及文件数量
    [root@A-linux inotify ~]# echo 327679 > /proc/sys/fs/inotify/max_queued_events  
    [root@A-linux inotify ~]# echo 30000000 > /proc/sys/fs/inotify/max_user_watches

    inotify软件包主要的2个命令

    [root@A-linux tools]# cd /usr/local/inotify
    [root@A-linux inotify]# tree ./bin
    ./bin
    ├── inotifywait          #<===等待文件发生变化,是inotify核心命令
    └── inotifywatch         #<===用于收集文件系统的统计数据,例如发生了多少次inotify事件,某文件被访问了多少次等等,一般用不上

    inotifywait 命令的参数

    -m  :表示始终监控,否则应该是监控到了一次就退出监控了
    -r  :递归监控,监控目录中的任何文件,包括子目录。递归监控可能会超出max_user_watches的值,需要适当调整该值
    @<file>  :如果是对目录进行递归监控,则该选项用于排除递归目录中不被监控的文件。file是相对路径还是绝对路径由监控目录是相对还是绝对来决定
    -q  :--quiet的意思,静默监控,这样就不会输出一些无关的信息
    -e  :指定监控的事件。一般监控的就 delete、create、attrib、modify、close_write
    --exclude <pattern>  :通过模式匹配来指定不被监控的文件,区分大小写
    --excludei <pattern> :通过模式匹配来指定不被监控的文件,不区分大小写
    --timefmt  :监控到事件触发后,输出的时间格式,可指定可不指定该选项,一般设置为[--timefmt '%Y/%m/%d %H:%M:%S']
    --format   :用户自定义的输出格式,如[--format '%w%f %e%T']
      %w  :产生事件的监控路径,不一定就是发生事件的具体文件,例如递归监控一个目录,该目录下的某文件产生事件,将输出该目录而非其内具体的文件
      %f  :如果监控的是一个目录,则输出产生事件的具体文件名。其他所有情况都输出空字符串
      %e  :产生的事件名称
      %T  :以"--timefmt"定义的时间格式输出当前时间,要求同时定义"--timefmt"
    

    inotifywait -e 指定可监控的事件

    access  :文件被访问
    modify  :文件被写入,内容被修改
    attrib  :元数据被修改。包括权限、时间戳、扩展属性等等
    close_write  :打开的文件被关闭,是为了写文件而打开文件,之后被关闭的事件
    close_nowrite :read only模式下文件被关闭,即只能是为了读取而打开文件,读取结束后关闭文件的事件
    close :是close_write和close_nowrite的结合,无论是何种方式打开文件,只要关闭都属于该事件
    open  :文件被打开
    moved_to  :向监控目录下移入了文件或目录,也可以是监控目录内部的移动
    moved_from :将监控目录下文件或目录移动到其他地方,也可以是在监控目录内部的移动
    move :是moved_to和moved_from的结合
    moved_self :被监控的文件或目录发生了移动,移动结束后将不再监控此文件或目录
    create :在被监控的目录中创建了文件或目录
    delete :删除了被监控目录中的某文件或目录
    delete_self :被监控的文件或目录被删除,删除之后不再监控此文件或目录
    umount :挂载在被监控目录上的文件系统被umount,umount后不再监控此目录
    isdir :监控目录相关操作
    

    测试inotify监控实例(监控A-linux主机中/data目录下子目录及文件的变化)

    监控单个事件
    [root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data     #<==仅仅监控目录中被删除文件或目录
    
    同时监控多个事件
    [root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write /data 
    

    使用inotify脚本实时监控目录内子目录和文件的变化

    [root@A-linux ~]# cat inotify.sh 
    #!/bin/bash
    
    ###########################################################
    # description: inotify+rsync best practice #
    # author : 骏马金龙 #
    # blog : http://www.cnblogs.com/f-ck-need-u/ #
    ###########################################################
    
    watch_dir=/data
    push_to=10.0.0.4
    
    # First to do is initial sync
    rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password
    
    /usr/local/inotify/bin/inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir 
    --exclude=".*.swp" >>/etc/inotifywait.log &
    
    while true;do
    if [ -s "/etc/inotifywait.log" ];then
    grep -i -E "delete|moved_from" /etc/inotifywait.log >> /etc/inotify_away.log
    rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password
    if [ $? -ne 0 ];then
    echo "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |
    mail -s "inotify+Rsync error has occurred" root@localhost
    fi
    cat /dev/null > /etc/inotifywait.log
    rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password
    else
    sleep 1
    fi
    done
    

    脚本可以放进 /etc/rc.local 中开机执行,或者放在后台执行

    [root@A-linux ~]# echo '/bin/sh /root/inotify.sh &' >>/etc/rc.local 
    [root@A-linux ~]# tail -1 /etc/rc.local 
    /bin/sh /root/inotify.sh &

    小结:

    (1)当同步的目录数据量不大时,建议使用 rsync+inotify
    (2)当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用 rsync+sersync
    (3)生产环境直接用 rsync+sersync,而不使用 rsync+inotify

    博客参考 

    http://www.cnblogs.com/f-ck-need-u/p/7220193.html

    www.cnblogs.com/kevingrace/p/6001252.html

  • 相关阅读:
    《解密腾讯海量服务之道》讲座笔记
    图解 Paxos 一致性协议
    向量时钟算法简介——本质类似MVCC
    logback配置
    Protocol Buffers简明教程
    分布式调用技术 RPC VS REST
    Dubbo和Spring Cloud微服务架构对比
    Spring Boot学习笔记
    relatedTarget、fromElement、toElement相关元素
    12个不可不知的Sublime Text应用技巧和诀窍
  • 原文地址:https://www.cnblogs.com/blog-tim/p/10369710.html
Copyright © 2020-2023  润新知