rsync+inotify搭建实时同步系统

简介:

rsync简介

   rsync是Linux和UNIX系统下的文件同步和数据传输工具,它采用了“rsync”算法使一个客户机和远程文件服务器之间的文件同步,它不同于cp和wget命令完整复制的局限性,它支持增量备份,因此文件传输效率高,因而同步时间很短,具体特性如下:

   1、可以镜像保存整个目录树和文件系统

   2、可以增量同步数据。文件传输效率高。

   3、可以保持原有文件的权限、时间等属性。

   4、加密传输数据,保证数据的安全性。

   5、可以使用rcp、ssh等方式来传输文件。


搭建远程容灾备份系统

   Web服务器为了保证数据安全,每天凌晨2点将数据备份到远程容灾服务器上,由于数据量比较大,每年只能进行增量备份,仅备份当天新增数据,系统环境如下:

操作系统 RHEL5.8
内核版本 2.6.18-308.el5
web服务器地址 192.168.1.104
远程容灾服务器地址 192.168.1.110

Web端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[ixdba]
path =  /webdata
commnet = ixdba  file
ignore errors
read  only = no
write only = no
hosts allow = *
hosts deny = 192.168.1.200
list =  false
uid = root
gid = root
auth  users  = backup
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass 

1
backup:ixdba123

启动rsync守护进程

1
/usr/local/bin/rsync  --daemon

远程容灾端配置

密码文件chmod 600 /etc/server.pass

1
ixdba123

添加计划任务crontab -e

1
1 3 * * *   /usr/local/bin/rsync  -vzrtopg --delete --progress --exclude  "*access*"  --exclude  "debug*"  backup@192.168.1.104::ixdba  /ixdba . dir  --password- file = /etc/server .pass

分析不足

此时一个远程容灾系统已经搭建完成,但这并非是一个完整意义上的容灾方案,由于rsync需要通过触发才能将服务器端数据同步,因此两次触发同步的时间间隔内,服务器和客户端的数据可能不一致,如果在这个时间间隔内,网站系统出现问题,数据必然丢失,Linux 2.6.13以后的内核提供了inotify文件系统监控机制,用过rsync与inotify的组合,完全可以实现rsync服务器端和客户端的实时数据同步。


rsync+inotify实现数据的实时备份

   inotify是一种强大的、细粒度的、异步的文件系统时间监控机制,Linux内核从2.6.13版本起,加入了对inotify的支持。通过inotify可以监控文件系统中添加、删除、修改、移动等各种细微事件,利用这个内核接口,第三方软件可以监控文件系统下文件的各种变化情况,inotify-tools软件正是基于这种需求实现的一个第三方软件。

   内容分发服务器实时同步数据对2个Web服务器上,inotify是用来监控文件系统变化的工具,因此必须安装在内容发布节点上,内容发布节点(Server)充当了rsync客户端的角色,2个Web节点充当了rsync服务器端的角色,整个数据同步过程就是一个从客户端向服务器端发送数据的过程,与前面案例中的逻辑结构刚好相反,系统环境如下:

     节点名称      内核版本        IP地址 网页数据存放路径
Web1 2.6.18-308.el5 192.168.1.110 /web1/wwwroot
Web2 2.6.18-308.el5 192.168.1.160 /web2/wwwroot
Server 2.6.18-308.el5 192.168.1.104 /web/wwwroot

Web1端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[web1]
path =  /web1/wwwroot
commnet = web1  file
ignore errors
read  only = no
write only = no
hosts allow = 192.168.1.104
hosts deny = *
list =  false
uid = root
gid = root
auth  users  = web1user
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass

1
web1user:ixdba123

启动rsync守护进程并添加开机自动启动

1
2
echo  "/usr/local/bin/rsync"  >> /etc/rc . local
/usr/local/bin/rsync--daemon

Web2端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[web2]
path =  /web2/wwwroot
commnet = web2  file
ignore errors
read  only = no
write only = no
hosts allow = 192.168.1.104
hosts deny = *
list =  false
uid = root
gid = root
auth  users  = web2user
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass

1
web2user:ixdba123

启动rsync守护进程并添加开机自动启动

1
2
echo  "/usr/local/bin/rsync"  >> /etc/rc . local
/usr/local/bin/rsync--daemon

Server端配置

安装inotify-tools

1
2
3
4
tar  xf  rsync -3.0.7. tar .gz
cd  rsync -3.0.7
. /configure
make  &&  make  install

密码文件chmod 600 /etc/server.pass

1
ixdba123

监控目录变化并同步Web节点脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
host1=192.168.1.110
host2=192.168.1.160
dir = /web/wwwroot/
dst1=web1
dst2=web2
usr1=web1user
usr2=web2user
/usr/local/bin/inotifywait  -mrq --timefmt  '%d/%m/%y %H:%M'  -- format  '%T %w%f%e'  -e close_write,delete,create,attrib $ dir  \
while  read  files
         do
         /usr/bin/rsync  -vzrtopg --delete --progress --password- file = /etc/server .pass $ dir  $usr1@$host1::$dst1
         /usr/bin/rsync  -vzrtopg --delete --progress --password- file = /etc/server .pass $ dir  $usr2@$host2::$dst2
         echo  "${files} was rsynced"  >>  /tmp/rsync .log 2>&1
         done

指定权限并放入后台运行

1
2
chmod  755  /web/wwwroot/inotifyrsync .sh
/web/wwwroot/inotifyrsync .sh &

为此脚本添加开机自动启动

1
echo  "/web/wwwroot/inotifyrsync.sh &"  >>  /etc/rc . local

测试结果

Server端(rsync客户端)创建测试文件,如图:

Web1端查看,如图:

Web2端查看,如图:




本文转自 ftmoonfans  51CTO博客,原文链接:http://blog.51cto.com/soulboy/1281704

相关文章
What is a PunchOut Catalog?什么是PunchOut目录?--关于电商对接采购平台知识分享
What is a PunchOut Catalog?什么是PunchOut目录?关于电商对接采购平台知识分享
2064 0
|
9月前
|
机器学习/深度学习
《深度学习梯度消失问题:原因与解决之道》
梯度消失是深度学习训练中的常见问题,严重影响模型性能。其原因包括激活函数选择不当(如Sigmoid)、网络层次过深和权重初始化不合理。解决方法有:选择合适激活函数(如ReLU及其变种)、优化权重初始化(如Xavier、He初始化)、采用批量归一化、引入残差连接、使用LSTM等特殊结构、调整学习率及预训练加微调等策略。
662 8
|
JavaScript 前端开发 API
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
本文介绍了如何在Vue 3项目中使用v-md-editor组件库来创建markdown编辑器和预览组件。文章提供了安装步骤、如何在main.js中进行全局配置、以及如何在页面中使用VMdEditor和VMdPreview组件的示例代码。此外,还提供了一个完整示例的链接,包括编辑器和预览组件的使用效果和代码。
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
|
机器学习/深度学习 自然语言处理 PyTorch
|
分布式计算 搜索推荐 Java
Maven依赖原则及如何解决Maven依赖冲突
Maven依赖原则及如何解决Maven依赖冲突
656 0
|
运维 Linux 数据安全/隐私保护
CentOS7下OpenLDAP部署
OpenLDAP作为开源的LDAP服务,可用于搭建统一认证平台,在很多企业内部应用比较广泛,本文将介绍在CentOS7下OpenLDAP的部署。 环境: CentOS 7.4 OpenLDAP 2.4.44 phpldapadmin 1.2.3 1、安装OpenLDAP # 安装openldap软件
1018 0
|
Kubernetes NoSQL 应用服务中间件
使用Portainer部署Docker容器实践
因此我将rancher管理docker的模式换成使用protainer的方式,这个portainer相对来说更加轻量级,在搭建过程中也使用了几个小时学习,现在讲整个过程尽量复原给大家一些参考。
3614 0
使用Portainer部署Docker容器实践
|
jenkins 测试技术 Shell
用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
|
网络协议 安全 前端开发
如何系统学习计算机网络?(一)
关于计算机网络如何学习,我就拿自己亲身实践的来举例吧,因为我也自学学起的。 我觉得最重要的就是看书(博客) + 实践。
如何系统学习计算机网络?(一)
|
机器学习/深度学习 数据可视化 TensorFlow
用TensorFlow和TensorBoard从零开始构建ConvNet(CNN)
Tensorflow作为当下最流行的深度学习框架,实现ConvNet(CNN)自然是轻而易举,但是本文创造性的使用的TensorBoard来图形化展示CNN实现过程,极大的提高了研究者的对自己模型的管理能力。
5103 0