一、实时同步服务概述
A主机 新的数据 -实时同步- B主机 数据备份
如何实现数据实时同步:
1)发现数据变化 inotify(监控软件)
2)进行数据备份 rsync
二、实时同步服务监控软件部署:
存储服务器部署
1.安装监控数据变化软件
条件:确认epel源可以正常使用
yum install -y inotify-tools
2.监控命令使用
/usr/bin/inotifywait --- 监控数据变化命令
/usr/bin/inotifywatch --- 统计数据变化次数 添加 删除 修改
inotifywait [参数] 事件 目录
· 永久监控目录中数据变化: inotifywait -m 目录
-m|--monitor Keep listening for events forever.
保持永久监控
· 实现目录中数据递归监控: inotifywait -rm 目录
-r|--recursive Watch directories recursively.
监控目录中子目录数据变化
-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命令类似)
[root@nfs ~ 18:05:59]# inotifywait -mrq --timefmt "%F %T" --format "%T %w %f %e" /data - e "close_write,move,create,delete"
2022-12-06 18:08:57 /data/ web.txt CLOSE_WRITE,CLOSE
[root@nfs ~ 18:09:11]# inotifywait -mrq --timefmt "%y %m %d" --format "%T %w %f %e" /data -e "close_write,move,create,delete"
22 12 06 /data/ web.txt CLOSE_WRITE,CLOSE
-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
文件系统中包含文件或目录被卸载
利用脚本实现实时同步: inotifywait + rsync
利用实时同步软件实现: sersync
三、实时同步利用脚本方式实现:
编写脚本思路:
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
-
监控操作和同步操作建立联系
shell脚本循环语句:
· for 循环语句 --- 有限循环
· while 循环语句 --- 无限循环
条件为真就会一直循环
· until 循环语句 --- 无限循环
条件为假就会一直循环脚本解释过程: inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"|while read data_info 信息 --- 赋值 买一 --- 循环动作 增一 do rsync -az $data_info --delete rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password done 脚本内容: # cat /server/scripts/sync_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