Rsync
Rsync 在我之前的备份服务器 博客详细记录了,这里不再重新记录。
Rsync 的不足
与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过crontab方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
inotify-tools
什么是inotify
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13开始引入,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。
inotify-tools:
inotify-tools是为linux下inotify文件监控工具提供的一套C的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。
安装inotify-tools:
yum install -y inotify-tools
命令行操作
/usr/bin/inotifywait
— 在被监控的文件或目录上等待特定文件系统事件(open,close,delete等)发生,执行后处于阻塞状态,适合shell脚本中使用。/usr/bin/inotifywatch
— 收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。
inotifywait命令常用参数详解
[root@backup ~]# inotifywait --help
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
下面用列表详细解释一下各个参数的含义
-e :–event的各种事件含义
测试监控事件
开启两个窗口
测试create
在第一个窗口输入如下内容:
[root@backup ~]# ls /backup
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e create /backup
在第二个窗口:输入如下内容
[root@backup ~]# cd /backup
[root@backup backup]# touch chensiqi
此时回到第一个窗口出现如下内容:
17 03 11 07 19 /backup/chensiqi
#命令说明
inotifywait:ionotify的命令工具
-mrq:-q只输入简短信息 -r,递归监控整个目录包括子目录 -m进行不间断持续监听
--timefmt 指定输出的时间格式
--format:指定输出信息的格式
-e create:制定监控的时间类型,监控创建create事件。
测试delte
第一个窗口输入如下信息:
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e delete /backup
第二个窗口输入如下信息:
[root@backup backup]# rm -rf chensiqi
此时第一个窗口会出现如下信息:
17 03 11 07 29 /backup/chensiqi
#命令说明:
-e delete:指定监听的事件类型。监听删除delete事件
测试close_write
第一个窗口输入如下信息:
inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e close_write /backup
第二个窗口输入如下信息:
[root@backup backup]# touch close_write.log
[root@backup backup]# echo 111 >> close_write.log
[root@backup backup]# rm -f close_write.log
此时第一个窗口会出现如下信息:
17 03 11 07 38 /backup/close_write.log
17 03 11 07 39 /backup/close_write.log
#命令说明:
-e close_write:指定监听类型。监听文件写模式的关闭。
测试move_to
第一个窗口输入如下信息:
[root@backup ~]# inotifywait -mrq --timefmt '%y %m %d %H %M' --format '%T %w%f' -e moved_to /backup
第二个窗口输入如下信息:
此时第一个窗口会出现如下信息:
[root@backup backup]# touch chensiqi
[root@backup backup]# mv chensiqi chen
[root@backup backup]# mkdir ddddd
[root@backup backup]# mv chen ddddd/
inotify监控命令格式
inotifywait -mrq --timefmt "%F" --format "%T %w %f 事件信息:%e" /data -e CREATE
create创建、delete删除、moved_to移入、close_write修改
sersync
可以使用 Rsync + inotify 编写脚本进行实时的数据同步,但是这种方式有很多缺点。所以我们一般不这样进行数据同步,而是使用 sersync
。它的功能更加强大。
- 支持通过配置文件管理
- 真正的守护进程socket
- 可以对失败文件定时重传(定时任务功能)
- 第三方的HTTP接口(例如:更新cdn缓存)
- 默认多进程rsync同步
**可以理解为 sersync 是 inotify + rsync **
部署sersync同步服务
- 需要去网站下载 rpm 包
https://github.com/wsgzao/sersync
- PS:软件尽量都统一保存在/server/tools目录中
- 解压软件压缩包,将解压的数据进行保存
unzip sersync_installdir_64bit.zip
root@nfs01 tools]# tree sersync_installdir_64bit
sersync_installdir_64bit
└── sersync
├── bin --- sersync软件命令目录
│?? └── sersync
├── conf --- sersync软件配置目录
│?? └── confxml.xml
└── logs --- sersync软件日志目录
[root@nfs01 tools]# mv sersync_installdir_64bit/sersync/ /usr/local/
- 编写配置文件:
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 -->
33 <rsync>
说明:sersync 需要用到 rsync ,这一段是配置rsync 内容的
- 启动sersync服务程序
PS: sersync 不像之前用 yum 直接按照的软件那样,可以直接使用,需要到它指定的脚本路径执行。当然也可以修改一下环境变量
[root@nfs01 bin]# export PATH="$PATH:/usr/local/sersync/bin"
[root@nfs01 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
参数-d: 启用守护进程模式
参数-r: 在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-o: 指定配置文件,默认使用confxml.xml文件
-o /usr/local/sersync/conf/confxml.xml
## 停止实时同步服务
killall sersync
## 开机自动启动
/etc/rc.local <-- sersync -dro /usr/local/sersync/conf/confxml.xml