Linux real-time synchronization inotify

Source: Internet
Author: User
Tags inotify rsync

#实时同步inotify

1, INotify Introduction
INotify is a powerful, delicate, asynchronous file system event monitoring mechanism, the Linux kernel from 2.6.13, joined the INotify support, through INotify can monitor the file system to add, delete, modify, move and other events, the use of this kernel interface, Third-party software can monitor the file system under a variety of changes, and Inotify-tloos is the implementation of such monitoring software.

2, INotify implementation
Check that the Rsync daemon service is working properly and can push data to implement synchronization
Ps-ef |grep rsync|grep-v grep
Root 5959 1 0 18:52? 00:00:00 rsync--daemon
1) adhere to the current system support inotify
The UNAME-R version is supported in 2.6.13 or more
2.6.32-504.el6.x86_64
# ls-l/proc/sys/fs/inotify
-rw-r--r--1 root root 0 Apr 4 20:23 max_queued_events
-rw-r--r--1 root root 0 Apr 4 20:23 max_user_instances
-rw-r--r--1 root root 0 Apr 4 20:23 max_user_watches
#显示这三个文件则证明支持INOTIFY
Proc/sys/fs/inotify/max_queued_evnets
Represents the maximum value assigned to the number of events that can be queued in inotify instance when calling Inotify_init, an event that exceeds this value is discarded, but the In_q_overflow event is triggered.

/proc/sys/fs/inotify/max_user_instances
Represents the maximum number of inotify instatnces that each real user ID can create.

/proc/sys/fs/inotify/max_user_watches
Represents the maximum number of directories that can be monitored per inotify instatnces. If you monitor a large number of files, you need to increase the size of this value appropriately, depending on the situation.
Example: Echo 30000000 >/proc/sys/fs/inotify/max_user_watches

2) Download INotify source package, compile and install
wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
Tar zxvf inotify-tools-3.13.tar.gz
CD inotify-tools-3.13
./configure--prefix=/usr/local/inotify-tools-3.13
Make && make install
Ln-s/usr/local/inotify-tools-3.13//usr/local/inotify
Cd/usr/local/inotify

./inotifywait-help
Inotifywait 3.13
Wait for a particular event on a file or set of files.
usage:inotifywait [Options] file1 [File2] [file3] [...]
Options:
-h|--help Show this help text.
@<file> Exclude The specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
Extended regular expression <pattern>.
--excludei <pattern>
Like--exclude but case insensitive. #排除文件或目录时, Case insensitive
-m|--monitor Keep listening for events forever. Without
This option, inotifywait would exit after one
Event is received. #始终保持事件监听状态
-r|--recursivewatch directories recursively. #递归查询目录
--fromfile <file>
Read files to watch from <file> or-for stdin.
-q|--quiet print (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. #指定时间输出的格式
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening-a single event
Waiting for a event for <seconds> seconds.
If <seconds> is 0, inotifywait'll never time out.
-e|--event <event1> [-e|--event <event2> ...]
Listen for specific event (s). If omitted, all events is
Listened for. #通过此参数可以指定需要监控的事件, as follows:

Exit Status:
0-an event asked to watch for is received.
1-an event you do not ' ask to watch ' for is received
(usually delete_self or unmount), or some error occurred.
2-the--timeout option was given and no events occurred
In the specified interval of time.

Events:
Accessfile or directory contents were read #文件或目录被读取
Modifyfile or directory contents were written #文件或目录内容被修改
Attribfile or directory attributes changed #文件或目录属性被修改
Close_writefile or directory closed, after being opened in
Writeable mode
Close_nowritefile or directory closed, after being opened in
Read-only mode
CloseFile or directory closed, regardless of read/write mode #文件或目录封闭, regardless of read/write mode
OpenFile or directory opened #文件或目录被打开
Moved_tofile or directory moved to watched directory #文件或目录被移动至另外一个目录
Moved_fromfile or directory moved from watched directory
MoveFile or directory moved to or from watched directory #文件或目录被移动另一个目录或另一个目录移动到当前目录
CreateFile or directory created within watched directory #文件或目录被创建
DeleteFile or directory deleted within watched directory #文件或目录被删除
Delete_selffile or directory was deleted
Unmountfile system containing file or directory unmounted #文件或目录被卸载
Real-time monitoring commands:
/usr/local/inotify/bin/inotifywait-mrq--timefmt '%d%m%y%h:%m '--format '%T%w%f '-e create,delete,close_write,attrib /data

INotify Disadvantages:
1) concurrency cannot be greater than 200 files

Real-time monitoring of inotify scripts

#!/bin/bash#inotify Jiankongcmd="/usr/local/inotify/bin/inotifywait"src="/data"MoD="Test"User="Rsyncback"IP=192.168.233.129Passfile="/etc/rsync.password"RSYC="/usr/bin/rsync"#judgeif[-F"$cmd"] && [-E"$src"] && [-N"$user"] && [-N"$mod"] && [-F"$passfile"] && [-F"$RSYC"]; Then Echo "file path is OK"ElseExit9fi while true Do$cmd-mrq--format'%w%f'-E Create,delete,modify,attrib,close_write,move $src | whileRead Line Do   [ ! -E"$line"] && Break | |    Echo$line >>/root/Inotiity.log $rsyc-az--delete $line ${user}@${ip}:: $mod--password-file=$passfile DoneCD $SRC&& $rsyc-az--delete./${user}@${ip}:: $mod--password-file=$passfile Done

Linux real-time synchronization inotify

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.