inotify or sersync实时同步

本文深入探讨了实时数据同步的重要性,介绍了inotify和rsync在实时数据同步中的应用,包括软件部署、工作原理及参数详解,并展示了sersync软件如何结合inotify与rsync实现高效数据同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 为什么要用实时同步服务

因为定时任务有缺陷,一分钟以内的数据无法进行同步,容易造成数据丢失

2 实时同步的工作原理

1.创建好存储数据的目录
2.利用实时同步的软件监控这个备份的数据目录
3.利用rsync服务进行数据的推送备份

3 实时同步的软件概念介绍

3.1 inotify软件

inotify是一种强大的。细粒度的。异步的文件系统系统事件监控机制
加入inotify支持,通过inotify可以监控文件系统中添加删除创建修改等等

4 实时同步服务器软件部署

4.1 inotify软件安装

安装方式 yum安装或者编译安装

[root@nfs01 ~]# yum -y install inotify-tools
[root@nfs01 ~]# rpm -ql inotify-tools
/usr/bin/inotifywait     <----实现对数据目录信息变化监控
/usr/bin/inotifywatch  <----监控数据信息变化,对变化的数据进行统计
[root@nfs01 ~]# cd /proc/sys/fs/inotify/  <----
[root@nfs01 inotify]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 28 14:53 max_queued_events
-rw-r--r--. 1 root root 0 Oct 28 14:53 max_user_instances
-rw-r--r--. 1 root root 0 Oct 28 14:53 max_user_watches

max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
默认只能监控8192个文件
max_user_instances:设置每隔用户可以运行的inotifywait或inotifywatch命令的进程数
默认每个用户可以开启inotify服务128个进程
max_queued_events:设置inotify实例事件队列可容纳的事件数量
默认事件队列监控长度是16384(排队)

4.2 将rsync守护进程模式部署完毕

在备份服务器部署

1.安装rsync
2.编辑配置文件
3.创建目录管理的用户
4.创建目录并授权
5.创建认证文件,编写认证用户密码,设置权限600
6.启动rsync服务

客户端部署

1.安装rsync
2.创建认证文件并授权600
3.链接数据同步

4.3 要让inotify软件和rsync软件建立链接(shell脚本)

4.3.1rsync应用命令参数

rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
inotify软件应用
inotifywait

参数含义
-m --monitor始终保持事件监听的状态
-d --daemon类似于-m在后台运行
-r递归监控
-q–quiet
–timefmt设定日期的格式,man strftime
–format输出命令执行过程中输出的信息格式
%w监控文件或者目录的名称信息
%f:目录下触发事件的文件或目录信息
%e:显示发生的事件,不同的事件信息用逗号分隔
%T:显示输出时间格式中定义的时间格式信息
-e指定监控的事件信息
access:文件或者目录被读取
modify:文件或目录被写入
attrib:文件或目录属性改变
close_write:文件或目录关闭。在写入模式打开之后关闭的
open:文件或目录打开
moved_to:文件或目录被移动到监控的目录中
moved_from:文件或目录被移动从监控目录中
move:不管移入移出都触发事件
create:创建
delete:删除

总结常用: create delete moved_to close_write

实践:

inotifywait命令
inotifywait -mrq --timefmt "%F" --format "%T %w%f 件信息:%e" -e create,delete,close_write,moved_to   /date  
精简
inotifywait -mrq --format "%w%f" -e create,delete,close_write,moved_to   /date

以上为文件实时同步过程,所需要的重要监控命令

4.4 同步方法一:直要有文件变化就推动整个目录

[root@nfs01 ~]# vim /server/scripts/inotify.sh   
#!/bin/bash
IP=172.16.1.41
DIR=/date
/usr/bin/inotifywait -mrq --format '%w%f' -e create,delete,close_write,moved_to  $DIR |\
while read file
 do
   rsync -az $DIR --delete  rsync_backup@${IP}::backup --password-file=/etc/rsync.password
 done

4.5 同步方法二:只有删除文件才会同步整个目录

[root@nfs01 date]# vim /server/scripts/inotify.sh   
#!/bin/bash
IP=172.16.1.41
DIR=/date/
/usr/bin/inotifywait -mrq --format '%w%f' -e create,delete,close_write,moved_to  $DIR |\
while read file
 do
   if [ -f $file ];then
   file=`echo $file|sed "s#$DIR##g"`
   cd $DIR&& \
   rsync -az $file --delete  rsync_backup@${IP}::backup/$file --password-file=/etc/rsync.pa
ssword
   else
   rsync -az $DIR --delete  rsync_backup@${IP}::backup --password-file=/etc/rsync.password
   fi
done

4.6 测试
在这里插入图片描述

5 sesync软件

seync利用inotify与rsync技术实现对服务器数据实时同步的解决方案其优点是只对变化的目录数据操作,甚至是一个文件不同的部分进行同步,所以其优势大大超过使用挂接文件系统或scp等方式进行镜像同步。
软件参考链接:https://github.com/wsgzao/sersync

5.1 下载安装

下载上传到文件内
解压

unzip sersync_installdir_64bit.zip

移动

[root@nfs01 sersync_installdir_64bit]# mv sersync/ /usr/local/

tree检查

[root@nfs01 sersync_installdir_64bit]# tree /usr/local/sersync/
/usr/local/sersync/
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── logs

3 directories, 2 files

5.2 编辑配置文件

[root@nfs01 ~]# ll /usr/local/sersync/conf/confxml.xml      
-rw-r--r--. 1 root root 2214 Oct 26  2011 /usr/local/sersync/conf/confxml.xml
[root@nfs01 ~]# vim /usr/local/sersync/conf/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
</filter>
说明:数据过滤排除
<inotify>
        <delete start="true"/>           	###删除事件
        <createFolder start="true"/>   	###创建文件夹
        <createFile start="false"/>	###创建文件
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>
#说明:类似于inotify的-e参数 可选
    <sersync>
        <localpath watch="/opt/tongbu">		###/opt/tongbu指定监控的目录        
            <remote ip="127.0.0.1" name="tongbu1"/>	     ###备份服务器的地址和模块
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>       ###指定rsync命令的参数
            <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>##用户密码
            <userDefinedPort start="false" port="874"/><!-- port=874 -->     ###端口
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
###说明以上内容是数据相关的配置信息,必须修改相当于rsync的命令
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>

    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

5.3 启动

[root@nfs01 ~]# sersync -h
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
[root@nfs01 ~]# sersync -dro /usr/local/sersync/conf/confxml.xml  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值