当前位置:网站首页>再说rsync+inotify实现数据的实时备份
再说rsync+inotify实现数据的实时备份
2022-07-04 12:35:00 【星哥玩云】
rsync的优点与不足
与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
inotify介绍
Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在前面我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
安装inotify和inotify-tools工具
由于inotify特性需要Linux内核的支持,在安装inotify-tools前要先确认Linux系统内核是否达到了2.6.13以上,如果Linux内核低于2.6.13版本,就需要重新编译内核加入inotify的支持,也可以用如下方法判断,内核是否支持inotify。
检测系统是否支持inotify
#Linux shell
uname -a
Linux localhost 3.2.0-4-amd64 #1 SMP Debian 3.2.57-3 x86_64 GNU/Linux #内核大于2.6.13是支持的
ll /proc/sys/fs/inotify
总用量 0
-rw-r--r-- 1 root root 0 5月 19 20:02 max_queued_events
-rw-r--r-- 1 root root 0 5月 19 20:02 max_user_instances
-rw-r--r-- 1 root root 0 5月 19 20:02 max_user_watches
如果有上面三项输出,表示系统已经默认支持inotify,接着就可以开始安装inotify-tools了。
安装inotify-tools
Debian系统
apt-get install inotify-tools
CentOS系统
yum install inotify-tools
源码方式
请到https://github.com/rvoicilas/inotify-tools/下载源码编译安装。
ll /usr/bin/inotifywa*
inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中,inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。
inotify参数介绍
inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小。
cat /proc/sys/fs/inotify/max_queued_evnets
表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
cat /proc/sys/fs/inotify/max_user_instances
表示每一个real user ID可创建的inotify instatnces的数量上限。
cat /proc/sys/fs/inotify/max_user_watches
表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小,例如:
echo 30000000 > /proc/sys/fs/inotify/max_user_watches
inotifywait相关参数
Inotifywait是一个监控等待事件,可以配合shell脚本使用它,下面介绍一下常用的一些参数:
•-m, 即–monitor,表示始终保持事件监听状态。
•-r, 即–recursive,表示递归查询目录。
•-q, 即–quiet,表示打印出监控事件。
•-e, 即–event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等。
更多详细参数请查看手册。
man inotifywait #查看手册
边栏推荐
- runc hang 导致 Kubernetes 节点 NotReady
- Reinforcement learning - learning notes 1 | basic concepts
- runc hang 导致 Kubernetes 节点 NotReady
- Talk about "in C language"
- Peak detection of measured signal
- AI 绘画极简教程
- DC-5 target
- C#/VB.NET 给PDF文档添加文本/图像水印
- 干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
- Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
猜你喜欢
2022, 6G is heating up
[data clustering] section 3 of Chapter 4: DBSCAN performance analysis, advantages and disadvantages, and parameter selection methods
Transformer principle and code elaboration (tensorflow)
MDK在头文件中使用预编译器时,#ifdef 无效的问题
Cadence physical library lef file syntax learning [continuous update]
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
Jetson TX2 configures common libraries such as tensorflow and pytoch
分布式事务相关概念与理论
ISO 27001 Information Security Management System Certification
Paper notes ACL 2020 improving event detection via open domain trigger knowledge
随机推荐
mysql三级分销代理关系存储
Kivy教程之 08 倒计时App实现timer调用(教程含源码)
6 分钟看完 BGP 协议。
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
Article download address
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
Definition of cognition
runc hang 导致 Kubernetes 节点 NotReady
Full arrangement (medium difficulty)
强化学习-学习笔记1 | 基础概念
C语言:围圈报号排序问题
A taste of node JS (V), detailed explanation of express module
Sort merge sort
认知的定义
PostgreSQL 9.1 飞升之路
Etcd 存储,Watch 以及过期机制
R语言--readr包读写数据
Master the use of auto analyze in data warehouse
I want to talk about yesterday
C language: find the palindrome number whose 100-999 is a multiple of 7