1、实时同步服务原理/概念
1)需要部署好rsync守护进程服务,实现数据传输
2)需要部署好inotify服务,实现目录中数据变化监控
3)将rsync服务和inotify服务建立联系,将变化的数据进行实时备份传输
2、实时同步服务部署
1)部署rsync守护进程(之前已配置过:点击送达)
服务器端配置操作
客户端配置操作
2)配置inotify监控服务
a、安装软件(在备份服务器的客户端安装(我是在NFS服务器安装的,web服务器也可以))
yum install -y inotify-tools
b、熟悉命令的使用
我们先查看以下命令
rpm -ql inotify-tools
/usr/bin/inotifywait #监控目录数据信息变化
/usr/bin/inotifywatch #对监控的变化信息进行统计
inotifywait命令使用
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
-qq Print nothing (not even 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.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.
Events:
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 [参数] 监控的目录
-m|--monitor --- 实现一直监控目录的数据变化
-r|--recursive --- 进行递归监控
-q|--quiet --- 尽量减少信息的输出
--format <fmt> --- 指定输出信息的格式
--timefmt --- 指定输出的时间信息格式
-e|--event --- 指定监控的事件信息
创建文件监控信息输出
/test/ CREATE user1 --- 一个文件被创建
/test/ OPEN user1 --- 打开创建的文件
/test/ ATTRIB user1 --- 修改文件的属性信息
/test/ CLOSE_WRITE,CLOSE user1 --- 保存关闭一个文件
删除文件监控信息输出
修改文件监控信息输出
sed命令修改文件原理
/test/ OPEN user1 打开文件
/test/ CREATE sedioUSRr 创建一个临时文件(内存)
/test/ OPEN sedioUSRr 临时文件打开
/test/ ACCESS user1 读取源文件内容
/test/ MODIFY sedioUSRr 修改临时文件
/test/ ATTRIB sedioUSRr 临时文件属性变化
/test/ CLOSE_NOWRITE,CLOSE user1 不编辑直接关闭源文件
/test/ CLOSE_WRITE,CLOSE sedioUSRr 写入关闭临时文件
/test/ MOVED_FROM sedioUSRr 将临时文件移除
/test/ MOVED_TO user1 移入一个新的user1源文件
inotify参数事件说明:
inotify监控命令格式:
inotifywait -mrq --timefmt "%F" --format "%T %w %f 事件信息:%e" /data -e CREATE
主要监控 create创建、delete删除、moved_to移入、close_write修改
企业应用:防止系统重要文件被破坏
需要用到inotify进行实时一直监控 /etc passwd /var/spool/cron/root
3、sersync同步服务
a、下载软件,保留上传到linux服务器中
sersync
或者下载我用的sersync
上传linux服务器
rz -y — 选择需要上传的数据信息
PS:软件尽量都统一保存在/server/tools目录中
b、解压软件压缩包,将解压的数据进行保存
unzip sersync-master.zip
解压之后把sersync目录移动的 /usr/local/
mv sersync /usr/local/
c、编写配置文件:
vim conf/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>
说明:定义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 -->
修改24到32行内容
<localpath watch="/test">
25 <remote ip="172.16.1.41" name="backup"/>
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"/>
31 <auth start="true" users="rsync_backup" passwordfil
32 <userDefinedPort start="false" port="874"/><!-- por=874 -->
d、启动sersync服务程序
由于这边服务不是通过yum安装的所有不能用systemctl restart xxx 命令启动,要到它的命令目录中(bin)启动服务
先赋予目录执行权限
cd bin/
chmod a+x sersync
export PATH="$PATH:/usr/local/sersync/bin"
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin
查看命令文档
sersync -dro /usr/local/sersync/conf/confxml.xml 启动实时同步服务
yum install -y psmisc
killall sersync 停止实时同步服务
/etc/rc.local <-- sersync -dro /usr/local/sersync/conf/confxml.xml 开机自动启动
4、实时同步服务概念总结
1) 实现实时同步的原理
监控目录数据变化 — inotify
将数据进行传输 — rsync
将监控和传输进行整合 — sersync
2) 实现实时同步部署方法
- 部署rsync守护进程
- 部署inotify软件
- 部署sersync软件
请登录后查看评论内容