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 Exclude all events on files matching the extended regular expression
排除指定数据信息不要进行监控 (区分大小写识别信息)
--excludei Like --exclude but case insensitive.
排除指定数据信息不要进行监控 (忽略大小写识别信息)
将无用的信息进制输出:
-q|--quiet Print less (only print events).
输出少量信息(只输出事件信息)
知识输出信息格式:
--format Print using a specified printf-like format string; read the man page for more details.
指定输出信息格式
%e 显示触发事件信息
%w 显示监控目录信息
%f 触发事件数据信息
--timefmt strftime-compatible format string for use with %T in --format string.
定义显示的时间格式信息(时间格式的定义和 date命令类似)
-c|--csv Print events in CSV format. (MySQL)
-e|--event [ -e|--event ... ] 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 数据信息 [email protected]::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 [email protected]::backup --password-file=/etc/rsync.password
done
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/
cd /usr/local/sersync/
vim confxml.xml
6 --- 排除指定数据不要进行同步(默认关闭)
7
8
9
10
11
12 --- 监控事件信息
13
14
15
16
17
18
19
20
21
24 --- 实现实时同步配置
25
26
27
28
29
30
31
32
33
34
35
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