rsync+inotify 实现远程文件实时增量同步至本地


版本控制

版本 作者 日期 修改记录 备注
V1.0 Mr.Wu 2019-10-15 初始创建  
         

一、部署规划

Ip地址 主机名 角色 软件版本(系统自带)
10.1.1.26 web-server 生产服务器 rsync-3.1.2-4.el7.x86_64
10.1.1.27 rsync 备份服务器 rsync-3.1.2-4.el7.x86_64

二、环境说明

1. 系统版本

CentOS Linux release 7.6.1810 (Core) x86_64

2. 系统初始化

2.1 关闭防火墙及selinx

在所有虚拟机上进行以下操作:
# systemctl stop iptables
# sed -i '/SELINUX=enforcing/cSELINUX=disabled' /etc/selinux/config

2.2 关闭 NetworkManager 服务

在所有虚拟机上进行以下操作:
# systemctl stop NetworkManager
# systemctl disable NetworkManager

三、配置 web-server 服务器

1. 配置文件添加同步模块

在 web-server 服务器上进行:
[root@web-server ~]# vim /etc/rsyncd.conf
......
[logs]
path = /var/log
comment = all files under this dir
[root@web-server ~]# ll /var/log |wc -l
41

在这里插入图片描述

2. 启动 rsyncd 后台服务

[root@web-server ~]# rsync --daemon
[root@web-server ~]# lsof -i:873

在这里插入图片描述

四、配置备份服务器

1. 指定同步的路径 /backup

[root@rsync /]# mkdir backup

2. 查看 web 服务端需同步的模块

[root@rsync /]# rsync -a 10.1.1.26::

在这里插入图片描述

五、定时任务实现模块同步(在备份机上进行)

1. 编写同步脚本

[root@rsync /]# vim /root/rsync.sh
#!/bin/bash
rsync -av root@10.1.1.26::logs  /backup
[root@rsync /]# chmod +x /root/rsync.sh

2. 编写定时同步任务测试

以每分钟执行一次进行测试
[root@rsync /]# crontab -e
* * * * *  sh /root/rsync.sh &>/dev/null
[root@rsync bakup]# date
2019年 10月 15日 星期二 20:38:26 CST
[root@rsync bakup]# ll /backup/ |wc -l
0
[root@rsync bakup]# date
2019年 10月 15日 星期二 20:39:01 CST
[root@rsync bakup]# ll /backup/ |wc -l
41

六、 rsync+inotify实现文件实时同步(在web服务器部署inotify,在备份服务器配置rsync守护进程)

1. web 服务器部署

1.1 下载安装 inotify 软件

[root@web-server ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@web-server ~]# cd /usr/local/ && mv /usr/local/inotify-tools-3.14/ /usr/local/inotify
[root@web-server ~]# cd /usr/local/inotify && ./configure
[root@web-server inotify]# make && make install

1.2 检查 inotify 带来的两个命令

[root@web-server inotify]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 60720 10月 15 21:18 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 55080 10月 15 21:18 /usr/local/bin/inotifywatch
相关参数:
-m 保持监控状态
-r 递归监控
-q 只打印事件
-e 指定事件

事件:
move	移动
delete	删除
create	创建
modify	修改
attrib	属性信息

1.3 编写同步脚本

[root@web-server inotify]# vim inotify.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /var/log/ |while read events
do
   rsync -a --delete /var/log/  10.1.1.27:/backup/
   echo "`date +%F\ %T`出现事件$events" >> /var/rsync.log 2>&1
done

1.4 配置web 服务器至备份服务器的免密登录

[root@web-server inotify]# ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" 
[root@web-server inotify]# ssh-copy-id 10.1.1.27

2. 备份服务器起后台进程

[root@rsync log]# rsync --daemon
[root@rsync log]# lsof -i:873
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   93582 root    4u  IPv4 236984      0t0  TCP *:rsync (LISTEN)
rsync   93582 root    5u  IPv6 236985      0t0  TCP *:rsync (LISTEN)

3. web 服务器起监控并添加文件测试

[root@web-server inotify]# ./inotify.sh &
[root@web-server log]# mkdir 11{1..3}
[root@web-server log]# touch file {5..8}
[root@web-server log]# cat /var/rsync.log
2019-10-15 22:28:11出现事件/var/log/ CREATE,ISDIR 111
2019-10-15 22:28:12出现事件/var/log/ CREATE,ISDIR 112
2019-10-15 22:28:12出现事件/var/log/ CREATE,ISDIR 113
2019-10-15 22:28:19出现事件/var/log/ CREATE file
2019-10-15 22:28:20出现事件/var/log/ ATTRIB file
2019-10-15 22:28:20出现事件/var/log/ CREATE 5
2019-10-15 22:28:20出现事件/var/log/ ATTRIB 5
2019-10-15 22:28:20出现事件/var/log/ CREATE 6
2019-10-15 22:28:20出现事件/var/log/ ATTRIB 6
2019-10-15 22:28:20出现事件/var/log/ CREATE 7
2019-10-15 22:28:21出现事件/var/log/ ATTRIB 7
2019-10-15 22:28:21出现事件/var/log/ CREATE 8
2019-10-15 22:28:21出现事件/var/log/ ATTRIB 8

4. 备份服务器查看

[root@rsync backup]# ll 
drwxr-xr-x 2 root root 6 10月 15 22:28 111
drwxr-xr-x 2 root root 6 10月 15 22:28 112
drwxr-xr-x 2 root root 6 10月 15 22:28 113
-rw-r--r-- 1 root root 0 10月 15 22:28 5
-rw-r--r-- 1 root root 0 10月 15 22:28 6
-rw-r--r-- 1 root root 0 10月 15 22:28 7
-rw-r--r-- 1 root root 0 10月 15 22:28 8
-rw-r--r-- 1 root root 0 10月 15 22:28 file


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM