Linux real-time inotify synchronization and linux inotify Synchronization

Source: Internet
Author: User
Tags inotify

Linux real-time inotify synchronization and linux inotify Synchronization

# Real-Time inotify Synchronization

1. Introduction to inotify
Inotify is a powerful, refined, and asynchronous file system event monitoring mechanism. The Linux kernel has been added to inotify since 2.6.13, INOTIFY can be used to monitor various events in the file system, such as adding, deleting, modifying, and moving events. With this kernel interface, third-party software can monitor various file changes in the file system, inotify-tloos is the software that implements such monitoring.

2. inotify implementation
Check whether the rsync daemon service is normal. You can push data for synchronization.
Ps-ef | grep rsync | grep-v grep
Root 5959 1 0 18:52? 00:00:00 rsync -- daemon
1) insist on whether the current system supports inotify
The uname-r version is later than 2.6.13.
2.6.32-504. el6.x86 _ 64
# Ls-l/proc/sys/fs/inotify
-Rw-r -- 1 root 0 Apr 4 20:23 max_queued_events
-Rw-r -- 1 root 0 Apr 4 20:23 max_user_instances
-Rw-r -- 1 root 0 Apr 4 20:23 max_user_watches
# Displaying these three files proves that INOTIFY is supported.
Proc/sys/fs/inotify/max_queued_evnets
Indicates the maximum number of events that can be queued in the inotify instance when inotify_init is called. Events that exceed this value are discarded, but the IN_Q_OVERFLOW event is triggered.

/Proc/sys/fs/inotify/max_user_instances
The maximum number of inotify instatnces that can be created by each real user ID.

/Proc/sys/fs/inotify/max_user_watches
The maximum number of directories that each inotify instatnces can monitor. If the number of monitored files is large, you need to increase the size of this value as appropriate.
Example: echo 30000000>/proc/sys/fs/inotify/max_user_watches

2) download the inotify source code package, compile and install it
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
Inoywait 3.13
Wait for a special 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
Extended regular expression <pattern>.
-- Excludei <pattern>
Like -- exclude but case insensitive. # case insensitive when excluding files or directories
-M | -- monitor Keep listening for events forever.
This option, inotifywait will exit after one
Event is already ed. # always keep the event listening status
-R | -- recursiveWatch directories recursively. # recursively query the Directory
-- Fromfile <file>
Read files to watch from <file> or-for stdin.
-Q | -- quiet Print less (only print events). # Print monitoring event information
-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
% T in -- format string. # specify the time output format
-C | -- csv Print events in CSV format.
-T | -- timeout <seconds>
When listening for a single event, time out after
Waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-E | -- event <event1> [-e | -- event <event2>...]
Listen for specific event (s). If omitted, all events are
Listened for. # You can use this parameter to specify the event to be monitored, as shown below:

Exit status:
0-An event you asked to watch for was stored ed.
1-An event you did not ask to watch for was stored ed
(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 # the file or directory is read
Modifyfile or directory contents were written # the file or directory content is modified.
Attribfile or directory attributes changed # the file or directory attribute is modified.
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 # the file or directory is closed, regardless of the read/write mode.
Openfile or directory opened # the file or directory is opened.
Moved_tofile or directory moved to watched directory # the file or directory is moved to another directory.
Moved_fromfile or directory moved from watched directory
Movefile or directory moved to or from watched directory # the file or directory is moved to another directory or another directory to the current directory.
Createfile or directory created within watched directory # the file or directory is created.
Deletefile or directory deleted within watched directory # the file or directory is deleted.
Delete_selffile or directory was deleted
Unmountfile system containing file or directory unmounted # the file or directory is uninstalled.
Real-time Monitoring command:
/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) The concurrency cannot exceed 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"else  exit 9fiwhile truedo$cmd -mrq --format '%w%f' -e create,delete,modify,attrib,close_write,move $src|\while read linedo   [ ! -e "$line" ] && break || \   echo $line >> /root/inotiity.log   $rsyc -az --delete $line ${user}@${ip}::$mod --password-file=$passfiledonecd $src && $rsyc -az --delete ./ ${user}@${ip}::$mod --password-file=$passfiledone

 

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.