第 1 章 资料实时同步介绍
1.1 什么是实时同步:如何实现实时同步
A. 要利用监控服务(inotify),监控同步资料站群服务器目录中资讯的变化
B. 发现目录中资料产生变化,就利用 rsync 服务推送到备份站群服务器上
1.2 实现实时同步的方法
inotify+rsync 方式实现资料同步
sersync 方式实现实时资料同步
1.2.1 实时同步原理介绍
1.3 inotify+rsync 方式实现资料同步
1.3.1 Inotify 简介
Inotify 是一种强大的,细粒度的。非同步的档案系统事件监控机制,linux 核心从 2.6.13 起,加入了 Inotify 支援,通过 Inotify 可以监控档案系统中新增、删除,修改、移动等各种事件, 利用这个核心介面,第三方站群软件就可以监控档案系统下档案的各种变化情况,而 inotify-tools 正是实施这样监控的站群软件。国人周洋在金山公司也开发了类似的实时同步站群软件 sersync 。
提示资讯:
sersync 站群软件实际上就是在 inotify 站群软件基础上进行开发的,功能要更加强大些 ,多了定时重传机制,过滤机制了提供介面做 CDN,支援多执行绪橾作。
Inotify 实际是一种事件驱动机制,它为应用程式监控档案系统事件提供了实时响应事件的机制,而无须通过诸如 cron 等的轮询机制来获取事件。 cron 等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify 基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知介面,也与自然世界事件机制相符合。
inotify 的实现有几款站群软件:
inotify-tools,sersync,lrsyncd
1.3.2 inotify+rsync 使用方式
inotify 对同步资料目录资讯的监控
rsync 完成对资料资讯的实时同步
利用指令码进行结合
1.4 部署 inotify 站群软件的前提
需要 2.6.13 以后核心版本才能支援 inotify 站群软件。 2.6.13 核心之后版本,在没有安装 inotify 站群软件之前,应该有这三个档案。
[root@backup ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r–r– 1 root root 0 Oct 17 10:12 max_queued_events
-rw-r–r– 1 root root 0 Oct 17 10:12 max_user_instances
-rw-r–r– 1 root root 0 Oct 17 10:12 max_user_watches
1.4.1 三个重要档案的说明
档案
预设值
作用说明
max_user_watches
8192
设定 inotifywait 或 inotifywatch 命令可以监视的档案数量(单程序)
max_user_instances
128
设定每个使用者可以执行的 inotifywait 或 inotifywatch 命令的程序数
max_queued_events
16384
设定 inotify 例项事件(event)伫列可容纳的事件数量
1.4.2【服务优化】可以将三个档案的数值调大,监听更大的范围
1.4.3【官方说明】三个重要档案
[root@nfs01 ~]# man proc
/proc/sys/fs/inotify (since Linux 2.6.13)
This directory contains files max_queued_events,
max_user_instances, and max_user_watches, that can be used
to limit the amount of kernel memory consumed by the inotify interface.
for further details, see inotify(7).
通过 man 手册的第 7 级别中查到 inotify 的预设档案的详细说明。
[root@nfs01 ~]# man 7 inotify
/proc/sys/fs/inotify/max_queued_events
The value in this file is used when an application calls
inotify_init(2) to set an upper limit on the number of
events that can be queued to the corresponding inotify
instance. Events in excess of this limit are dropped, but
an IN_Q_OVERFLOW event is always generated.
/proc/sys/fs/inotify/max_user_instances
This specifies an upper limit on the number of inotify
instances that can be created per real user ID.
/proc/sys/fs/inotify/max_user_watches
This specifies an upper limit on the number of watches
that can be created per real user ID.
1.5 inotify 站群软件介绍及引数说明
1.5.1 两种安装方式
1)yum install -y inotify-tools
2)手工编译安装
注:
YUM 安装需要有 epel 源
http://mirrors.aliyun.com
手工编译安装方式需要到 github 上进行下载站群软件包
inotify 站群软件的参考资料连结:
https://github.com/rvoicilas/inotify-tools/wiki
1.5.2 inotify 主要安装的两个站群软件
inotifywait:(主要)
在被监控的档案或目录上等待特定档案系统事件(open close delete 等)发生,执行后处于阻塞状态,适合在 shell 指令码中使用
inotifywatch:
收集被监控的档案系统使用的统计资料,指档案系统事件发生的次数统计。
说明:在实时实时同步的时候,主要是利用 inotifywait 对目录进行监控
1.5.3 inotifywait 命令引数说明
引数
含义
-m, –monitor
(重要引数)
Keep listening for events forever. Without this option, inotifywait will exit after one event is received.
始终保持事件监听。
-d, –daemon
111
-r, –recursive
(重要引数)
Watch all subdirectories of any directories passed as arguments.
递回监控目录资料资讯变化
-o, –outfile
Print events to
列印事件到档案中,相当于标准正确输出
-s, –syslog
Output errors to syslog(3) system log module rather than stderr.
传送错误到 syslog 相当于标准错误输出
-q, –quiet
(重要引数)
If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches.
输出资讯少(只列印事件资讯)
–exclude
排除档案或目录
–excludei
排除档案或目录时,不区分大小写
–timefmt
(重要引数)
Print using a specified printf-like format string; read the man page for more details.
指定时间输出格式
–format
(重要引数)
Print using a specified printf-like formatstring; read the man page for more details.
列印使用指定的输出类似格式字串;即实际监控输出内容
-e
(重要引数)
Listen for specific event(s). If omitted, all events are listened for.
指定监听指定的事件,如果省略,表示所有事件都进行监听
以上的资讯可以通过 inotifywait –help 获得
1.5.4 -e[引数] 可以指定的事件型别
事件名称
事件说明
access
file or directory contents were read
档案或目录内容被读取
modify
file or directory contents were writterv
档案或目录内容被写入
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 –help 获得
1.5.4.1 【例项】inotifywait 监控中的事件测试
1 、建立事件
[root@nfs01 data]# touch test2.txt
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e create
17-10-17 11:19 /data/test2.txt 事件资讯: CREATE
2 、删除事件
[root@nfs01 data]# rm -f test1.txt
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e delete
17-10-17 11:28 /data/test1.txt 事件资讯: DELETE
3 、修改事件
[root@nfs01 data]# echo “132” > test.txt
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e close_write
17-10-17 11:30 /data/test.txt 事件资讯: CLOSE_WRITE,CLOSE
4 、移动事件 moved_to
[root@nfs01 data]# mv /etc/hosts .
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e moved_to
17-10-17 11:33 /data/hosts 事件资讯: MOVED_TO
移动事件 moved_from
[root@nfs01 data]# mv ./hosts /tmp/
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e moved_from
17-10-17 11:34 /data/hosts 事件资讯: MOVED_FROM
1.5.5 inotifywait 引数 –format
命令引数
引数说明
%w(重要引数)
事件出现时,监控档案或目录的名称资讯
%f(重要引数)
事件出现时,将显示监控目录下触发事件的档案或目录资讯,否则为空
%e(重要引数)
显示发生的事件资讯,不同的事件资讯用逗号进行分隔
%Xe
显示发生的事件资讯,不同的事件资讯有 x 进行分隔,可以修改 X 为指定分隔符
%T(重要引数)
输出时间格式中定义的时间格式资讯,通过 –timefmt option 语法格式指定时间资讯
这个格式是通过 strftime 函式进行匹配时间格式资讯的
以上的资讯可以通过 inotifywait –help 获得
1.5.6 inotifywait 引数–timefmt
命令引数
引数说明
%d(重要引数)
The day of the month as a decimal number(range 01 to 31)
每月的第几天,显示倍息为十进位制数(范围是 01-31 )
%m(重要引数)
The month as a decimal number (range 01 to 12).
显示月份,显示资讯为十进位制(范围 01-12 )
%M
The minute as a decimal number (range 00 to 59).
显示分钟,显示资讯为十进位制(范围 00-59 )
%y(重要引数)
The year as a decimal number without a century (range 00 to 99).
年份资讯,显示资讯为十进位制,并且没有世纪资讯
%Y
The year as a decimal number including the century.
年份资讯,显示资讯为十进位制,并且包含世纪资讯
%H
The hour as a decimal number using a 24-hour clock (range 00 to 23).
小时资讯,显示资讯为十进位制,使用 24 小时制(范围 00-23 )
说明:以上资讯可以通过 man strftime 资讯获取
1.5.6.1 修改输出的日期格式
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d/%m/%y %H:%M” –format “%T %w%f”
17/10/17 11:12 /data/test1.txt
1.5.7 -e[引数] 重要监控事件引数汇总表:
重要事件
包含事件
备注说明
close
close_write
close_nowrite
档案或目录关闭,不管读或是写模式
即包含写关闭与读关闭
close_write
create
包含档案建立事件,但不包含目录建立事件
move
moved_to
moved_from
档案或目录不管移动到或是移动出监控目录都触发事件
即包含资讯移入或移出监控目录事件
重要引数汇总:根据以上说明,在实际使用时,只要监控以下事件即可
create 建立、 delete 删除、 movedjto 移入、 close_write 修 改
inotifywait -mrq /data –format “%w%f” -e create,delete,moved_to,close_write
1.6 对 inotifywait 命令的测试
对 inotifywait 命令测试的说明:
需要开启两个连线视窗
1 视窗执行 inotifywait
2 视窗对资料夹进行操作,可在一视窗中检视出 inotifywait 的监控记录
1.6.1 、建立档案的逻辑↓
[root@nfs01 ~]# inotifywait /data
Setting up watches.
Watches established.
/data/ CREATE test1.txt
/data/ OPEN test1.txt
/data/ ATTRIB test1.txt
/data/ CLOSE_WRITE,CLOSE test1.txt
建立档案,inotifywait 显示建立档案的过程↑
[root@nfs01 data]# touch test1.txt
1.6.2 建立目录逻辑↓
[root@nfs01 data]# mkdir testdir
[root@nfs01 ~]#
/data/ CREATE,ISDIR testdir
1.6.3 监控子目录下的档案↓
[root@nfs01 data]# touch testdir/test01.txt
[root@nfs01 ~]# inotifywait -mrq /data
/data/testdir/ OPEN test01.txt
/data/testdir/ ATTRIB test01.txt
/data/testdir/ CLOSE_WRITE,CLOSE test01.txt
1.6.4 sed 命令修改逻辑
[root@nfs01 data]# sed ‘s#132#123#g’ test.txt -i
[root@nfs01 ~]# inotifywait -mrq /data –timefmt “%d-%m-%y %H:%M” –format “%T %w%f 事件资讯: %e” -e moved_from
/data/test.txt 事件资讯: OPEN
/data/sedDh5R8v 事件资讯: CREATE
/data/sedDh5R8v 事件资讯: OPEN
/data/test.txt 事件资讯: ACCESS
/data/sedDh5R8v 事件资讯: MODIFY
/data/sedDh5R8v 事件资讯: ATTRIB
/data/sedDh5R8v 事件资讯: ATTRIB
/data/test.txt 事件资讯: CLOSE_NOWRITE,CLOSE
/data/sedDh5R8v 事件资讯: CLOSE_WRITE,CLOSE
/data/sedDh5R8v 事件资讯: MOVED_FROM
/data/test.txt 事件资讯: MOVED_TO
sed 命令替换逻辑 :
01. 建立临时档案
02. 将原档案内容放置到临时档案中,修改替换临时档案中的内容,原有档案不做改动
03. 重新命名临时档案,覆盖原档案
1.6.5 inotifywait 监控中 -e 的引数使用
inotifywait -mrq /data –timefmt “%d/%m/%y %H:%M” –format “%T %w%f 事件资讯: %e” -e create
说明:表示只监听 create 事件
inotifywait -mrq /data –timefmt “%d/%m/%y %H:%M” –format “%T %w%f 事件资讯: %e”
说明:不指定-e 引数,表示监听所有事件
02. 删除事件 delete
# inotifywait -mrq /data –timefmt “%F %H:%M” –format “%T %w%f 事件资讯: %@e” -e delete
2017-10-17 11:28 /data/02.txt 事件资讯: DELETE
2017-10-17 11:28 /data/03.txt 事件资讯: DELETE
2017-10-17 11:28 /data/04.txt 事件资讯: DELETE
03. 修改事件 close_write
# inotifywait -mrq /data –timefmt “%F %H:%M” –format “%T %w%f 事件资讯: %@e” -e delete,close_write
2017-10-17 11:30 /data/oldgirl.txt 事件资讯: CLOSE_WRITE@CLOSE
2017-10-17 11:30 /data/.oldgirl.txt.swx 事件资讯: CLOSE_WRITE@CLOSE
2017-10-17 11:30 /data/.oldgirl.txt.swx 事件资讯: DELETE
2017-10-17 11:30 /data/.oldgirl.txt.swp 事件资讯: CLOSE_WRITE@CLOSE
2017-10-17 11:30 /data/.oldgirl.txt.swp 事件资讯: DELETE
2017-10-17 11:30 /data/.oldgirl.txt.swp 事件资讯: CLOSE_WRITE@CLOSE
2017-10-17 11:30 /data/.oldgirl.txt.swp 事件资讯: DELETE
04. 移动事件 moved_to
inotifywait -mrq /data –timefmt “%F %H:%M” –format “%T %w%f 事件资讯: %@e” -e delete,close_write,moved_to
2017-10-17 11:34 /data/hosts 事件资讯: MOVED_TO
1.7 实时同步命令引数示意图
第 2 章 inotify+rsync 实时同步服务部署
2.1 第一个里程碑:部署 rsync 服务
2.1.1 rsync 服务端部署
1) 站群软件是否存在
[root@backup ~]# rpm -qa |grep rsync
rsync-3.0.6-12.el6.x86_64
需求:查询到某个命令非常有用。但是不知道属于哪个站群软件包
yum provides rysnc
provides Find what package provides the given value
2) 进行站群软件服务配置
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = “backup dir by oldboy”
path = /backup
[nfsbackup]
comment = “nfsbackup dir by hzs”
path = /nfsbackup
3) 建立 rsync 管理使用者
[root@backup ~]# useradd -s /sbin/nologin -M rsync
4) 建立资料备份储存目录, 目录修改属主
[root@backup ~]# mkdir /nfsbackup/
[root@backup ~]# chown -R rsync.rsync /nfsbackup/
5) 建立认证使用者密码档案并进行授权 600
echo “rsync_backup:oldboy123” >>/etc/rsync.password
chmod 600 /etc/rsync.password
6) 启动 rsync 服务
rsync –daemon
至此服务端配置完成
[root@backup ~]# ps -ef |grep rsync
root 2076 1 0 17:05 ? 00:00:00 rsync –daemon
root 2163 1817 0 17:38 pts/1 00:00:00 grep –color=auto rsync
2.1.2 rsync 客户端配置
1) 站群软件是否存在
[root@backup ~]# rpm -qa |grep rsync
rsync-3.0.6-12.el6.x86_64
2) 建立安全认证档案,并进行修改许可权 600
echo “oldboy123” >>/etc/rsync.password
chmod 600 /etc/rsync.password
3) 测试资料传输
[root@nfs01 sersync]# rsync -avz /data rsync_backup@172.16.1.41::nfsbackup –password-file=/etc/rsync.password
sending incremental file list
data/
data/.hzs
data/.tar.gz
data/.txt
2.2 第二个里程碑:部署 inotify 服务
首先先确认是否有 epel 源用来安装 inotify-tools 站群软件
[root@nfs01 ~]# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base CentOS-6 – Base – mirrors.aliyun.com 6,706
epel Extra Packages for Enterprise Linux 6 – x86_64 12,401
extras CentOS-6 – Extras – mirrors.aliyun.com 46
updates CentOS-6 – Updates – mirrors.aliyun.com 722
repolist: 19,875
2.2.1 安装 inotify 站群软件
两种安装方式
1)yum install -y inotify-tools
2)手工编译安装
注:
手工编译安装方式需要到 github 上进行下载站群软件包
inotify 站群软件的参考资料连结:
https://github.com/rvoicilas/inotify-tools/wiki
2.2.2 检视 inotify 安装上的两个命令 (inotifywait,inotifywatch)
[root@nfs01 ~]# rpm -ql inotify-tools
/usr/bin/inotifywait #主要
/usr/bin/inotifywatch
2.2.2.1 inotifywait 和 inotifywatch 的作用:
一共安装了 2 个工具(命令),即 inotifywait 和 inotifywatch
inotifywait : 在被监控的档案或目录上等待特定档案系统事件(open close delete 等)发生,
执行后处于阻塞状态,适合在 shell 指令码中使用
inotifywatch : 收集被监控的档案系统使用的统计资料, 指档案系统事件发生的次数统计。
说明:yum 安装后可以直接使用,如果编译安装需要进入到相应站群软件目录的 bin 目录下使用
#命令 man 手册说明
# man inotifywait
inotifywait – wait for changes to files using inotify
使用 inotify 进行监控,等待产生变化的档案资讯
# man inotifywatch
inotifywatch – gather filesystem access statistics using inotify
使用 inotify 进行监控,收集档案系统访问统计佶息
2.3 第三个里程碑:编写指令码,实现 rsync+inotify 站群软件功能结合
2.3.1 rsync 服务命令:
rsync -avz –delete /data/ rsync_backup@172.16.1.41::nfsbackup –password-file=/etc/rsync.password
2.3.2 inotify 服务命令:
inotifywait -mrq /data -format “%w%f” -e create,delete,move_to,close_write
2.3.3 编写指令码:
[root@nfs01 sersync]# vim /server/scripts/inotify.sh
#!/bin/bash
inotifywait -mrq /data –format “%w%f” -e create,delete,moved_to,close_write|
while read line
do
rsync -az –delete /data/ rsync_backup@172.16.1.41::nfsbackup –password-
file=/etc/rsync.password
done
指令码说明:
for 回圈会定义一个条件,当条件不满足时停止回圈
while 回圈:只要条件满足就一直回圈下去
2.3.4 对指令码进行优化
#!/bin/bash
Path=/data
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq –format ‘%w%f’ -e create,close_write,delete /data | while read line
do
if [ -f $line ];then
rsync -az $line –delete rsync_backup@$backup_Server::nfsbackup –password-file=/etc/rsync.password
else
cd $Path &&
rsync -az ./ –delete rsync_backup@$backup_Server::nfsbackup –password-file=/etc/rsync.password
fi
done
2.4 第四个里程碑:测试编写的指令码
2.4.1 让指令码在后台执行
在/data 目录先建立 6 个档案
[root@nfs01 data]# sh /server/scripts/inotify.sh &
[root@nfs01 data]# touch {1..6}.txt
在 backup 站群服务器上,已经时候同步过去了 6 个档案。
[root@backup ~]# ll /nfsbackup/
total 8
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 1.txt
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 2.txt
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 3.txt
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 4.txt
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 5.txt
-rw-r–r– 1 rsync rsync 0 Oct 17 12:06 6.txt
2.5 利用 while 回圈语句编写的指令码停止方法(kill)
01. ctrl+z 暂停程式执行,kill -9 杀死
02. 不要暂停程式,直接利用杀手三剑客进行杀程序
说明:kill 三个杀手不是万能的,在程序暂停时,无法杀死;kill -9(危险)
2.5.1 检视后台都要哪些程式在执行
[root@nfs01 data]# jobs
[1]+ Running sh /server/scripts/inotify.sh &
2.5.2 fg 将后台的程式调到前台来
[root@nfs01 data]# fg 1
sh /server/scripts/inotify.sh
2.6 程序的前台和后台执行方法:
fg — 前台
bg — 后台
2.6.1 指令码后台执行方法
01. sh inotify.sh &
02. nohup sh inotify.sh &
03. screen 实现指令码程式后台执行
sh /server/scripts/inotify.sh &
nohup
nohup sh inotify.sh &
2.7 screen 实现指令码程式后台执行
2.7.1 经过 yum 查询发现 screen 命令属于 screen 包
[root@test ~]# yum provides screen
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
epel | 4.3 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on
: one terminal
Repo : base
Matched from:
2.7.2 安装 screen 站群软件
[root@test ~]# yum install -y screen
2.7.3 screen 命令的引数
在 shell 中输入 screen 即可进入 screen 检视
[root@test ~]# screen
Screen 实现后台执行程式的简单步骤:
screen -ls :可看 screen 会话
screen -r ID : 指定进入哪个 screen 会话
Screen 命令中用到的快捷键
Ctrl+a c :建立视窗
Ctrl+a w :视窗列表
Ctrl+a n :下一个视窗
Ctrl+a p :上一个视窗
Ctrl+a 0-9 :在第 0 个视窗和第 9 个视窗之间切换
Ctrl+a K(大写) :关闭当前视窗,并且切换到下一个视窗 ,
(当退出最后一个视窗时,该终端自动终止,并且退回到原始 shell 状态)
exit :关闭当前视窗,并且切换到下一个视窗
(当退出最后一个视窗时,该终端自动终止,并且退回到原始 shell 状态)
Ctrl+a d :退出当前终端,返回载入 screen 前的 shell 命令状态
Ctrl+a ” : 视窗列表不同于 w