16.实时同步服务sersync(rsync+inotify)

实现实时同步的方法

①部署rsync守护进程,实现数据传输
②部署inotify监控服务,实现目录中数据变化监控
③部署sersync实时同步服务.将rsync和inotify服务结合,

简图:


image.png

实时同步服务器部署

部署rsync

②部署inotify监控服务,实现目录中数据变化监控

安装inotify
yum install -y inotify-tools

#查看有哪些命令文件
rpm -ql inotify-tools

/usr/bin/inotifywait   ---监控目录数据信息变化
/usr/bin/inotifywatch   ---对监控的变化信息进行统计

inotifywait命令的参数

inotifywait --help
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
    -m|--monitor    Keep listening for events forever.  Without
                    this option, inotifywait will exit after one
                    event is received.
实现实时监控,一直监控目录的数据变化

    -r|--recursive  Watch directories recursively.
递归监控, 用于监控子目录

    -q|--quiet      Print less (only print events).
减少信息的输出

--format <fmt>  Print using a specified printf-like format
                    string; read the man page for more details.
指定输出信息的格式

    --timefmt <fmt> strftime-compatible format string for use with
                    %T in --format string.
指定输出的时间信息格式

    -e|--event <event1> [ -e|--event <event2> ... ]
        Listen for specific event(s).  If omitted, all events are 
        listened for.
指定监控的事件信息

inotifywait 的使用

开启实时监控, 目标文件夹/data
inotifywait -m /data
创建文件
touch /data/user13

创建文件/修改 监控信息输出如下:
/data/ CREATE user13         user13文件被创建
/data/ OPEN user13            打开创建的文件
/data/ ATTRIB user13         修改文件属性信息
/data/ CLOSE_WRITE,CLOSE  user13                保存,关闭文件


只开启创建事件的监控
[root@nfs01 /data]$ inotifywait -m /data -e CREATE
Setting up watches.
Watches established.
/data/ CREATE user12

开启递归监控
-r
inotifywait -mr /data -e CREATE
这样在/data的子目录下创建的文件就会被监控到
image.png

inotifywait 监控格式的常用参数

--timefmt "%F"    时间的格式
--format "%T %w %f 事件信息:%e" 输出内容的格式
inotifywait -mrq --timefmt "%F" --format "%T %w %f 事件信息:%e" /data -e CREATE

一般需要监控
create 创建
delete 删除
moved_to 移入
close_write 修改
image.png

企业应用:
防止系统文件被破坏
使用inotify进行实时监控,比如监控/etc目录,将操作记录入某个文档,
黑客一般会修改的文件:
passwd
/var/spool/cron/root

事件events信息

image.png

image.png

③部署sersync实时同步服务.将rsync和inotify服务结合, 部署在存储服务器,同步到备份服务器

下载sersync
链接:https://pan.baidu.com/s/1cA-nX6GVJQV1urOwu7mnmg 
提取码:4ylj

rz -y上传保存到服务器的/server/tools目录中

解压缩:
unzip sersync_installdir_64bit.zip
image.png
#/usr/local/用于存放第三方的软件工具
mv sersync_installdir_64bit/sersync/  /usr/local/

编写配置文件

cd /usr/local/sersync
vim /usr/local/sersync/conf/confxml.xml

<localpath watch="/data">  --指定需要监控的目录
 25             <remote ip="172.16.1.41" name="backup"/> --指定需要监控的ip    指定备份服务器模块信息
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-az"/> --参数:指定rsync命令参数
 31             <auth start="true" users="rsync_backup" passwordfile="rsync.password"/>  ---指定rysnc认证用户,和密码文件
 32             <userDefinedPort start="true" port="873"/><!-- port=874 -->  --指定873端口
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>

启动sersync

cd /usr/local/sersync/bin
赋予执行权限
chmod a+x sersync 
需要执行的命令添加到环境变量里
[root@nfs01 /usr/local/sersync/bin]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

export PATH="$PATH:/usr/local/sersync/bin"

查看命令帮助文档
[root@nfs01 /usr/local/sersync/bin]$ sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________


开启服务的命令
sersync -dro /usr/local/sersync/conf/confxml.xml 

停止服务
ps -ef|grep sersync
kill 掉
或者
yum install -y psmisc
killall sersync

实现开机自启
/etc/rc.local
sersync -dro /usr/local/sersync/conf/confxml.xml 

总结

  1. 实现实时同步的原理
    监控目录数据变化 --- inotify
    将数据进行传输 --- rsync
    将监控和传输进行整合 --- sersync
  2. 实现实时同步部署方法
    1. 部署rsync守护进程
    2. 部署inotify软件
    3. 部署sersync软件
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容