Using INotify to synchronize folders in real time

Source: Internet
Author: User
Tags inotify rsync

Connect the article: http://blog.csdn.net/oldmtn/article/details/47836859



This article is after I look at the reference 1, turn, but is the verification of my own, true and reliable, not indiscriminately reproduced.

and has been subtly modified.


The following actions need to be done on the source server ...

Install the Inotify-tools tool to trigger rsync to sync in real time

1. See if the server kernel supports inotify

Ll/proc/sys/fs/inotify #列出文件目录, the following contents show that the server kernel supports inotify

-rw-r--r--1 Root 0 Mar 7 02:17 max_queued_events

-rw-r--r--1 Root 0 Mar 7 02:17 max_user_instances

-rw-r--r--1 Root 0 Mar 7 02:17 max_user_watches

Note: Linux support inotify kernel minimum 2.6.13, you can enter the command: UNAME-A view the kernel

The CentOS 5.X kernel is 2.6.18 and is supported by default INotify

2. Installation Inotify-tools

Yum install make GCC gcc-c++ #安装编译工具

Inotify-tools Download Address: http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

Upload inotify-tools-3.14.tar.gz to/usr/local/src directory

Cd/usr/local/src

Tar zxvf inotify-tools-3.14.tar.gz #解压

CD inotify-tools-3.14 #进入解压目录

./configure--prefix=/usr/local/inotify #配置

Make #编译

Make install #安装

3, set the system environment variable, add soft connection

echo "Path=/usr/local/inotify/bin: $PATH" >>/etc/profile.d/inotify.sh

Source/etc/profile.d/inotify.sh #使设置立即生效

echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf

Ln-s/usr/local/inotify/include/usr/include/inotify

4, modify inotify default parameter (inotify default kernel parameter value is too small)

View system default parameter values

sysctl-a | grep max_queued_events

The result: Fs.inotify.max_queued_events = 16384

sysctl-a | grep max_user_watches

The result: Fs.inotify.max_user_watches = 8192

sysctl-a | grep max_user_instances

The result: fs.inotify.max_user_instances = 128

To modify a parameter:

Sysctl-w fs.inotify.max_queued_events= "99999999"

Sysctl-w fs.inotify.max_user_watches= "99999999"

Sysctl-w fs.inotify.max_user_instances= "65535"

Vi/etc/sysctl.conf #添加以下代码

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

: wq! #保存退出

Parameter description:

Max_queued_events:

INotify Queue Maximum Length, if the value is too small, there will be "* Event queue Overflow * *" error, resulting in inaccurate monitoring files

Max_user_watches:

How many directories are included in the file to sync, available by: Find/root/ceshi-type D | Wc-l statistics, the max_user_watches value must be guaranteed to be greater than the statistical result (here/root/ceshi is the synchronized file directory)

Max_user_instances:

Create INotify instance maximum per user

5, create the script, real-time trigger rsync to sync

Vi/usr/local/inotify/rsync.sh #编辑, add the following code

======================================

#!/bin/sh

srcdir=/home/www.osyunwei.com/

Dstdir=home_www.osyunwei.com

Rsyncuser=home_www.osyunwei.com_user

Rsyncpassdir=/etc/passwd.txt

dstip= "192.168.11.128"

For IP in $dstip

Todo

RSYNC-AVH--port=873--progress--delete $srcdir $rsyncuser @ $ip:: $dstdir--password-file= $rsyncpassdir

Done

/usr/local/inotify/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f%e '-e close_write,modify,delete,  Create,attrib,move $srcdir | While read file

Todo

For IP in $dstip

Todo

RSYNC-AVH--port=873--progress--delete $srcdir $rsyncuser @ $ip:: $dstdir--password-file= $rsyncpassdir

echo "${file} was rsynced" >>/tmp/rsync.log 2>&1

Done

Done

======================================

chmod +x/usr/local/inotify/rsync.sh #添加脚本执行权限

Script parameter Description:

srcdir=/root/ceshi/#源服务器同步目录

Dstdir=test1 #目标服务器rsync同步目录模块名称

Rsyncuser=root #目标服务器rsync同步用户名

Rsyncpassdir=/etc/passwd.txt #目标服务器rsync同步用户的密码在源服务器的存放路径

dstip= "192.168.11.128" #目标服务器ip, multiple IP separated by space

/tmp/rsync.log #脚本运行日志记录

6, set the script on the automatic execution of the boot

Vi/etc/rc.d/rc.local #编辑, adding a row at the end

Sh/usr/local/inotify/rsync.sh & # Set up to run the script in the background automatically

: wq! #保存退出

7, test inotify real-time trigger rsync synchronization script is normal operation

Create a file on the source server 192.168.11.130 Inotify_rsync_ceshi

Mkdir/root/ceshi/inotify_rsync_ceshi

Reboot source server: 192.168.11.130

After the system starts, check to see if there are Inotify_rsync_ceshi folders under the/root/folder of the two target servers 192.168.11.128

Then create the folder on the source server 192.168.11.130 inotify_rsync_ceshi_new

Mkdir/root/ceshi/inotify_rsync_ceshi_new

Continue to see if there are inotify_rsync_ceshi_new folders under/root/folder1 192.168.11.128 of the target server

If all the above tests pass, the inotify real-time triggering rsync synchronization script is working properly.

at this point, Linux Rsync+inotify-tools implementation of real-time data synchronization completed.

Extended reading:

============================================

INotify parameters

-M is to keep listening all the time

-R is a recursive view of the directory

-Q is printing out events

-e Create,move,delete,modify,attrib refers to the "Listen for Create move delete Write permission" event

rsync parameters

============================================

-V,--verbose verbose mode output

-Q,--quiet thin output mode

-C,--checksum Open the check switch to force the file transfer to verify

-A,--archive archive mode, which represents the transfer of files recursively and maintains all file attributes equal to-rlptgod

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.