rsync+inotify实现实时数据同步

本文介绍了一种使用inotify监控文件变化并结合rsync进行实时同步的方法。具体步骤包括在客户端安装inotify-tools和rsync,配置rsync密码文件,编写监控脚本来监控指定目录的变化,然后通过rsync将变化的文件同步到服务器端。服务器端同样需要安装rsync,并配置rsyncd.conf文件来接受客户端的连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例中是通过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/ root@10.237.150.72::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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值