rsync+inotify实时数据同步单目录实战

 

rsync+inotify实时数据同步单目录实战

inotify是一个强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

inotify配置是建立在rsync服务基础上的配置过程

操作系统

主机名 网卡eth0 默认网关 用途
root@58server1 192.168.1.111 192.168.1.1 Rsync服务端
root@58client 192.168.1.121 192.168.1.1 Rsync 节点

子网掩码均为255.255.255.0

具体需求:

要求在58server1上以rsync守护进程的方式部署rsync服务,使得root@58client的 rsync节点客户端主机,可以把本地数据通过rsync的方式备份到数据备份服务器58server1上,本例的客户端仅以58client为例

一、在配置inotify前己经把root@58server1 Rsync服务端的rsync服务部置好

现在在58client服务器中安装部置inotify监控服务

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@58server1 ~]# cat /etc/rsyncd.conf
 
#Rsync server
 
#created by oldboy 15:01 2009-6-5
 
##rsyncd.conf start##
 
uid = root
 
gid = root
 
use chroot = no
 
max connections = 2000
 
timeout = 600
 
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 = 192.168.1.1/24
 
hosts deny = 0.0.0.0/32
 
auth users = rsync_backup
 
secrets file = /etc/rsync.password
 
#####################################
 
[dingjian]
 
comment = bbs by dingjian 14:18 2012-1-13
 
path = /dingjian

配置rsync服务略

二、开始安装inotify

在安装inotify-tools前请先确认你的linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,

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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@58client ~]# uname -r
 
2.6.18-308.el5
 
[root@58client ~]# ls -l /proc/sys/fs/inotify/
 
total 0
 
-rw-r--r-- 1 root root 0 May  4 22:33 max_queued_events
 
-rw-r--r-- 1 root root 0 May  4 22:33 max_user_instances
 
-rw-r--r-- 1 root root 0 May  4 22:33 max_user_watches
 
#显示这三个文件则证明支持

2)下载inotify源码包

 
1
2
3
4
5
[root@58client ~]# mkdir /home/Mr.Xing/tools/ -p
 
[root@58client ~]# cd /home/Mr.Xing/tools/
 
[root@58client tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

3)编译安装inotfiy

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@58client tools]# ls
 
inotify-tools-3.14.tar.gz
 
[root@58client tools]# tar zxf inotify-tools-3.14.tar.gz
 
[root@58client tools]# cd inotify-tools-3.14
 
[root@58client inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
 
[root@58client inotify-tools-3.14]# make
 
[root@58client inotify-tools-3.14]# make install
 
[root@58client inotify-tools-3.14]# cd ..
 
[root@58client tools]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotfiy
 
[root@58client tools]# ls -l /usr/local/|grep inotify
 
lrwxrwxrwx 1 root root   30 May  4 22:42 inotfiy -> /usr/local/inotify-tools-3.14/
 
drwxr-xr-x 6 root root 4096 May  4 22:41 inotify-tools-3.14

参数:

--prefix=PATH   指定编译安装的路径

提示:更多的编译参数可以使用./configure –h 查看,编译成功后会生成4个目录,

小软件一般规范安装到同一个目录,一般为/usr/local中

建立一个软链接

进入安装inotify的目录

 
1
2
3
4
5
6
7
8
9
10
11
[root@58client tools]# ls -l /usr/local/inotify-tools-3.14/
 
total 16
 
drwxr-xr-x 2 root root 4096 May  4 22:41 bin            #inotfiy执行命令(二进制)
 
drwxr-xr-x 3 root root 4096 May  4 22:41 include        #inotfiy程序所需用的头文件
 
drwxr-xr-x 2 root root 4096 May  4 22:41 lib            #动态链接的库文件
 
drwxr-xr-x 4 root root 4096 May  4 22:41 share         #帮助文件

查看是否编译成功,

使用echo $?  返回0表示成功

只关注下面两个文件

bin

|-- inotifywait

`-- inotifywatch

./bin/inotifywait –help

4) 编写inotify实时监控脚本

事件相关参数大小:

默认:

[root@58client inotify]# cat /proc/sys/fs/inotify/max_user_watches

8192

[root@58client inotify]# cat /proc/sys/fs/inotify/max_queued_events

16384

[root@58client inotify]# cat /proc/sys/fs/inotify/max_user_instances

128

并发大时可以修改增大这些参数

开始编写inotify脚本

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@58client inotify]# mkdir /server/scripts/ -p
 
[root@58client inotify]# cd /server/scripts/
 
[root@58client scripts]# vi inotify.sh
 
[root@58client scripts]# cat inotify.sh
 
#!/bin/bash
 
#para
 
host01=192.168.1.111
 
src=/dingjian
 
dst=dingjian
 
user=rsync_backup
 
rsync_passfile=/etc/rsync.password
 
inotify_home=/usr/local/inotify-tools-3.14/
 
 
 
#judge
 
if [ ! -e "$src" ] \
 
|| [ ! -e "${rsync_passfile}" ] \
 
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
 
|| [ ! -e "/usr/bin/rsync" ];
 
then
 
echo "Check File and Folder"
 
exit 9
 
fi
 
 
 
 
 
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
 
| while read file
 
do
 
#  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
 
cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
 
done
 
exit 0

host rsync服务器地址

src本地目录  dst模块  要和/etc/rsyncd.conf中的模块名对应

user 用户

rsync_passfile密码文件

inotify_home  inotify定义inotifywait的目录

#jude下面的内容是检查上面参数是否存在

 
1
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \

判断$src目录的变化等参数

while reade file死循环,通过|管道实时读取上面事件的变化,一讲取到就循环执行以下命令进行推送

 
1
cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

经过测试,每秒200个文件并发,数据同步几乎无延尽(小于1秒)

5) 测试rsync是否成功推送

[root@58server1 backup]# rsync -avz -P --delete /backup rsync_backup@192.168.1.111::backup/ --password-file=/etc/rsync.password

一般添加了脚本后要格式化一次脚本

 
1
2
3
[root@58client scripts]# dos2unix inotify.sh
 
dos2unix: converting file inotify.sh to UNIX format ...

6) 在客户端运行脚本

sh -x inotify.sh

使脚本在后台运行

[root@58client scripts]# sh inotify.sh &

[root@58client dingjian]# for n in `seq 10000`;do /bin/cp /bin/cat /dingjian/cat$n;done

服务端

[root@58server1 dingjian]# ls -l /dingjian|wc -l

查看客户端58client后台运行的inotify脚本进程

[root@58client scripts]# ps -ef|grep inotify

rsync+inotify实时数据同步单目录实战的更多相关文章

  1. rsync+inotify实时数据同步多目录实战

    rsync+inotify实时数据同步多目录实战       inotify配置是建立在rsync服务基础上的配置过程 操作系统 主机名 网卡eth0 默认网关 用途 root@58server1 1 ...

  2. Rsync+inotify 实时数据同步 inotify master 端的配置

    强大的,细致的,异步的文件系统事件监控机制.Linux 内科从 2.6.13 起支持 inotify Inotify 实现的几款软件:Inotify,sersync,lsyncd ※Inotify 实 ...

  3. Linux学习系列之Inotify+Rsync实现实时数据同步

    Inotify简介 inotify介绍 inotify是一种强大的.异步的文件系统监控机制,linux内核从2.6.13起,加入了inotify的支持,通过inotify可以监控文件系统中添加.删除. ...

  4. Inotify+rsync实现实时数据同步

    使用rsync可以实现数据同步,但是即使使用crontab定时任务最小执行间隔为1分钟,在数据实时性要求比较高场合需使用inotify+rsync实现实时同步 下载inotify wget https ...

  5. centos7部署inotify与rsync实现实时数据同步

    实验环境:CentOS Linux release 7.6.1810 node1:192.168.216.130 客户端(向服务端发起数据同步) node2:192.168.216.132 服务端(接 ...

  6. rsync+inotify实时数据同步

    没有实际的用过,先mark一下,后面实践. https://www.osyunwei.com/archives/7447.html 一.为什么要用Rsync+sersync架构? 1.sersync是 ...

  7. rsync+inotify 实时双向同步

    前言 在遇到需要 nginx 负载均衡,承受较高并发的情况,同时会有双机相同目录内容须保持一致的需求 rsync+inotify 是一个不错的解决方案,相较于 rsync+sersync 在处理大量文 ...

  8. 我的一次rsync+inotify本地数据同步示例

    环境: web工作目录:/var/www/mydafuhao git仓库目录: /var/www/mydafuhao.git/mydafuhao 需求:inotify监控git仓库目录,发现有版本更新 ...

  9. Centos rsync+inotify 实现数据同步备份

    最近公司做了一

随机推荐

  1. oracle数据库面试相关

    1.实现分页 rownum: select * from (select aa.* rownum rn from (select * from student )aa where rownum < ...

  2. java:Oracle(视图,索引,序列)

    1.索引:索引一共分为4种 -- 在oracle中, -- normal:普通索引 -- unique:唯一索引 -- bitmap:位图索引 -- B 树索引:默认:如果不建立索引的情况下,orac ...

  3. 快速乘+快速幂(用于模数超过int范围)

    一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...

  4. [转帖]Spring Cloud底层原理

    拜托!面试不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA 毫无疑问,Spring Cloud 是目前微服 ...

  5. 手写数字识别 卷积神经网络 Pytorch框架实现

    MNIST 手写数字识别 卷积神经网络 Pytorch框架 谨此纪念刚入门的我在卷积神经网络上面的摸爬滚打 说明 下面代码是使用pytorch来实现的LeNet,可以正常运行测试,自己添加了一些注释, ...

  6. C语言--- 高级指针2(结构体指针,数组作为函数参数)

    一.结构体指针 1. 什么是结构体指针?指向结构体变量的指针     结构体:     typedef  struct stu{                          char name[ ...

  7. C++继承中的同名覆盖

    1,父子间的冲突是由继承带来的,两个类之间存在了继承的关系,必然的会带来一 些问题,本文要讨论的是父子之间成员变量或成员函数的命名问题: 2,思考: 1,子类中是否可以定义父类中的同名成员? 1,可以 ...

  8. vmware中的虚拟linux配置多块网卡

    在使用vm上运行多个linux系统,来模拟LVS负载均衡实验中.需要在lvs服务器中设置两块网卡,发现可以在vm给虚拟机添加任意多个网卡. 方法: 不要启动Linux,在上面的菜单项中选择: “VM— ...

  9. Win7(64位)下安装Anaconda+Tensorflow(CPU)

    一.安装Python 3.5 下载Anaconda网址:https://www.anaconda.com/download/ 安装:Anaconda3-4.2.0-Windows-x86_64.exe ...

  10. win10删除文件夹时需要管理员授权或拒绝访问(无权访问权限修改)

    win10 用户:我自己就是电脑主人,凭啥我没有自己电脑文件夹的权限? 微软:对不起,您是电脑硬件的主人,但是电脑系统的主人是我!你只不过是个用户而已. win10 用户:我cao你...[哔-] 对 ...