美文网首页
rsync+inotify文件实时同步

rsync+inotify文件实时同步

作者: e20d9310ced3 | 来源:发表于2020-06-23 10:03 被阅读0次

    需求:

    192.168.255.56:/opt目录下的文件变动,实时同步到192.168.255.57:/opt下

    部署:

    • 192.168.255.57(文件同步客户端)上操作:

      • 安装rsync
    yum -y install rsync
    
    • 添加配置文件
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 10
    pid file = /var/run/rsyncd.pid
    exclude = lost+found/
    log file = /var/log/rsync.log
    log format = %t %a %m %f %b
    motd file = /etc/rsyncd/rsyncd.motd
    [data]
    path = /opt/  # 同步数据目录,以/结尾,否则会在目录下再创建一个同步目录
    transfer logging = yes
    ignore errors
    read only = no
    list = no
    hosts allow = 192.168.255.56/32  # ip白名单
    uid = root
    gid = root
    auth users = backuser
    secrets file = /etc/rsync.password
    
    • 添加密码文件,并把权限修改为600
    echo "backuser:123456" > /etc/rsync.password
    chmod 600 /etc/rsync.password
    
    • 启动rsync服务,添加开机启动
    rsync --daemon
    echo "/usr/bin/rsync --daemon" >> /etc/rc.local
    
    • 192.168.255.56(文件同步服务端/文件改变端)上操作:

      • 安装rsync
    yum -y install rsync
    
    • 添加配置文件
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 10
    pid file = /var/run/rsyncd.pid
    exclude = lost+found/
    log file = /var/log/rsync.log
    log format = %t %a %m %f %b
    motd file = /etc/rsyncd/rsyncd.motd
    [data]
    path = /opt/  # 同步数据目录
    transfer logging = yes
    ignore errors
    read only = no
    list = no
    hosts allow = 192.168.255.57/32  # ip白名单
    uid = root
    gid = root
    auth users = backuser
    secrets file = /etc/rsync.password
    
    • 添加密码文件,并把权限修改为600,这里不需要用户名
    echo "123456" > /etc/rsync.password
    chmod 600 /etc/rsync.password
    
    • 安装inotify
      下载安装包
    tar -zxf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure
    make && make install
    
    • 编写同步脚本rsync.sh
    #!/bin/bash
    src=/opt/
    des=data
    host="192.168.255.57"
    /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files
    do
    for hostip in $host
    do
    rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password $src backuser@$hostip::$des
    done
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
    done
    
    • 启动实时同步
    nohup /bin/bash /root/rsync.sh &
    

    相关文章

      网友评论

          本文标题:rsync+inotify文件实时同步

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