中小型网站搭建-数据实时的复制-inotify/sersyncphp
inotify是一种强大的,细粒度的、异步的文件系统事件监控机制(软件),linux内核从2.6.13起,加入inotify支持,经过inotify能够监控文件系统中添加、删除、修改、移动等各类事件。linux
1.backup部署rsync服务端 /nfsbackupshell
drwxr-xr-x 2 rsync rsync 4096 Jun 5 22:21 /backup/
drwxr-xr-x 2 rsync rsync 4096 Jun 5 22:18 /nfsbackup/express
[root@backup ~]# cat /etc/rsync.password
rsync_backup:123456vim
-rw------- 1 root root 20 Jun 5 22:30 /etc/rsync.password缓存
######rsync_config___start
#created by hkping 15:01 2018-5-27
##rsyncd.conf start##
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.logbash
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password多线程
[backup]
path = /backup/
[nfsbackup]
path = /nfsbackup/并发
root 1481 1 0 22:15 ? 00:00:00 rsync --daemon
root 1484 1374 0 22:17 pts/0 00:00:00 grep rsyncdom
2.客户端配置,并测试nfs01推送数据rsync
sending incremental file list
sent 26 bytes received 8 bytes 68.00 bytes/sec
total size is 310 speedup is 9.12
3.安装epel源,nfs01配置inotify,安装inotify-tools
total 0
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_queued_events 可容纳的事件数量
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_user_instances 可运行的进程数
-rw-r--r-- 1 root root 0 Jun 5 22:47 max_user_watches 可监视文件数量
/usr/bin/inotifywait
/usr/bin/inotifywatch
一共安装了2个工具(命令),即inotifywait和inotifywatch
inotifywait(经常使用) 在被监控的文件或目录上等待特定文件系统事件(open、close,delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。
inotifywatch 收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。
-r 递归查询目录
-q 打印不多的信息,仅仅打印监控事件的信息
-m 始终保持事件监听状态
--timefmt 指定时间输出的格式
--format 打印使用指定的输出相似格式字符串
-e 指定要监视的事件列表
4.测试监控
5.在nfs01,写监控脚本,运行监控脚本
#!/bin/bash
#desc: watch /data dir && rsync to backup
inotifywait -mrq /data/ --format "%w%f" -e create,modify,close_write,moved_to|while read line
do
rsync -az /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
done
root 2088 1736 0 22:14 pts/1 00:00:00 /bin/sh /server/scripts/jiankong.sh
root 2090 2088 0 22:14 pts/1 00:00:00 /bin/sh /server/scripts/jiankong.sh
6.同步测试文件
total 8
-rw-r--r-- 1 rsync rsync 310 Jun 5 20:14 hosts
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy01.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy02.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy03.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy04.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy05.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy06.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy07.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy08.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy09.txt
-rw-r--r-- 1 rsync rsync 0 Jun 6 21:26 oldboy10.txt
总结:inotify优缺点:
inotify优势:监控文件系统事件变化,经过同步工具实现实时数据同步。
inotify缺点:
1)并发若是大于200个文件(10-100K),同步就会有延迟。
2)咱们前面写的脚本,每次都是所有推送一次,但确实是增量的。
3)监控到事件后,调用rsync同步是单进程的,sersync多进程同步。既然有了inoitfy-tools,为何还要开发sersync?
sersync功能多:(inotify+rsync命令)
1)支持经过配置文件管理
2)真正的守护进程socket
3)能够对失败文件定时重传(定时任务功能)
4)第三方的HTTP接口(例如:更新cdn缓存)
5)默认多线程rsync同步
安装sersync软件
[root@nfs01 /]# mkdir -p /home/oldboy/tools
[root@nfs01 /]# cd /home/oldboy/tools
[root@nfs01 tools]# rpm -qa lrzsz
lrzsz-0.12.20-27.1.el6.x86_64
[root@nfs01 tools]# rz -E
rz waiting to receive.
[root@nfs01 tools]# ll
total 692
-rw-r--r-- 1 root root 708025 Jun 6 22:20 sersync_installdir_64bit.zip
[root@nfs01 tools]# unzip sersync_installdir_64bit.zip
Archive: sersync_installdir_64bit.zip
creating: sersync_installdir_64bit/
creating: sersync_installdir_64bit/sersync/
creating: sersync_installdir_64bit/sersync/bin/
inflating: sersync_installdir_64bit/sersync/bin/sersync
creating: sersync_installdir_64bit/sersync/conf/
inflating: sersync_installdir_64bit/sersync/conf/confxml.xml
creating: sersync_installdir_64bit/sersync/logs/
[root@nfs01 tools]# mv sersync_installdir_64bit/sersync/ /usr/local/
[root@nfs01 tools]# ll /usr/local/sersync/
total 12
drwxr-xr-x 2 root root 4096 Dec 23 2012 bin
drwxr-xr-x 2 root root 4096 Dec 23 2012 conf
drwxr-xr-x 2 root root 4096 Dec 23 2012 logs
[root@nfs01 tools]# ll /usr/local/sersync/bin/sersync
-rw-r--r-- 1 root root 1810128 Oct 26 2011 /usr/local/sersync/bin/sersync
[root@nfs01 tools]# chmod +x /usr/local/sersync/bin/sersync
[root@nfs01 tools]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 188 bytes received 27 bytes 143.33 bytes/sec
total size is 310 speedup is 1.44
[root@nfs01 tools]# ln -s /usr/local/sersync/bin/sersync /usr/local/bin/
[root@nfs01 tools]# sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其余模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其余模块,使用 -m socket 开启socket模块
参数-m:单独启用其余模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
<localpath watch="/opt/tongbu">
<remote ip="127.0.0.1" name="tongbu1"/>
30,31c30,31
< <commonParams params="-artuz --delete"/>
< <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/><commonParams params="-artuz"/> <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>36c36
< <failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once--><failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
[root@nfs01 conf]# cat confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.).svn"></exclude>
<exclude expression="(.).gz"></exclude>
<exclude expression="^info/"></exclude>
<exclude expression="^static/"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync> <localpath watch="/data"> <remote ip="172.16.1.41" name="nfsbackup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz --delete"/> <auth start="true" users="rsynz_backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once--> <crontab start="false" schedule="600"><!--600mins--> <crontabfilter start="false"> <exclude expression="*.php"></exclude> <exclude expression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync> <plugin name="command"> <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> <filter start="false"> <include expression="(.*)\.php"/> <include expression="(.*)\.sh"/> </filter> </plugin> <plugin name="socket"> <localpath watch="/opt/tongbu"> <deshost ip="192.168.138.20" port="8009"/> </localpath> </plugin> <plugin name="refreshCDN"> <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> <sendurl base="http://pic.xoyo.com/cms"/> <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> </localpath> </plugin>
</head>
rsync the directory recursivly to the remote servers onceworking please wait...execute command: cd /data && rsync -artuz --delete -R --delete ./ rsynz_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1 run the sersync: watch path is: /data 表示成功运行