rsycn+inotify实时网络数据镜像备份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsycn+inotify实时网络数据镜像备份相关的知识,希望对你有一定的参考价值。

inotify简介:

   inotify是一个文件系统监控机制,是一个内核特性,它监控文件系统的操作如:读取,写入,和创建以及删除。对文件的变化反应灵敏,且使用简单,所以这是一个很好的自动化检测数据变化的工具。

   从上面的一段简介中我们可以看出inotify就是一个内核文件监控工具,实时监控着数据的变化,而对于rsync来说这是莫大的一个帮助,因为在之前要触发rsync数据镜像同步,我们是使用crond来指定时间执行rsync,虽然这样做到了数据的镜像同步备份,但是有一个缺点就是当两次触发rsync时的时间段里发生数据丢失,那么那部分丢失的数据可想而知。。。

   所以现在我们可以将rsync结合inotify来一起使用,当数据发生变化,可以通过inotify监控立即反应并触发rsync,就可以达到数据的实时镜像同步备份。流程大致如下:

技术分享

inotify的安装

首先要内核的支持

查看内核是否是2.6.13版本或以上,否则升级内核

[[email protected] ~]# uname -r

3.10.0-514.26.2.el7.x86_64

yum install inotify

inotify两个指令:

inotifywait 可以监控文件以及目录设置,监控事件。

inotifywatch 收集监控统计数据,如监控事件次数等。

##详细命令加-h查看

接下来以两台服务器做示范

京东云主机为A IP :  AAA.AAA.AAA.AAA

阿里云主机为B IP :  BBB.BBB.BBB.BBB

使用B的数据为基准实时同步到A机上


A

在A机器上设置rsync.conf
[[email protected] ~]# cat /etc/rsyncd.conf 
 uid = nobody
 gid = nobody
 use chroot = no
 max connections = 4
 pid file = /var/run/rsyncd.pid
 [XAD]
        path = /root/testrsync
        comment = xadsrc file
read only = no
write only = no
hosts allow = *
uid = root
gid = root
auth users = backup
secrets file = /etc/server.pass
[[email protected] ~]# cat /etc/server.pass 
backup:xad123
[[email protected] ~]# ll /etc/server.pass 
-rw------- 1 root root 14 Sep 23 22:05 /etc/server.pass
[[email protected] ~]# [[email protected] ~]#/usr/bin/rsync --daemon
[[email protected] ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local ##开启守护进程



B

[[email protected] ~]# cat /etc/client.pass 
xad123


[[email protected] ~]# ll /etc/client.pass 
-rw------- 1 root root 7 Sep 23 22:07 /etc/client.pass
在B上通过执行后台shell脚本来却保rsync+inotify的工作协调。

[[email protected] ~]# cat rsync_inotify.sh
#!/bin/bash
/usr/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e close_write,delete,create,attrib /root/rsynctest/ | while read data_change
do /usr/bin/rsync  -vzrtopg --delete --progress --password-file=/etc/client.pass /root/rsynctest/  [email protected]::XAD 
           echo "${data_change} was rsync" >> /var/log/rsync.log   2>&1
done


[[email protected] ~]# chmod +x rsync_inotify.sh 
[[email protected] ~]# echo "/root/rsync_inotify.sh &" >> /etc/rc.local



测试B中创建文件,A中查看

[[email protected] rsynctest]# touch test
[[email protected] rsynctest]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 25 11:26 test

[[email protected] testrsync]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 25 11:26 test



查看日志

[[email protected] rsynctest]# cat /var/log/rsync.log 
25/09/17 11:26 /root/rsynctest/testCREATE was rsync
25/09/17 11:26 /root/rsynctest/testATTRIB was rsync
25/09/17 11:26 /root/rsynctest/testCLOSE_WRITE,CLOSE was rsync



本文出自 “TOP-ONE” 博客,请务必保留此出处http://onenice.blog.51cto.com/13201315/1968387

以上是关于rsycn+inotify实时网络数据镜像备份的主要内容,如果未能解决你的问题,请参考以下文章

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

rsync+inotify实现代码实时同步

rsync+inotify实现数据实时备份

rsync+inotify-tools实时同步

rsync+inotify实现数据实时同步

rsync+inotify实时备份