zoukankan      html  css  js  c++  java
  • (转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

    Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步
    原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80%9A%E8%BF%87rsync%E4%B8%8Einotify%E5%BC%82%E6%AD%A5%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E4%BA%8B%E4%BB%B6%E7%9B%91%E6%8E%A7%E6%9C%BA%E5%88%B6%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6/
    目录 [隐藏]

    inotify-tools工具安装
    与rsync配合通过shell脚本实现同步
    将inotify加入自动开机启动服务中
    inotify参数优化
    rsync+intity压力测试效果
    rsync+inotify优缺点
    高并发数据实时同步方案
    inotify-tools工具安装
    查看系统支持:


    uname -r #系统内核至少达到2.6.13
    ls -l /proc/sys/fs/inotify/
    总用量 0
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_queued_events
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_user_instances
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_user_watches
    #显示这三个文件则证明支持

    uname -r #系统内核至少达到2.6.13
    ls -l /proc/sys/fs/inotify/
    总用量 0
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_queued_events
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_user_instances
    -rw-r--r-- 1 root root 0 6月 20 13:17 max_user_watches
    #显示这三个文件则证明支持
    下载inotify源码包


    mkdir -p /home/kendall/tools/
    cd /home/kendall/tools/
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    ls inotify-tools-3.14.tar.gz

    mkdir -p /home/kendall/tools/
    cd /home/kendall/tools/
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    ls inotify-tools-3.14.tar.gz
    编译安装


    cd /home/kendall/tools/
    tar zxf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14/
    ./configure --prefix=/usr/local/inotify-tools-3.14
    make && make install
    echo $?
    cd ../
    ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools

    cd /home/kendall/tools/
    tar zxf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14/
    ./configure --prefix=/usr/local/inotify-tools-3.14
    make && make install
    echo $?
    cd ../
    ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
    命令位置


    /usr/bin/inotifywait
    inotifywait 可以直接使用

    /usr/bin/inotifywait
    inotifywait 可以直接使用
    inotify安装在rsync客户端,监控到数据变化后通过rsync推给rsync服务端.

    与rsync配合通过shell脚本实现同步

    #!/bin/bash
    inotify=/usr/bin/inotifywait
    Path=/data
    IP=172.16.1.41
    $inotify -mrq --format '%w%f' -e create,close_write,delete $Path
    |while read file
    do
    if [ -f $file ];then
    rsync -az $file --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
    else
    cd $Path
    rsync -az ./ --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
    fi
    done

    #!/bin/bash
    inotify=/usr/bin/inotifywait
    Path=/data
    IP=172.16.1.41
    $inotify -mrq --format '%w%f' -e create,close_write,delete $Path
    |while read file
    do
    if [ -f $file ];then
    rsync -az $file --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
    else
    cd $Path
    rsync -az ./ --delete rsync_backup@$IP::nfsbackup --password-file=/etc/rsync.password
    fi
    done
    简易版


    #!/bin/bash
    inotify=/usr/bin/inotifywait
    $inotify -mrq --format '%w%f' -e create,close_write,delete /data
    |while read file
    do
    cd /data &&
    rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password & #可以增加效率
    done

    #!/bin/bash
    inotify=/usr/bin/inotifywait
    $inotify -mrq --format '%w%f' -e create,close_write,delete /data
    |while read file
    do
    cd /data &&
    rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password & #可以增加效率
    done
    后台运行,开机启动
    /bin/sh 脚本 &

    将inotify加入自动开机启动服务中

    vim /etc/init.d/syncd
    1
    vim /etc/init.d/syncd

    #!bin/bash
    #chkconfig: 2345 38 46
    #######################
    #
    #######################
    . /etc/init.d/functions
    if [ $# -ne 1 ];then
    usage: $0 {start|stop}
    exit 1
    fi
    case "$1" in
    start)
    /bin/bash /server/scripts/inotify.sh &
    echo $$ >/var/run/inotify.pid
    if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then
    action "inotify service is started" /bin/true
    else
    action "inotify service is started" /bin/false
    fi
    ;;
    stop)
    kill -9 cat /var/ run/inotify.pid >/dev/null 2>&1
    pkill inotifywait
    sleep 2
    if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then
    action "inotify service is stopped" /bin/true
    else
    action "inotify service is stopped" /bin/false
    fi
    ;;
    *)
    usage: $0 {start|stop}
    exit 1
    esac

    #!bin/bash
    #chkconfig: 2345 38 46
    #######################
    #
    #######################
    . /etc/init.d/functions

    if [ $# -ne 1 ];then
    usage: $0 {start|stop}
    exit 1
    fi

    case "$1" in
    start)
    /bin/bash /server/scripts/inotify.sh &
    echo $$ >/var/run/inotify.pid
    if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then
    action "inotify service is started" /bin/true
    else
    action "inotify service is started" /bin/false
    fi
    ;;
    stop)
    kill -9 cat /var/ run/inotify.pid >/dev/null 2>&1
    pkill inotifywait
    sleep 2
    if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then
    action "inotify service is stopped" /bin/true
    else
    action "inotify service is stopped" /bin/false
    fi
    ;;
    *)
    usage: $0 {start|stop}
    exit 1
    esac

    chmod +x /etc/init.d/syncd
    chkconfig --add syncd
    chkconfig syncd on
    1
    2
    3
    chmod +x /etc/init.d/syncd
    chkconfig --add syncd
    chkconfig syncd on
    inotify参数优化
    下面两条命令需要加入开机启动/etc/rc.local中


    echo "50000000" > /proc/sys/fs/inotify/max_user_watches
    echo "50000000" > /proc/sys/fs/inotify/max_queued_events
    max_queued_events #队列容纳事件数量
    max_user_instances #每个用户可以运行wait watch的数量
    max_user_watches #最大监控文件数量
    1
    2
    3
    4
    5
    echo "50000000" > /proc/sys/fs/inotify/max_user_watches
    echo "50000000" > /proc/sys/fs/inotify/max_queued_events
    max_queued_events #队列容纳事件数量
    max_user_instances #每个用户可以运行wait watch的数量
    max_user_watches #最大监控文件数量
    rsync+intity压力测试效果
    每秒200文件以下并发,基本没有差异.

    rsync+inotify优缺点
    优点:

    监控文件系统事件变化,通过同步工具实现实时数据同步

    缺点:

    并发如果大于200个文件(10-100k),同步会有延迟。
    我们前面写的脚本,每次都是全部推送一次,但确实是增量的,也可以只同步变化的文件,不变化的不理。
    监控到事件后,调用rsync同步是单进程的(加&并发),sersync多进程同步。
    高并发数据实时同步方案
    inotify(sersync)+rsync 文件级别
    drbd 文件系统级别,基于block块同步 缺点:备份节点数据不可用
    第三方软件同步功能:mysql同步,oracle mongodb
    程序双写,直接写两台服务器
    业务逻辑解决(【写主nfs,读备backup】读写分离,备读不到,读主 )防止延迟,弃用NFS方案,用web做nfs的备节点。效率提高很多
    NFS集群(145方案整合)(双写主存储,备存储用inotify(sersync)+rsync,备没有找主解决延迟问题)

  • 相关阅读:
    dll反编译工具(ILSpy)的使用
    织梦CMS进入自定义表单界面查看数据
    SQL -注释方法
    SQL之CASE WHEN用法详解
    Windows Installer Clean Up 软件卸载工具下载
    【精华合辑】金蝶云BOS资料合辑
    报表二次开发添加自定义字段的指导方案
    怎样选购笔记本内存条?
    SQL Server 2008修改sa密码的详细方法
    VS 2015 2019注册码
  • 原文地址:https://www.cnblogs.com/liujiacai/p/8481383.html
Copyright © 2011-2022 走看看