rsync+inotify实现实时数据同步

本例中是通过inotify监控客户端的目录文件是否发生变化,如发生变化实时的同步到服务端

主机

client 17X.19.23.24
server 1X.237.150.72

监控client端目录

/export/jenkins_data
/export/yangfan

client端

  • 安装文件监控工具inotify-tools
    [root@A01-R08-I23-24 Shell]# yum install inotify-tools
  • 安装rsync
    [root@A01-R08-I23-24 Shell]# yum install rsync
  • 配置rsync客户端密码 (此密码是rsync互相通信的密码,与系统登录密码不是一回事)
    [root@A01-R08-I23-24 Shell]# cat /etc/rsync.password
    2egseZjPc7jJxwa#
  • 配置密码文件权限
    [root@A01-R08-I23-24 Shell]# chmod 600 /etc/rsync.password
  • 配置监控脚本 (本例中是监控两个目录,所以写了两个监控脚本,之前尝试一个脚本中监控两个目录会有问题)
    [root@A01-R08-I23-24 Shell]# cat inotify-an.sh
    #!/bin/bash
    /usr/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M’ --format ‘%T %w%f’ -e modify,delete,create,attrib /export/yangfan | while read file
    do
    /usr/bin/rsync -vzrtopg --delete --progress /export/yangfan/ [email protected]::ansible-data --password-file=/etc/rsync.password
    echo “KaTeX parse error: Expected 'EOF', got '&' at position 46: …og/rsync.log 2>&̲1 done [root@…{files} was rsynced” >> /var/log/rsync.log 2>&1
    done
  • 启动监控脚本
    [root@A01-R08-I23-24 Shell]# nohup sh inotify-an.sh &
    [root@A01-R08-I23-24 Shell]# nohup sh inotify-jen.sh &

server端

  • 安装rsync
    [root@A01-R15-I150-72-4000255 ~]# yum install rsync
  • 修改rsync配置文件
    [root@A01-R15-I150-72-4000255 ~]# cat /etc/rsyncd.conf
    uid = 0
    gid = 0
    use chroot = no
    max connections = 0
    timeout = 300
    #####################
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    motd file=/etc/rsyncd.motd
    #####################
    [ansible-data]
    path = /export/yangfan/
    read only = false
    list = false
    secrets file = /etc/rsync.password
    #hosts allow = 0.0.0.0/24
    #hosts deny = 0.0.0.0/32
    auth users = root
    #######################
    [jenkins-data]
    path = /export/jenkins_data/
    read only = false
    list = false
    #hosts allow = 0.0.0.0/24
    #hosts deny = 0.0.0.0/32
    auth users = root
    secrets file = /etc/rsync.password
  • 配置密码文件 (服务端密码文件里格式必须是 用户名:密码)
    [root@A01-R15-I150-72-4000255 ~]# cat /etc/rsync.password
    root:2egseZjPc7jJxwa#
  • 配置文件权限
    [root@A01-R15-I150-72-4000255 ~]# chmod 600 /etc/rsyncd.conf
    [root@A01-R15-I150-72-4000255 ~]# chmod 600 /etc/rsync.password

猜你喜欢

转载自blog.csdn.net/weixin_41569908/article/details/85707039