rsync + inotifywait 监控文件并同步

环境准备

1、原服务器安装软件

yum install rsync inotify-tools -y

2、目标机器安装软件

yum install rsync -y

部署目标机器rsync

1、配置vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
#
#工作中指定用户(可以不指定为0)
uid = root 
gid = root
port = 873
#相当于黑洞.出错定位
use chroot = no
#有多少个客户端同时传文件
max connections = 200
#超时时间
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#表示出现错误忽略错误
ignore errors
#表示网络权限可写(本地控制真正可写)
read only = no
#这里设置IP或让不让同步
list = no
#允许进行数据同步的原服务器IP地址,可以设置多个,用英文状态下 逗号隔开
hosts allow = 192.168.0.3
#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开(认证用户)
auth users = rsync_backup
#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
secrets file = /etc/rsyncd.pass
#fake supper = yes
#同步模块名称,(这个模块就是待会儿对端写脚本的里面一个参数的名称)
[upload]
##同步过来后放到哪里的目录路径
path = /data/web/log

2、配置rsyncd.pass

##用户:密码
rsync_backup:rsync@2022

创建用户

useradd rsync_backup
passwd rsync_backup

创建目录授权

mkdir -p /data/web/log
chmod 765 /data/web/log

启动rsync服务

systemctl start rsyncd
systemctl enable rsyncd

原服务器配置rsync

1、配置

vim /etc/rsyncd.passwd
#上面目标机器上配置的密码
rsync@2022

2、编辑inotify监控脚本

vim inotify.sh 
#!/bin/bash
##原文件目录
Path="/data/web/log/ywzcgo-admin-oversea"
##目标文件存放目录
Path2="/data/web/log"
##目标机器ip
backup_Server="192.168.0.4"
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径
#excludedir="/home/scripts/exclude.list"
##用户
user="rsync_backup"
##密码文件
pass_file="/etc/rsyncd.passwd"
##rsync模板
module="upload"
cd ${Path}
# 此方法中,由于rsync同步的特性,这里必须要先cd到源目录,inotify再监听 ./ 才能rsync同步后目录结构一致
inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file; do
    INO_EVENT=$(echo $file | awk '{print $1}')  # 把inotify输出切割 把事件类型部分赋值给INO_EVENT
    INO_FILE=$(echo $file | awk '{print $2}')   # 把inotify输出切割 把文件路径部分赋值给INO_FILE
    echo "-------------------------------$(date)------------------------------------"
    echo $file
    # 增加、修改、写入完成、移动进事件
    if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]; then # 判断事件类型
        rsync -avzcR --progress $(dirname ${INO_FILE}) ${user}@${backup_Server}::${module} --password-file=$pass_file
    fi
    # 删除、移出事件
    if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]; then
        rsync -avzR --delete $(dirname ${INO_FILE}) ${user}@${backup_Server}::${module} --password-file=$pass_file
    fi
    # 修改属性事件 指 touch chgrp chmod chown 等操作
    if [[ $INO_EVENT =~ 'ATTRIB' ]]; then
        echo 'ATTRIB'
        if [ ! -d "$INO_FILE" ]; then # 如果修改属性的是目录 则不同步,因为同步目录会发生递归扫描,等此目录下的文件发生同步时,rsync会顺带更新此目录。
            rsync -avzcR --progress $(dirname ${INO_FILE}) ${user}@${backup_Server}::${module} --password-file=$pass_file
        fi
    fi
done

启动rsync,创建开机自启

systemctl start rsyncd
systemctl enable rsyncd

在后台运行inotify监控脚本

nohup bash inotify.sh >> /var/log/inotify.log 2>&1 &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr&梁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值