美文网首页
基于 rsync + inotify 实现实时同步

基于 rsync + inotify 实现实时同步

作者: Alexander_Zz | 来源:发表于2019-04-18 16:52 被阅读0次

    Rsync 简介
    • 官方网站:https://rsync.samba.org
    • 软件工作流程简介
      服务端:提供同步账号密码,可使用密钥认证。默认监听在 873 的 TCP 端口上。服务端需配置在放置同步数据的服务器上。
      客户端:配置在被同步的服务器端
    Inotify 简介
    • 官方页面:https://github.com/rvoicilas/inotify-tools/wiki
    • 主要文件
      inotifywait:在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,常用于实时同步的目录监控
      inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发 生的次数统计

    配置过程
    • 查看是否支持 Inotify
    # Linux 下支持 inotify 的最小内核为 2.6.13
    # 可使用 ll /proc/sys/fs/inotify 命令检查是否支持
    # 出现以下三个文件表示支持
    ~]# ll /proc/sys/fs/inotify
    total 0
    -rw-r--r-- 1 root root 0 Apr 18 15:46 max_queued_events
    -rw-r--r-- 1 root root 0 Apr 18 15:46 max_user_instances
    -rw-r--r-- 1 root root 0 Apr 18 15:46 max_user_watches
    
    # max_queued_events:inotify事件队列最大长度,如值太小会出现 Event Queue Overflow 错误,默认值:16384 
    # max_user_watches:可以监视的文件数量(单进程),默认值:8192 
    # max_user_instances:每个用户创建inotify实例最大值,默认值:128
    
    • Node 1 Source
    ~]# yum install -y rsync inotify-tools
    ~]# echo "rookie" > /etc/rsync.pass   # 非交互式
    ~]# chmod 600 /etc/rsync.pass
    ~]# vim /data/scripts/rsync_inotify.sh
    #!/bin/bash
    #
    SRC='/data/'
    DEST='rsyncuser@172.18.33.105::backup   # backup 是服务端配置的共享名称
    inotifywait -mrq --timefmt '%T %F' --format '%T %w %f -e create,delete,moved_to,close_write,attrib ${SRC} | while read DATE TIME DIR FILE; do
        FILEPATH=${DIR}${FILE}
        rsync -avlopg --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped via rsync" >> /var/log/change_list.log   # 记录日志可酌情添加
    done
    
    ~]# bash /data/scripts/rsync_inotify.sh
    # 可使用 screen 命令跑此脚本实现终端没有断开亦可执行
    
    • Node 2 Destination
    ~]# yum install -y rsync
    ~]# vim /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot= no
    max connections = 0
    ignore errors exclude = lost+found/
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    reverse lookup = no
    hosts allow = 172.18.33.104
    [backup]   # 服务共享名
    path = /data/nfs/
    comment = backup
    read only = no
    auth users = rsyncuser
    secrets file = /etc/rsync.pass
    
    ~]# echo "rsyncuser:rookie" > /etc/rsync.pass
    ~]# chmod 600 /etc/rsync.pass
    ~]# mkdir -pv /data/nfs
    ~]# rsync --daemon   # 后台运行
    # 可加入 /rc.d/rc.local 实现开机启动(需加执行权限)
    

    相关文章

      网友评论

          本文标题:基于 rsync + inotify 实现实时同步

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