中小型网站搭建-数据实时的复制-inotify/sersync

inotify是一种强大的,细粒度的、异步的文件系统事件监控机制(软件),linux内核从2.6.13起,加入inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件。

1. backup部署rsync服务端  /nfsbackup目录

# mkdir -p /nfsbackup/

# mkdir -p /backup/

# useradd -s /sbin/nologin -M rsync

# chown -R rsync.rsync /backup

# chown -R rsync.rsync /nfsbackup

# ll -d /backup/ /nfsbackup/

drwxr-xr-x 2 rsync rsync 4096 Jun  5 22:21 /backup/

drwxr-xr-x 2 rsync rsync 4096 Jun  5 22:18 /nfsbackup/

# echo 'rsync_backup:123456' >/etc/rsync.password

[root@backup ~]# cat /etc/rsync.password

rsync_backup:123456

# chmod 600 /etc/rsync.password

# ll /etc/rsync.password

-rw------- 1 root root 20 Jun  5 22:30 /etc/rsync.password

# vim /etc/rsyncd.conf

######rsync_config_______________start

#created by hkping 15:01 2018-5-27

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

#hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

path = /backup/

[nfsbackup]

path = /nfsbackup/

# rsync --daemon

# echo 'rsync --daemon' >> /etc/rc.local

# ps -ef|grep rsync

root       1481      1  0 22:15 ?        00:00:00 rsync --daemon

root       1484   1374  0 22:17 pts/0    00:00:00 grep rsync

2. 客户端nfs01配置,并测试nfs01推送数据rsync

# echo '123456' > /etc/rsync.password

# chmod 600 /etc/rsync.password

# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password

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

# yum install epel-release -y 一般之前优化已经有了epel源(可不做)

关键参数说明

# ls -l /proc/sys/fs/inotify/

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 可监视文件数量

# yum install inotify-tools -y

# rpm -ql inotify-tools

/usr/bin/inotifywait

/usr/bin/inotifywatch

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

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

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

-r   递归查询目录

-q 打印很少的信息,仅仅打印监控事件的信息

-m 始终保持事件监听状态

--timefmt 指定时间输出的格式

--format 打印使用指定的输出类似格式字符串

-e 指定要监视的事件列表

4. 测试监控,复制nfs01窗口测试

# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e create 创建

# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e delete 删除

# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e close_write 文件内容是否修改

# inotifywait -mrq /data/ --timefmt '%d/%m/%y %H:%M' --format "%T %w%f" -e moved_to 重命名

5. 在nfs01,写监控脚本,运行监控脚本

# mkdir -p /server/scripts

# vim jiankong.sh

#!/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

# sh /server/scripts/jiankong.sh

# /bin/sh /server/scripts/jiankong.sh & 把监控脚本放在后台运行

# ps -ef|grep jiankong

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. 同步测试文件

# touch /data/oldboy{01..10}.txt

# ll /nfsbackup/

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同步

建议:

(1)当同步的目录数据量不大时,建议使用rsync+inotify

(2)当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync

安装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 上传文件sersync_installdir_64bit.zip

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/ 创建软连接,以后可以直接使用sersync命令

[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参数,则默认执行同步程序

[root@nfs01 conf]# cd /usr/local/sersync/conf

[root@nfs01 conf]# cp confxml.xml confxml.xml.ori

[root@nfs01 conf]# vim confxml.xml

:set nu 显示行号

[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-->

[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>

[root@nfs01 conf]# sersync -dro /usr/local/sersync/conf/confxml.xml

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

option: -d run as a daemon

option: -r rsync all the local files to the remote servers before the sersync work

option: -o config xml name:  /usr/local/sersync/conf/confxml.xml

daemon thread num: 10

parse xml config file

host ip : localhost host port: 8008

daemon start,sersync run behind the console

use rsync password-file :

user is rsynz_backup

passwordfile is /etc/rsync.password

config xml parse success

please set /etc/rsyncd.conf max connections=0 Manually

sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)

Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)

please according your cpu ,use -n param to adjust the cpu rate

------------------------------------------

rsync the directory recursivly to the remote servers once

working 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   表示成功运行

测试nfs01 /data目录下增删文件,是否同步到backup /nfsbackup目录

inotify+rsync sersync+rsync实时同步服务的更多相关文章

  1. Rsync+Sersync数据实时同步(双向)

    Rsync+Sersync数据实时同步(双向) 服务介绍 一.为什么要用rsync+sersync架构? 1.sersync是基于inotify开发的,类似于inotify-tools的工具 2.se ...

  2. Rsync+sersync文件实时同步

    一.为什么要用Rsync+sersync架构1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改 ...

  3. centos6.6配置rsync+sersync实现实时同步分布式多客户端分发同步

    1.sersync项目: sersync项目利用inotify与rsync技术实现对服务器数据实时同步到解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rsync是目 ...

  4. Rsync+sersync实现实时同步

    介绍: sersync主要用于服务器同步,web镜像等功能.基于boost1.43.0,inotify api,rsync command.开发.目前使用的比较多的同步解决方案是inotify-too ...

  5. rsync+sersync多线程实时同步

    一.sersync优点 1)使用c++编写,对linux系统文件产生的临时文件和重复文件操作会进行过滤,在结合rsync同步的时候,会减少运行时消耗的本地及网络资源,因此速度更快. 2)相比较inot ...

  6. 文件触发式实时同步 Rsync+Sersync Rsync+Inotify-tools

    一.概述 1.Rsync+Sersync 是什么? 1)Sersync使用c++编写基于inotify开发的触发机制: 2)Sersync可以监控所监听的目录发生的变化(包括新建.修改.删除),具体到 ...

  7. rsync 与 inotify 的使用 & 实现实时同步备份

    今日内容 rsync 内容详细 上一篇内容问题 1.yum源问题 2.VPN链接正常,但是没办法通过172 3.VPN链接时,出现了DNS错误 4.掩码不对 5.openvpn开启错误 复制的命令 1 ...

  8. rsync+inotify-tools文件实时同步

    rsync+inotify-tools文件实时同步案例 全量备份 Linux下Rsync+sersync实现数据实时同步完成. 增量备份 纯粹的使用rsync做单向同步时,rsync的守护进程是运行在 ...

  9. inotify软件部署及实时同步

    声明:博主使用的是CentOS6.9的系统 参考资料: https://github.com/rvoicilas/inotify-tools/wiki http://www.ibm.com/devel ...

  10. (转)Linux系统sersync数据实时同步

    Linux系统sersync数据实时同步 原文:http://blog.csdn.net/mingongge/article/details/52985259 前面介绍了以守护进程的方式传输或同步数据 ...

随机推荐

  1. Visual Studio 2015、2013、2012、2010、2008、2005各版本下载+有效密钥激活

    Visual Studio是微软发布的一个集成开发工具,业内一般简称为VS,广泛应用于Windows软件开发.网站开发等,是目前十分流行的windows应用程序的集成开发工具,如果大家不了解,可以简单 ...

  2. vi/vim打开文件提示Found a swap file by the name

    问题分析 有一次在远程连接主机时,用vi打开文件my.ini却提示:Found a swap file by the name ".my.ini.swp".百度了下才知道,原来在使 ...

  3. UVa12716:gcd等于xor(打表+类素数筛+差分约束)

    紫书给的分析缺少一些证明性的东西,将我自己的OneNote笔记贴在这里.

  4. Machine Learning Codeforces - 940F(带修莫队) && 洛谷P4074 [WC2013]糖果公园

    以下内容未验证,有错请指正... 设块大小为T,则块数为$\frac{n}{T}$ 将询问分为$(\frac{n}{T})^2$块(按照左端点所在块和右端点所在块分块),同块内按时间从小到大依次处理 ...

  5. 洛谷 P1067 多项式输出

    P1067 多项式输出 模拟,很坑的那种 var i,n:longint; a:array[1..105] of integer; begin readln(n); for i:=1 to n+1 d ...

  6. JS中的关系操作符与自动转型

    很多时候对数据操做时都会遇到数据转换,有的是显示转化,有的是隐式转化,即调用默认的规则进行数据转换,经常会把数据转换的方式搞混,于是就花了点时间做了个小小的总结: 一元操作符(--,++,-,+)作用 ...

  7. Windows如何利用输入法简单的打出 ‘↑’ ‘↓’ ‘↖’等箭头

    ‘↑’  shang ‘↓’ xia ‘←’ zuo ‘→’ you ‘↖’ zuoshang ‘↙’  zuoxia ‘↗’  youshang ‘↘’  youxia

  8. ZOJ Saddle Point 数学思维题

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5564   根据它的定义是行最小,列最大. 可以证明鞍点是唯一的. ...

  9. POJ - 2186  Popular Cows tarjain模板题

    http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...

  10. PL/SQL 多表关联UPDATE

    假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 upda ...