实时同步服务概述
A主机 新的数据 ---实时同步--- B主机 数据备份
在web服务器上/上传一个文件,实际是把数据传到 nfs服务器上 ,nfs服务器进行实时同步 ,至 backup服务器 到 /data
如何实现数据实时同步
1) 发现数据变化 inotify (监控软件)
2) 进行数据备份 rsync
实时同步服务监控软件部署
存储服务器部署
第一步: 安装监控数据变化软件
yum install -y inotify-tools
注: 确认epel源可以正常使用
第二步:监控命令使用
/usr/bin/inotifywait --- 监控数据变化命令
/usr/bin/inotifywatch --- 统计数据变化次数 添加 删除 修改
语法结构
inotifywait 参数 事件 监控目录
永久监控目录中数据变化: inotifywait -m 目录
-m|--monitor Keep listening for events forever.
保持永久监控
-d|--daemon Same as --monitor
类似-m参数
实现目录中数据递归监控: inotifywait -rm 目录
-r|--recursive Watch directories recursively.
监控目录中子目录数据变化
实时目录中数据排除监控功能:
--exclude <pattern> Exclude all events on files matching the extended regular expression <pattern>
排除指定数据信息不要进行监控 (区分大小写识别信息)
--excludei <pattern> Like --exclude but case insensitive.
排除指定数据信息不要进行监控 (忽略大小写识别信息)
将无用的信息进制输出:
-q|--quiet Print less (only print events).
输出少量信息(只输出事件信息)
知识输出信息格式:
--format <fmt> Print using a specified printf-like format string; read the man page for more details.
指定输出信息格式
%e 显示触发事件信息
%w 显示监控目录信息
%f 触发事件数据信息
--timefmt <fmt> strftime-compatible format string for use with %T in --format string.
定义显示的时间格式信息(时间格式的定义和 date命令类似)
-c|--csv Print events in CSV format. (MySQL)
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s)
指定监视事件信息
事件信息:inotify软件采用触发机制进行监控
access file or directory contents were read
文件或目录内容被读取时
modify file or directory contents were written
文件或目录内容被写入
attrib file or directory attributes changed
文件或目录属性信息被改变
close_write file or directory closed, after being opened in writeable mode ******
文件或目录关闭,写入新的信息之后
close_nowrite file or directory closed, after being opened in read-only mode
文件或目录关闭,只读模式进行关闭
close file or directory closed, regardless of read/write mode
文件或目录关闭,无论文件数据是否进行读或者写入
open file or directory opened
文件或目录被打开了
moved_to file or directory moved to watched directory
文件或目录移动到监控目录中
moved_from file or directory moved from watched directory
文件或目录从监控目录移除
move file or directory moved to or from watched directory *****
文件或数据不管是从目录中移除或是移入
create file or directory created within watched directory *******
文件或目录被创建出来在监控目录中
delete file or directory deleted within watched directory ******
在监控目录中文件或目录被删除
delete_self file or directory was deleted
在监控目录中文件或目录被删除
unmount file system containing file or directory unmounted
文件系统中包含文件或目录被卸载
实时同步利用脚本方式实现
编写脚本思路
1) 发现变化数据信息 inotifywait
inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"
2) 将变化数据进行传输 rsync
rsync -az 数据信息 rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
3) 监控操作和同步操作建立联系
shell脚本循环语句:
for 循环语句 --- 有限循环
while 循环语句 --- 无限循环(条件为真就会一直循环)
until 循环语句 --- 无限循环(条件为假就会一直循环)
脚本内容
vim rsync_data.sh
#!/bin/bash
inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"|while read data_info
do
rsync -az /data/ --delete rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
done
实时同步软件 - sersync
部署前提
inotify-tools 的安装
rsync 为守护进程模式
诞生过程
周洋 --- 数据实时同步 (inotify+rsync 脚本) --- 开发实时同步程序(inotify+rsync)
部署过程
第一个历程: 下载软件二进制包
https://github.com/wsgzao/sersync
第二个历程: 解压软件, 并保存到相应目录中
mkdir /server/tools -p
cd /server/tools
rz -E
unzip sersync-master.zip
cd sersync-master/
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
tree GNU-Linux-x86/
GNU-Linux-x86/
|-- confxml.xml
`-- sersync2
mkdir /usr/local/sersync
mv GNU-Linux-x86/* /usr/local/sersync/
第三个历程:修改sersync配置文件
cd /usr/local/sersync/
vim confxml.xml
6 <filter start="false"> --- 排除指定数据不要进行同步(默认关闭)
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify> --- 监控事件信息
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
24 <localpath watch="/opt/tongbu"> --- 实现实时同步配置
25 <remote ip="127.0.0.1" name="tongbu1"/>
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="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
第四个历程: 启动服务程序
cd /usr/local/sersync/
mv sersync2 sersync
vim /etc/profile
export PATH="$PATH:/usr/local/sersync"
. /etc/profile
sersync -h --- 查看 sersync 参数
参数-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/confxml.xml
实时同步服务出现异常
修改配置文件参数,进行调试
vim /usr/local/sersync/confxml.xml
<debug start="true"/>