Two, Linux's real-time synchronization software inotify

Linux real-time synchronization software of inotify

 

Linux kernel version 2.6.13 from the start inotify notification interface provides for a variety of monitoring changes in the file system, such as file access, delete, move, and so on. Using this mechanism, you can very easily implement file movement alarm, incremental backup, and timely response to changes in the directory or file. inotify can change by calling the system interface, real-time monitoring of directories and files, combined with rsync, real-time directory synchronization. After determining the system kernel version, you can install inotify packages.

[nfs01 the root @ ~] # the uname -R & lt
2.6.32-696.el6.x86_64
[nfs01 the root @ ~] # LS / proc / SYS / FS / the inotify /
max_queued_events max_user_instances max_user_watches
#max_queued_events default queue 16384 indicates time monitoring may be used cat command to view
#max_user_instances represent up to monitor the number of instances of the default 128
#max_user_watches represent each instance of monitoring up to 8192 the number of files by default

 

Want to change the system defaults, you need to modify /etc/sysctl.conf file

[root @ Backup nfs] # vim /etc/sysctl.conf
# three lines added to the end
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 90000000

 

  • Software installation and use inotify

1) inotify package is installed, you can download the official website ( http://sourceforge.net/projects/inotify-tools/) source package compiled and installed, you can install yum, you need to configure the premise epel source.

[root@nfs01 ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@nfs01 ~]# yum install -y inotify-tools
[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-1.el6.x86_64

 

2) inotify common parameters

After the software is installed inotify two commands used are / usr / bin / inotifywait and / usr / bin / inotifywatch

inotifywait <--- Information to achieve change monitoring (focus on understanding the command) to the data directory

 

inotifywatch <--- monitor data changes, changes in statistical data

Usually we use inotifywait command to view the directory information can change, inotifywatch less use statistical functions

inotifywait command of some commonly used parameters are the following:

Command parameters

Parameter Description

-m|--monitor

Always listening state

-d|--daemon

Background monitoring, event information will be recorded in the specified file

-r|--recursive

Recursive directory monitor data changes

-q|--quiet

Reduce output content

--timefmt

Date Format Set (% F =% Y-% m-% d - in - days% T =% H:% M:% S hours: minutes: seconds)

--format

Setting the output format information (% T time monitoring directory% f% w file change event information% e)

-e

Specifies that the test action (use Close the Write modifications create create delete delete Moved to mobile)

 

例:inotify -mrq --timefmt "%F %T" --format "%T %w/%f %e" \

-e creat,delete,movedto,closewrite /data

 

  • inotify + rsync real-time synchronization

1) The principle

inotify can achieve continuous monitoring of data for a particular directory, rsync --delete parameter of the difference can be achieved without a directory data synchronization client and server specified when the inotify monitoring data changes to the directory, you can script calls rsync service, enabling real-time synchronization function.

2) rsync service set up

Real-time synchronization, first ensure that the server and client installation configuration rsync (rsync service on an introductory deployment), that the client can push files to the backup server, test as follows:

[nfs01 the root @ ~] # -AZ the rsync / etc / sysconfig [email protected] --password-File :: = NFS / etc / rsync.password
# push client
[Backup the root @ ~] # LS / NFS
sysconfig
# server receives

 

 3) scripting

inotify After installation, use the / usr / bin / inotifywait real-time monitoring directory, when the directory data changes, calling rsync service, implemented by the script:

#!/bin/bash
#inotify+rsync backup /data
/usr/bin/inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /data |\
while read line
do
rsync -az /data/ --delete [email protected]::nfs --password-file=/etc/rsync.password
done

 

 

 4) Run the script, test

[root @ nfs01 the Data] # SH /root/inotify_rsync.sh &
[1] 3555
[root @ nfs01 the Data] # Touch {01..20}
[root @ nfs01 the Data] # LS
01 02 03 04 05 06 07 08 09 10 11 12 1,314,151,617,181,920
# client to create a test file
[root @ Backup nfs] # LS / nfs
01 02 03 04 05 06 07 08 09 10 11 12 1,314,151,617,181,920
# server successfully received, to achieve real-time synchronization function

 

  • inotify Software inadequate

inotify monitoring system interface is invoked, but the process is limited, if there are a large number of small files transfer, there will be a significant delay, the actual production, use less.

 

 

 

Guess you like

Origin www.cnblogs.com/yaokaka/p/11620751.html