22. inotify+rsync实时同步服务 & sersync实时同步服务
unit21 inotify+rsync实时同步服务,sersync实时同步服务。
1.部署环境:
[root@backup ~]# tail -5 /etc/rsyncd.conf
[backup]
path = /backup/
[nfsbackup]
path = /nfsbackup/
[root@backup ~]# cat /etc/rsync.password
rsync_backup:123456
[root@backup ~]# rsync --daemon
[root@backup ~]# chkconfig |grep rsync
[root@backup ~]# ps -ef |grep rsync
root 1504 1 0 09:59 ? 00:00:00 rsync --daemon
root 1508 1471 0 09:59 pts/0 00:00:00 grep rsync
[root@backup ~]# echo "rsync --daemon" >>/etc/rc.local
[root@backup ~]# tail -1 /etc/rc.local
rsync --daemon
[root@backup ~]# ll -d /nfsbackup/
drwxrwxrwt 3 rsync rsync 4096 Jan 23 12:41 /nfsbackup/
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password //免密码推送
Password:
sending incremental file list
hosts
sent 188 bytes received 27 bytes 61.43 bytes/sec
total size is 303 speedup is 1.41
[root@backup ~]# ll /nfsbackup/
total 4
-rw-r--r-- 1 rsync rsync 303 Jan 22 13:04 hosts
[root@backup ~]# tail -f /var/log/rsyncd.log
2018/01/30 10:08:00 [1543] connect from nfs01 (172.16.1.31)
2018/01/30 10:08:03 [1543] rsync to nfsbackup/ from rsync_backup@nfs01 (172.16.1.31)
2018/01/30 10:08:03 [1543] receiving file list
2018/01/30 10:08:03 [1543] sent 95 bytes received 248 bytes total size 303
2.安装inotify与实践
(1)rsync+inotify组合
rsync(remote sync)远程同步工具,通过rsync可以实现对远程服务器数据的增量备份同步,但是rsync自身也有瓶颈,同步数据时,rsync采用核心算法对远程服务器的目标文件进行对比,只进行差异同步。若服务器的文件数量达到了百万甚至千万量级,那么文件对比将是非常耗时的。而且发生变化的往往是其中很少的一部分,这是非常低效的方式。
inotify的出现可以缓解rsync的不足之处,取长补短。
(2)inotify简介
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制(软件),linux内核从2.6.13开始加入对inotify的支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施这样监控的软件。国人周洋在金山公司开发的sersync。
inotify实际是一种事件驱动机制,它为应用程序监控文件系统事件提供了实时响应事件的机制,而无需通过诸如cron等的轮询机制来获取事件。cron等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify 基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界的事件机制相符合。
inotify的实现有几款软件:
inotify-tools,sersync,lsyncd
下面的inotify配置是建立在rsync服务上的配置过程:
[root@nfs01 ~]# yum repolist
法一:
[root@nfs01 ~]# yum install -y epel-release
法二:推荐!
epel是yum的一个软件源,里面包含了许多基本源里没有的软件。
http://mirrors.aliyun.com/help/epel
备份(如有配置其他epel源):
[root@nfs01 ~]# mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
[root@nfs01 ~]# mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
下载新repo 到/etc/yum.repos.d/:
[root@nfs01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@nfs01 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jan 30 10:26 max_queued_events
-rw-r--r-- 1 root root 0 Jan 30 10:26 max_user_instances
-rw-r--r-- 1 root root 0 Jan 30 10:26 max_user_watches
说明:
显示该三个文件说明支持inotfy软件
max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。
max_user_instances:设置每个用户可以运行的inotify wait或者inotify watch命令的进程数。
max_user_watches:设置inotify wait或者inotify watch命令可以监视的文件的数量(单进程)。
安装
[root@nfs01 ~]# yum install -y inotify-tools
[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-1.el6.x86_64
[root@nfs01 ~]# rpm -ql inotify-tools
/usr/bin/inotifywait //重要!
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz
说明:
安装的2个命令inotifywait和inotifywatch。
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open,close,delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用。
inotifywatch:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计。
3.inotify监控参数详解
[root@nfs01 ~]# inotifywait --help
-r --recursive:递归查询目录。
-q --quiet:打印很少的信息,仅仅打印监控事件的信息。
-m --monitor:始终保持事件监听状态。
--excludei:排除文件或者目录时不区分大小写。
--timefmt:指定时间输出格式。
-e --event:通过此参数可以指定需要监控的事件。
常用监控事件:
create(创建文件或目录)
delete(删除文件或目录)
close_write(创建文件或文件内容是否修改)
moved_to(文件或目录重命名)
举例:
//
[root@nfs01 ~]# inotifywait -mrq /data/
/data/ OPEN,ISDIR
/data/ CLOSE_NOWRITE,CLOSE,ISDIR
/data/ CREATE 0130.txt
/data/ OPEN 0130.txt
/data/ ATTRIB 0130.txt
/data/ CLOSE_WRITE,CLOSE 0130.txt
/data/ DELETE 0130.txt
[root@nfs01 ~]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 29 12:08 test.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 29 12:19 upload.txt
[root@nfs01 ~]# touch /data/0130.txt
[root@nfs01 ~]# rm /data/0130.txt
rm: remove regular empty file `/data/0130.txt'? y
[root@nfs01 ~]#
//
[root@nfs01 ~]# inotifywait -mrq /data/ --timefmt "%y-%m-%d %H:%M" --format "%T %w%f"
18-01-30 11:22 /data/
18-01-30 11:22 /data/
18-01-30 11:22 /data/0130.txt
18-01-30 11:22 /data/0130.txt
18-01-30 11:22 /data/0130.txt
18-01-30 11:22 /data/0130.txt
18-01-30 11:22 /data/0130.txt
[root@nfs01 ~]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 29 12:08 test.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 29 12:19 upload.txt
[root@nfs01 ~]# touch /data/0130.txt
[root@nfs01 ~]# rm /data/0130.txt
rm: remove regular empty file `/data/0130.txt'? y
//
[root@nfs01 ~]# inotifywait -mrq /data/ --timefmt "%y-%m-%d %H:%M" --format "%T %w%f" -e create
18-01-30 11:26 /data/0130.txt
[root@nfs01 ~]# touch /data/0130.txt
//
[root@nfs01 ~]# inotifywait -mrq /data/ --timefmt "%y-%m-%d %H:%M" --format "%T %w%f %e"
18-01-30 11:33 /data/0130.txt CREATE
18-01-30 11:33 /data/0130.txt OPEN
18-01-30 11:33 /data/0130.txt ATTRIB
18-01-30 11:33 /data/0130.txt CLOSE_WRITE,CLOSE
18-01-30 11:33 /data/hosts CREATE
18-01-30 11:33 /data/hosts OPEN
18-01-30 11:33 /data/hosts MODIFY
18-01-30 11:33 /data/hosts CLOSE_WRITE,CLOSE
[root@nfs01 ~]# touch /data/0130.txt
[root@nfs01 ~]# cp /etc/hosts /data/
4.inotify监控脚本
练习:
[root@nfs01 ~]# mkdir -p /server/scripts
[root@nfs01 ~]# vim /server/scripts/jiankong.sh
[root@nfs01 ~]# cat /server/scripts/jiankong.sh
#!/bin/sh
#desc:watch /data dir && rsync to backup
inotifywait -mrq /data/ --format "%w%f" -e create,modify,close_write,moved_to |while read line
do
echo "the dir and file is :$line"
done
[root@nfs01 ~]# sh /server/scripts/jiankong.sh
the dir and file is :/data/nfs_test.txt
the dir and file is :/data/nfs_test.txt
[root@nfs01 ~]# ls /data/
0130.txt a hosts test.txt upload.txt
[root@nfs01 ~]# touch /data/nfs_test.txt
nfs服务器处创建脚本监控和推送:
[root@nfs01 ~]# cat /server/scripts/jiankong.sh
#!/bin/sh
#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 -avz /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
done
[root@nfs01 ~]# sh /server/scripts/jiankong.sh
[root@nfs01 ~]# touch /data/nfs{01..05}.txt
[root@nfs01 ~]# ls /data/
0130.txt a hosts nfs01.txt nfs02.txt nfs03.txt nfs04.txt nfs05.txt nfs_test.txt test.txt upload.txt
backup服务器处检查验证:
[root@backup ~]# ls /nfsbackup/
0130.txt a hosts nfs01.txt nfs02.txt nfs03.txt nfs04.txt nfs05.txt nfs_test.txt test.txt upload.txt
5.inotify优缺点
优点:
(1)监控文件系统事件变化,通过同步工具实现实时数据同步。
缺点:
(1)并发如果大于200个文件(10-100k),同步就会延迟。
(2)之前写的脚本每次都是全部推送一次,但确实是增量的。也可以只同步变化的文件,不变化的不理。
(3)监控到事件后,调用rsync同步是单进程的(加&并发),sersync多进程同步。
6.sersync功能特点(嵌入了inotify+rsync命令)
(1)支持通过配置文件管理。
(2)真正的守护进程socket。
(3)可以对失败文件定时重传(定时任务功能)。
(4)第三方的http接口(例如:更新CDN缓存)。
(5)默认多线程rsync同步。
[root@nfs01 ~]# /bin/sh /server/scripts/jiankong.sh & //可以让其开机自启动。
[1] 3628
[root@nfs01 ~]# ps -ef |grep jiankong
root 3628 3596 0 15:06 pts/2 00:00:00 /bin/sh /server/scripts/jiankong.sh
root 3630 3628 0 15:06 pts/2 00:00:00 /bin/sh /server/scripts/jiankong.sh
root 3632 3596 0 15:06 pts/2 00:00:00 grep jiankong
[root@nfs01 ~]# kill 3628
[root@nfs01 ~]# ps -ef |grep jiankong
root 3630 1 0 15:06 pts/2 00:00:00 /bin/sh /server/scripts/jiankong.sh
root 3634 3596 0 15:07 pts/2 00:00:00 grep jiankong
[1]+ Terminated /bin/sh /server/scripts/jiankong.sh
[root@nfs01 ~]# kill 3630
[root@nfs01 ~]# kill 3630
-bash: kill: (3630) - No such process
[root@nfs01 ~]# ps -ef |grep jiankong
root 3636 3596 0 15:07 pts/2 00:00:00 grep jiankong
7.sersync部署和使用流程
(1)解压和使用sersync安装包:
[root@nfs01 opt]# mkdir tools
[root@nfs01 opt]# cd tools/
[root@nfs01 tools]#
[root@nfs01 tools]# rpm -qa lrzsz
lrzsz-0.12.20-27.1.el6.x86_64
[root@nfs01 tools]# rz
[root@nfs01 tools]# ls
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]# tree sersync_installdir_64bit
sersync_installdir_64bit
└── sersync
├── bin
│ └── sersync
├── conf
│ └── confxml.xml
└── logs
4 directories, 2 files
[root@nfs01 tools]# cp sersync_installdir_64bit/sersync/ /usr/local/ -a
[root@nfs01 tools]# ls /usr/local/sersync/
bin conf logs
[root@nfs01 tools]# chmod +x /usr/local/sersync/bin/sersync
(2)需要配置rsync支持:
[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 303 speedup is 1.41
创建软链接:
[root@nfs01 tools]# ln -s /usr/local/sersync/bin/sersync /usr/local/bin/
[root@nfs01 tools]# sersync -h //--help
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参数,则默认执行同步程序
________________________________________________________________
[root@nfs01 ~]# cd /usr/local/sersync/
[root@nfs01 sersync]# tree
.
├── bin
│ └── sersync
├── conf
│ └── confxml.xml
└── logs
3 directories, 2 files
[root@nfs01 sersync]# cp conf/confxml.xml{,.ori}
[root@nfs01 sersync]# cd conf/
[root@nfs01 conf]# ls
confxml.xml confxml.xml.ori
(3)修改配置文件:
[root@nfs01 ~]# vim /usr/local/sersync/conf/confxml.xml 23 <sersync> 24 <localpath watch="/data"> 25 <remote ip="127.16.1.41" name="nfsbackup"/> 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 --delete"/> 31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> 32 <userDefinedPort start="false" port="874"/><!-- port=874 --> 33 <timeout start="false" time="100"/><!-- timeout=100 --> 34 <ssh start="false"/> 36 <failLog path="/var/log/rsync_fail_log" timeToExecute="60"/><!--default every 60mins execute once--> 检查: [root@nfs01 conf]# diff confxml.xml confxml.xml.ori 24,25c24,25 < <localpath watch="/data"> < <remote ip="172.16.1.41" name="nfsbackup"/> --- > <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-->
(4)启动sersync软件程序:
[root@nfs01 ~]# sersync -d -r -o /usr/local/sersync/conf/confxml.xml
出现watch path is: /data 表示sersync运行成功!
[root@nfs01 ~]# ps -ef|grep sersync
root 3705 1 0 15:39 ? 00:00:00 sersync -d -r -o /usr/local/sersync/conf/confxml.xml
root 3728 3613 0 15:42 pts/3 00:00:00 grep sersync
马虎大意,将备份服务器IP写错了!切记!
测试:
[root@nfs01 ~]# touch /data/nfs{11..15}.txt
[root@nfs01 ~]# ls /data/
0130.txt hosts nfs12.txt nfs14.txt nfs.txt se02.txt se04.txt test.txt
a nfs11.txt nfs13.txt nfs15.txt se01.txt se03.txt se05.txt upload.txt
[root@nfs01 ~]# mkdir -p /data/nfs1/nfs2
[root@backup ~]# ls /nfsbackup/
0130.txt hosts nfs12.txt nfs14.txt nfs.txt se02.txt se04.txt test.txt
a nfs11.txt nfs13.txt nfs15.txt se01.txt se03.txt se05.txt upload.txt
[root@backup ~]# ls /nfsbackup/
0130.txt hosts nfs11.txt nfs13.txt nfs15.txt se01.txt se03.txt se05.txt upload.txt
a nfs1 nfs12.txt nfs14.txt nfs.txt se02.txt se04.txt test.txt
官网:可靠高效的数据实时同步方式
https://github.com/wsgzao/sersync
前言
提到数据同步就必然会谈到rsync,一般简单的服务器数据传输会使用ftp/sftp等方式,但是这样的方式效率不高,不支持差异化增量同步也不支持实时传输。针对数据实时同步需求大多数人会选择rsync+inotify-tools的解决方案,但是这样的方案也存在一些缺陷(文章中会具体指出),sersync是国人基于前两者开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。
浙公网安备 33010602011771号