综合架构_实时同步服务 inotify

作用说明:

员工传输数据:好回复 定时备份(最短周期1分钟)

 

用户传输数据:难恢复 实时备份

  普通数据:图片 ,压缩包, 视频, 文档等  ---> inotify/Sersync +Rsync 实时备份

 

 

实时同步原理:

1.发现新的数据信息   inotify

2.将新的数据进行传输  rsync

3.发行数据过程+数据传输过程 ==实时同步

 inotify+sersync == 脚本/sersync

 

 

 inotify软件介绍

监控数据变化软件,变化信息成为事件(创建 修改 删除 移动)

 


inotify机制优点:

监控文件系统时间变化,通过复制工具实现实时数据复制

支持多线程实时复制

缺点:

实测并发如果大于200个文件(10-100k),复制会有延迟 


inotify软件部署:前提 epel源

 服务端:

1.检测rsync 服务是否开启

2.检查Linux系统内核(内核版本大于2.6) inotify支持的情况

[root@backup backup]# uname -r
3.10.0-957.el7.x86_64

3.检查参数文件

[root@backup backup]# ls -l /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 10月 26 20:10 max  _queued_events  -->设置inotify实例事件(event)队列可容纳的事件数量
-rw-r--r-- 1 root root 0 10月 26 20:10 max_user_instances --> 设置每个用户可以运次那个的inotifywait或inotifywatch命令的进程数
-rw-r--r-- 1 root root 0 10月 26 20:10 max_user_watches ->设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)

curl -o /etc/yum.repos.d/epel.repo 

http://mirrors.aliyun.com/repo/epel-7.repo

yum  install epel-release -y   --> 安装epel源,默认的官方源没有inotify-tools工具包

yum install -y inotify-tools     --->安装在存储服务器

inotify软件应用:

rpm  -qa inotify-tools 

rpm  -ql  inotify-tools

/usr/bin/inotifywait *** ---监控数据变化命令(open, close, delete)等,执行后处于阻塞状态,适合在shell脚本中使用 *****

/usr/bin/inotifywatch   ---对变化的事件信息进行统计

 

 


监控命令使用方法

     -h|--help     	Show this help text.
	--exclude <pattern>
	              	Exclude all events on files matching the
	              	extended regular expression <pattern>.
					排除指定数据信息不用进行监控		
	--excludei <pattern>
	              	Like --exclude but case insensitive.
					排除指定数据信息不用进行监控(对数据信息忽略大小写)			
	-m|--monitor  	Keep listening for events forever.  Without
	              	this option, inotifywait will exit after one
	              	event is received.
					一直处于监控状态			
	-d|--daemon   	Same as --monitor, except run in the background
	              	logging events to a file specified by --outfile.
	              	Implies --syslog.
	-r|--recursive	Watch directories recursively.
	                监控指定目录下面数据变化, 还可以监控目录中子目录里面数据变化
					递归监控
	-q|--quiet    	Print less (only print events).
                    尽量输出少的信息	             		 
	-qq           	Print nothing (not even events).
	                什么都不输出
	--format <fmt>	Print using a specified printf-like format
	              	string; read the man page for more details.
					定义输出格式信息
	--timefmt <fmt>	strftime-compatible format string for use with
	              	%T in --format string.
					定义时间格式信息				
	-e|--event <event1> [ -e|--event <event2> ... ]
		            Listen for specific event(s).  If omitted, all events are 
		            listened for.
		            监听指定事件信息
					
	inotifywait命令语法格式
	inotifywait 参数信息  监听数据(目录)
	
    

标准命令用法:

  inotifywait -mrq /data

/data/oldboy01/ CREATE olddog.txt /data/oldboy01/ OPEN olddog.txt /data/oldboy01/ ATTRIB olddog.txt /data/oldboy01/ CLOSE_WRITE,CLOSE olddog.txt

 

 常用监控文件事件功能参数:

实时监控事件信息
      access		file or directory contents were read
	            文件或目录内容被读取--访问事件
	modify ***   file or directory contents were written
	            文件或目录内容被写入--修改时间 ***
	attrib		file or directory attributes changed
	            文件或目录属性信息被改变---属性改变
	close_write ***	file or directory closed, after being opened in writeable mode
	            文件或目录被关闭, 在打开之后并且进行修改了
				开发编写文件--> 文件打开 -- 修改 -- 关闭   ***
	close_nowrite	file or directory closed, after being opened in read-only mode
	                文件或目录被关闭, 在打开之后并且未做修改
	close ***		file or directory closed, regardless of read/write mode
	            文件或目录被关闭, 无论是读了或者写了之后   ***
	open		file or directory opened
	            文件或目录被打开
	moved_to	file or directory moved to watched directory
	            文件或目录移动到监控目录中--移动事件        
	moved_from	file or directory moved from watched directory
	            文件或目录移动从监控目录中移走---移动事件  
	move		file or directory moved to or from watched directory
	            文件或目录进行移动了--移动事件  ***
	create		file or directory created within watched directory
	            文件或目录在监控目录进行创建操作 ***
	delete		file or directory deleted within watched directory
	            文件或目录被删除在监控目录中  ***
	delete_self	file or directory was deleted
	            删除目录时触发删除事件
	准备环境:
    ├── oldboy01            --- 目录    删除  delete_self  delete
    │?? ├── oldgir01        --- 子目录  删除  delete_self  delete
    │?? ├── oldgir02        --- 子目录  删除  delete_self  delete
    │?? ├── oldgirl01.txt   --- 文件    删除  delete 
    │?? └── oldgirl02.txt   --- 文件    删除  delete
    ├── oldboy01.txt        --- 文件    删除  delete 
    ├── oldboy02            --- 目录    删除  delete_self  delete
    ├── oldboy02.txt        --- 文件    删除  delete 
    └── oldboy03
	unmount		file system containing file or directory unmounted
    
    监控事件信息:	
	modify close_write move create delete  --> close_write,move,create,delete
	 
	实时监控数据命令:
	inotifywait -mrq /data --format "%w%f"  -e modify,close_write,move,create,delete 

  

 

利用脚本实现实时同步
	
	
	inotifywait -mrq /data --format "%w%f"  -e modify,close_write,move,create,delete 
	
	Date=/data/oldboy01.txt
	Date=/data/oldboy02.txt
	
	rsync -avz $Date  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

	shell脚本中有三种常见循环方式:
	for    --- 有限循环  
	while  --- 有限循环/无限循环(死循环)
	           while 条件设置  条件为真就会一直循环, 条件为假才会停止循环
	until  --- 有限循环/无限循环(死循环)
	           until 条件设置  条件为假就会一直循环, 条件为真才会停止循环
	
    实现实时同步脚本:	
	[root@nfs01 data]# cat /server/scripts/inotify.sh 
    #!/bin/bash
    
    
    inotifywait -mrq /data --format "%w%f"  -e modify,close_write,move,create,delete|
    while read line
    do
       rsync -avz /data/  --delete  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
    done  
	
	脚本如何变为守护进程一直运行:
	sh /server/scripts/inotify.sh &
	nohup sh /server/scripts/inotify.sh &

  

原文地址:https://www.cnblogs.com/zhanghongqi/p/11739768.html