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

    前序

    使用inotify+rsync架构实现(文件夹/文件)实时同步, 双机之间需要ssh免密码配置(两步完成ssh免密码登录)

    环境

    客户端:192.168.137.176 (rsync + inotify-tools-3.14 + 实时shell脚本)

    服务端:192.168.137.177 (rsync + xinetd)

    操作

    客户端

    工具:inotify-tools-3.14.tar.gz

    工具:rsync + xinetd

    tar -zxvf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure && make && make install
    

    实时shell(csync.sh)

    #*************************************************************************
    #         > File Name: /tmp/1.sh
    #         > Author: chenglee
    #         > Main : chengkenlee@sina.com
    #         > Blog : http://www.cnblogs.com/chenglee/
    #         > Created Time : 2019年02月19日 星期二 19时30分51秒
    #*************************************************************************
    #!/bin/bash
    serverip="192.168.137.177"
    serverdir="/home/test"
    clientdir="/home/test"
    module="test"
    
    /usr/local/bin/inotifywait -mrq -e modify,create,move,delete,attrib ${clientdir} | while read events
        do
        rsync -a --delete ${serverdir} ${serverip}::${module}
        echo "`date +'%F %T'` 出现事件 $events" >>rsync.log 2>&1
        done 

    服务端

    vim /etc/rsyncd.conf

    # /etc/rsyncd: configuration file for rsync daemon mode
    # See rsyncd.conf man page for more options.
    # configuration example:
    # uid = nobody
    # gid = nobody
    # use chroot = yes
    # max connections = 4
    # pid file = /var/run/rsyncd.pid
    # exclude = lost+found/
    # transfer logging = yes
    # timeout = 900
    # ignore nonreadable = yes
    # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
    
    [test]
        path = /home/
        read only = false
        uid = root
        gid = root
    

    启动xinetd(没有的安装一下)

    systemctl restart xinetd

    启动rsync

    rsync --daemon
    

    测试

    1.新建资源文件夹(客户端个服务端都需要建立)

    mkdir /home/test

    2.启动实时shell

    nohup ./csync.sh &
    

    3.在客户端/home/test文件夹下建立资源

    touch /home/test/file{1..10}
    

    日志

    2019-02-19 21:39:52 出现事件 /home/test/ CREATE file6
    2019-02-19 21:39:52 出现事件 /home/test/ ATTRIB file6
    2019-02-19 21:39:52 出现事件 /home/test/ CREATE file7
    2019-02-19 21:39:52 出现事件 /home/test/ ATTRIB file7
    2019-02-19 21:39:53 出现事件 /home/test/ CREATE file8
    2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file8
    2019-02-19 21:39:53 出现事件 /home/test/ CREATE file9
    2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file9
    2019-02-19 21:39:53 出现事件 /home/test/ CREATE file10
    2019-02-19 21:39:53 出现事件 /home/test/ ATTRIB file10

    服务端展示

    只要实时监控脚本启动, 无论你在客户端机器的/home/test下做任何操作都会自动同步到服务端的/home/test下面

  • 相关阅读:
    [算法]外部排序
    [笔试]华为编程大赛题目
    [C++]字符串处理方法(STL与C风格)
    如何动态建立VFP能够打开的中文字段 dbf 表 北极星
    使用 VCL BDE 组件动态创建数据库表 北极星
    如何用Table控件判断数据库是否为空 北极星
    DNGuard HVM副产品(元数据名称编辑器)
    常见dotNet加密保护工具分析介绍
    DNGuard HVM 试用版 RC1 发布
    [转载]Modifying IL at runtime
  • 原文地址:https://www.cnblogs.com/chenglee/p/10401610.html
Copyright © 2011-2022 走看看