Inotify+Rsync 实现自动同步数据

1. Inotify

​ inotify 是一个文件系统事件监控对于从文件管理器到安全工具的各种程序都是必要的。

​ inotify 结合 Rsync 实现自动同步数据

  • inotify 可以监控目录、文件的删除、创建】修改等操作,通过这些操作来触发 Rsync

2. 部署 inotify+rsync

实验环境:

HostIP
Host11.1.1.101
Host21.1.1.102

1) 实现互相互相可以免密登陆

  • 生成秘钥对进行传输
[root@localhost ~]# ssh-keygen

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub  root@1.1.1.102
[root@localhost ~]# ssh-keygen

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub  root@1.1.1.101

2) 安装Inotify

  • 源码安装
[root@localhost ~]# tar -zxf inotify-tools-3.14.tar\(1\)\(1\).gz -C /usr/src/
[root@localhost ~]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
  • 路径优化
[root@localhost inotify-tools-3.14]# ln -s /usr/local/inotify/bin/* /usr/local/bin/

3) 创建监控的目录

[root@localhost ~]# mkdir /test1

4) 监控目录(test1)

[root@localhost ~]# inotifywait -mrq --format %w%f -e create,delete,close_write /test1

参数解释:

        -m	一直处于监控的状态
        -r	递归监控
        -q	将监控的目录显示在当前的终端上
        --format:	指定输出事件的格式
        %w	产生监控的目录
        %f	输出文件名
        %e	输出时间名称
        %T	输出当前的时间
        -e	监控指定的时间
        
        动作(操作)
        move		移动
        create		创建
        delete		删除
        close_write	查看只读内容
  • 另开一个终端,在test1 目录中创建文件或者 目录
[root@localhost ~]# cd /test1/ 
[root@localhost test1]# mkdir a 
[root@localhost test1]# touch bb
  • 原终端
[root@localhost ~]# inotifywait -mrq --format %w%f -e create,delete,close_write /test1
/test1/a
/test1/bb

5) Inotify+rsync 实现实时同步数据

  • 修改 Rsync 配置文件
[root@localhost ~]# vim /etc/rsyncd.conf 

        port = 873								# rsync监听的端口
        uid = root								# rsync 运行用户
        gid = root								# rsync 运行用户组
        use chroot = no							# 对于当前操作用户,没有root权限
        max connections = 0						# 最大连接数,0 为不限制
        pid file = /var/run/rsyncd.pid			# 指定 pid 文件
        exclude = lost+found/					# 同步时取出到 lost_found 的内容
        transfer logging = yes					# 开启日志
        log file = /var/lib/rsyncd.log			# 指定日志文件路径
        timeout = 900							# 超时时间
        ignore nonreadable = yes				# 忽略没有权限的访问用户
        dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2		# 同步时这类文件不需要压缩
        
        [test]								# 同步模块名称
                path = /test1					# 实际的目录
                comment = rsync_test1			# 声明
                read only = no					# 是否允许 
        
  • 启动或重启服务
[root@localhost ~]# systemctl restart rsyncd
[root@localhost ~]# netstat -anpt | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN  
  • 编写脚本
    • 监控目录的数据动态,用于触发同步命令
[root@localhost ~]# vim rsync.sh 
        #! /bin/bash
        path=/test1
        client=1.1.1.102
        /usr/local/bin/inotifywait -mrq --format %w%f -e create,delete,close_write /test1 | while read file
        do
        if [ -f $file ];
                then
                rsync -az --delete $file root@$client:/test2
        else
                cd $path && \
                rsync -az --delete ./ root@$client:/test2
        fi
        done
  • 赋予执行权限
[root@localhost ~]# chmod +x /root/rsync.sh 
[root@localhost ~]# ./rsync.sh
  • 接下来就可以编写内容测试了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值