您的位置:首页 > 其它

inotify+rsync实现实时同步部署

2016-03-04 22:15 429 查看
1.1.架构规划

1.1.1架构规划准备

服务器系统角色IP
Centos6.7 x86_64NFS服务器端(NFS-server-inotify-tools)192.168.1.14
Centos6.7 x86_64rsync服务器端(rsync热备服务器)192.168.1.17
[b]1.1.2架构图[/b]

[b]

[/b]

2.1 部署前检查

2.1.1 检查rsync热备服务器daemon是否起来

[root@backup data]# lsof -i:873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   3071 root    3u  IPv4  18684      0t0  TCP *:rsync (LISTEN)
rsync   3071 root    5u  IPv6  18685      0t0  TCP *:rsync (LISTEN)
[root@backup data]# ps -ef|grep rsync|grep -v grep
root      3071     1  0 Dec02 ?        00:00:00 rsync --daemon
[root@backup data]#


2.1.2 NFS服务器相对于rsync服务器来说是客户端,所以要配置一个客户端密码

[root@nfs-server ~]# echo "zhurui" >>/etc/rsync.password
[root@nfs-server ~]# cat /etc/rsync.password
zhurui
[root@nfs-server ~]# chmod 600 /etc/rsync.password
[root@nfs-server ~]# ll /etc/rsync.password
-rw-------. 1 root root 7 Jan 31 00:49 /etc/rsync.password
[root@nfs-server ~]#


2.1.3 NFS客户端手动推/data/目录下数据到rsync热备服务器/backup目录下

[root@nfs-server data]# showmount -e localhost ##检查共享目录
Export list for localhost:
/data 192.168.1.0/24
[root@nfs-server ~]# cd /data/
[root@nfs-server data]# ll
total 12
-rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 03:05 beautiful.txt
-rw-r--r--. 1 nfsnobody nfsnobody    9 Jan 30 04:33 f11.txt
-rw-r--r--. 1 root      root         0 Jan 30 04:41 fs.txt
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 03:05 huang
-rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 03:17 reew.txt
-rw-r--r--. 1 root      root         0 Jan 30 02:51 test1.txt
-rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 04:41 wd.txt
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 02:57 zhurui
-rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 02:47 zhurui1.txt
-rw-r--r--. 1 nfsnobody root         0 Jan 30 02:10 zhurui.txt
[root@nfs-server /]# rsync -avz /data/ rsync_backup@192.168.1.17::backup/ --password-file=/etc/rsync.password   ##推送数据命令
sending incremental file list
./
beautiful.txt
f11.txt
fs.txt
reew.txt
test1.txt
wd.txt
zhurui.txt
zhurui1.txt
huang/
zhurui/

sent 572 bytes  received 171 bytes  114.31 bytes/sec
total size is 9  speedup is 0.01
[root@nfs-server /]#


2.1.4 rsync服务器端检查/backup目录下数据





3.1 服务部署

3.1.1 NFS服务端安装inotify工具

inofity-tools下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

1)查看当前系统是否支持inotify

[root@nfs-server tools]# uname -r   ##内核版本,从kernel2.6.13开始支持
2.6.32-573.el6.x86_64
[root@nfs-server tools]# ls -l /proc/sys/fs/inotify/  ##查看是否支持
total 0
-rw-r--r-- 1 root root 0 Jan 31 01:34 max_queued_events
-rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_instances
-rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_watches
##显示当前三个文件表示支持


2)下载inotify源码包

[root@nfs-server tools]# ll
total 352
-rw-r--r--. 1 root root 358772 Mar 14  2010 inotify-tools-3.14.tar.gz


3)编译安装inotify

[root@nfs-server tools]# tar zxf inotify-tools-3.14.tar.gz
[root@nfs-server tools]# cd inotify-tools-3.14
[root@nfs-server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@nfs-server inotify-tools-3.14]# make&&make install


[root@nfs-server inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools  ##创建软链接
[root@nfs-server inotify-tools-3.14]# cd ..
[root@nfs-server local]# ll
total 44
drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
lrwxrwxrwx. 1 root root   30 Jan 31 01:58 inotify-tools -> /usr/local/inotify-tools-3.14/
drwxr-xr-x. 6 root root 4096 Jan 31 01:55 inotify-tools-3.14
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Nov 25 02:40 share
drwxr-xr-x. 2 root root 4096 Sep 23  2011 src
[root@nfs-server local]# cd inotify-tools
[root@nfs-server inotify-tools]# ll
total 16



提示编译成功后会生成4个目录,分别是:

[root@nfs-server /]# cd /usr/local/inotify-tools
[root@nfs-server inotify-tools]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jan 31 01:55 bin    ##inotify执行命令(二进制)
drwxr-xr-x. 3 root root 4096 Jan 31 01:55 include   ##inotify程序所需用的头文件
drwxr-xr-x. 2 root root 4096 Jan 31 01:55 lib  ##动态链接的库文件
drwxr-xr-x. 4 root root 4096 Jan 31 01:55 share  ##帮助文档


工具集合介绍:

一共安装了2个工具(命令),即inotifywait和inotifywatch

inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。

inotifywatch:收集被监视的文件系统使用度统计数据,指定文件系统事件发生的次数统计。

4)inotify命令常用参数详解

[root@nfs-server inotify-tools]# ./bin/inotifywait --help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-r|--recursive  Watch directories recursively.  ##递归查询目录
-q|--quiet      Print less (only print events)   ##打印很少的信息,仅仅打印监控相关的信息
-m|--monitor    Keep listening for events forever.  Without
this option, inotifywait will exit after one
event is received.   ##始终保持事件监听状态
--excludei <pattern>
Like --exclude but case insensitive.   ##排除文件或目录时,不区分大小写
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.   ##指定时间的输出格式


5)测试监控事件

开启两个窗口

1.测试create

第一步:
[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data  ##实行当前命令后,界面处于阻塞状态,只有在另外一个客户端测试时,才会显示监控状态
31/01/16 10:23 /data/ceshi.txt
31/01/16 10:24 /data/what
31/01/16 10:27 /data/a
31/01/16 10:27 /data/b
31/01/16 10:27 /data/c
31/01/16 10:27 /data/d
31/01/16 10:27 /data/e
31/01/16 10:27 /data/f

第二步:
克隆一个NFS客户端切换到/data目录下,新建ceshi.txt、what文件,观察监控的变化
[root@nfs-server data]# touch ceshi.txt
[root@nfs-server data]# mkdir /who
[root@nfs-server data]# touch what
[root@nfs-server data]#
[root@nfs-server data]# touch {a..f}


2、测试delete

[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data   ##监控/data目录删除时间

监控如下:

31/01/16 10:31 /data/reew.txt
31/01/16 10:31 /data/test1.txt

[root@nfs-server data]# rm -f reew.txt test1.txt   ##删除这两个文件
[root@nfs-server data]#


3、[b]同时测试create、delete[/b]

第一步:
[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /data
监控输出信息:
31/01/16 10:35 /data/hero.sh
31/01/16 10:35 /data/a
31/01/16 10:35 /data/f
31/01/16 10:36 /data/b
31/01/16 10:36 /data/c
31/01/16 10:36 /data/d
31/01/16 10:36 /data/e
第二步:
[root@nfs-server data]# touch hero.sh
[root@nfs-server data]# ll
[root@nfs-server data]# rm -f [a..f]
[root@nfs-server data]# rm -f {a..f}
[root@nfs-server data]#


一般工作中使用到:[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data

工作环境中通过脚本实时同步:

#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait
$inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data
\
|while read file
do
cd / &&
rsync -az /data --delete rsync_backup@192.168.1.17::backup \
--password-file=/etc/rsync.password
done


测试:

脚本属于开启状态:
[root@nfs-server scripts]# sh -x inotify.sh
+ inotify=/usr/local/inotify-tools/bin/inotifywait
+ read file
+ /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data
+ cd /
+ rsync -az /data --delete rsync_backup@192.168.1.17::backup --password-file=/etc/rsync.password
+ read file
+ cd /
+ rsync -az /data --delete rsync_backup@192.168.1.17::backup --password
接着在NFS客户端/data目录下创建文件
[root@nfs-server data]# touch {a..z}
[root@nfs-server data]# echo i am best >a  ##修改a中的内容,查看热备服务器是否同步

rsync热备服务器端显示如下:
[root@backup //]# cd /backup/
[root@backup backup]# cd data/
[root@backup data]# ll
total 0
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 a
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 b
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 c
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 d
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 e
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 f
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 g
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 h
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 i
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 j
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 k
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 l
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 m
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 n
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 o
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 p
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 q
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 r
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 s
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 t
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 u
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 v
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 w
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 x
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 y
-rw-r--r--. 1 rsync rsync 0 Mar  4  2016 z
[root@backup data]# cat a
i am best   ##显示已经实时同步过来
[root@backup data]#


6)优化

[root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches
[root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events


[b]结论:经过测试,每秒200个文件并发,数据同步几乎无延迟(小于1秒)[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: