美文网首页
在linux中使用rsync与inotify进行自动同步

在linux中使用rsync与inotify进行自动同步

作者: 多拉 | 来源:发表于2018-11-05 10:53 被阅读0次

    场景

    有时候我们会遇到在服务器中同步某些文件,例如从一台数据服务器同步到其他接口服务器中,这时可以使用rsync配合计划任务来进行,如果对同步时效要求比较高,可以考虑使用inotify对文件进行监控,遇到源文件有变动(新增、修改、删除等)时可以及时的调用rsync同步到其他服务器中。

    步骤

    1. 在要同步的几台目标服务器上开启rsync服务,新增或者修改/etc/rsyncd.conf文件如下:
    pid file=/var/run/rsyncd.pid
    uid=root  #运行rsync的用户,根据实际需要修改,注意非root用户可能导致同步文件权限不够而失败
    gid=root
    hosts allow=10.10.10.10  #允许的客户端ip,多个ip使用空格分隔
    log file=/var/log/rsync.log
    timeout=300
    port=1873
    
    [upload]
    path=/data/upload #同步路径
    ignore errors
    list=no
    write only=yes  #允许可写权限
    read only=no
    auth users=rsync
    secrets file=/etc/rsyncd.secrets
    
    1. 服务端执行rsync --daemon开启服务
    2. 客户端安装inotify-tools配合rsync开启自动同步,脚本如下:
    #!/bin/bash
    inotifywait -mrq /hwdata/upload --format "%w%f"  -e create,delete,moved_to,close_write|\
    while read line; do
        rsync --port 1873 -az --delete /data/upload/ rsync@10.10.10.10::upload 
    done
    
    1. 如果需要开启免密码验证,客户端导入环境变量RSYNC_PASSWORD即可。

    相关文章

      网友评论

          本文标题:在linux中使用rsync与inotify进行自动同步

          本文链接:https://www.haomeiwen.com/subject/uqgjxqtx.html