Go Real-time file synchronization via Rsync and INotify (asynchronous file system event monitoring mechanism) under Linux

Source: Internet
Author: User
Tags inotify rsync pkill

Real-time file synchronization via Rsync and INotify (asynchronous file system event monitoring mechanism) under Linux
Original: http://www.summerspacestation.com/linux%E4%B8%8B%E9%80%9A%E8%BF%87rsync%E4%B8%8Einotify%E5%BC%82%E6%AD%A5% E6%96%87%e4%bb%b6%e7%b3%bb%e7%bb%9f%e4%ba%8b%e4%bb%b6%e7%9b%91%e6%8e%a7%e6%9c%ba%e5%88%b6%e5%ae%9e%e7%8e%b0%e6 %96%87%e4%bb%b6/
Catalog [Hide]

Inotify-tools Tool Installation
Synchronization with Rsync through shell scripting
Adding inotify to the automatic boot service
INotify parameter optimization
rsync+intity pressure test effect
Rsync+inotify Advantages and Disadvantages
A real-time synchronization scheme for high concurrency data
Inotify-tools Tool Installation
View system Support:


Uname-r #系统内核至少达到2.6.13
Ls-l/proc/sys/fs/inotify/
Total dosage 0
-rw-r--r--1 root root 0 June 13:17 max_queued_events
-rw-r--r--1 root root 0 June 13:17 max_user_instances
-rw-r--r--1 root root 0 June 13:17 max_user_watches
#显示这三个文件则证明支持

Uname-r #系统内核至少达到2.6.13
Ls-l/proc/sys/fs/inotify/
Total dosage 0
-rw-r--r--1 root root 0 June 13:17 max_queued_events
-rw-r--r--1 root root 0 June 13:17 max_user_instances
-rw-r--r--1 root root 0 June 13:17 max_user_watches
#显示这三个文件则证明支持
Download INotify Source Package


Mkdir-p/home/kendall/tools/
cd/home/kendall/tools/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
LS inotify-tools-3.14.tar.gz

Mkdir-p/home/kendall/tools/
cd/home/kendall/tools/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
LS inotify-tools-3.14.tar.gz
Compiling the installation


cd/home/kendall/tools/
Tar zxf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14/
./configure--prefix=/usr/local/inotify-tools-3.14
Make && make install
echo $?
Cd.. /
Ln-s/usr/local/inotify-tools-3.14//usr/local/inotify-tools

cd/home/kendall/tools/
Tar zxf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14/
./configure--prefix=/usr/local/inotify-tools-3.14
Make && make install
echo $?
Cd.. /
Ln-s/usr/local/inotify-tools-3.14//usr/local/inotify-tools
Command location


/usr/bin/inotifywait
Inotifywait can be used directly

/usr/bin/inotifywait
Inotifywait can be used directly
The inotify is installed on the rsync client, which is monitored for data changes and pushed to the rsync server via rsync.

Synchronization with Rsync through shell scripting

#! /bin/bash
inotify=/usr/bin/inotifywait
path=/data
ip=172.16.1.41
$inotify-mrq--format '%w%f '-E Create,close_write,delete $Path \
|while Read file
do
If [-F $file];then
Rsync-az $file--delete [Email&nbs P;protected] $IP:: Nfsbackup--password-file=/etc/rsync.password
Else
CD $Path
Rsync-az./--delete [email  protected] $IP:: nfsbackup--password-file=/etc/rsync.password
Fi
Done

#! /bin/bash
inotify=/usr/bin/inotifywait
path=/data
ip=172.16.1.41
$inotify-mrq--format '%w%f '-E Create,close_write,delete $Path \
|while Read file
do
If [-F $file];then
Rsync-az $file--delete [EMAIL&N Bsp;protected] $IP:: Nfsbackup--password-file=/etc/rsync.password
Else
CD $Path
Rsync-az./--delete [email  protected] $IP:: nfsbackup--password-file=/etc/rsync.password
Fi
Done
Simple version


#! /bin/bash
Inotify=/usr/bin/inotifywait
$inotify-mrq--format '%w%f '-e create,close_write,delete/data \
|while Read File
Do
Cd/data &&\
Rsync-az./--delete [email protected]::nfsbackup--password-file=/etc/rsync.password & #可以增加效率
Done

#! /bin/bash
Inotify=/usr/bin/inotifywait
$inotify-mrq--format '%w%f '-e create,close_write,delete/data \
|while Read File
Do
Cd/data &&\
Rsync-az./--delete [email protected]::nfsbackup--password-file=/etc/rsync.password & #可以增加效率
Done
Running in the background, booting up
/bin/sh Script &

Adding inotify to the automatic boot service

Vim/etc/init.d/syncd
1
Vim/etc/init.d/syncd

#!bin/bash
#chkconfig: 2345 38 46
#######################
#
#######################
. /etc/init.d/functions
If [$#-ne 1];then
Usage: $ {start|stop}
Exit 1
Fi
Case "$" in
Start
/bin/bash/server/scripts/inotify.sh &
Echo $$ >/var/run/inotify.pid
If [' Ps-ef|grep inotify|wc-l '-gt 2];then
Action "INotify service is started"/bin/true
Else
Action "INotify service is started"/bin/false
Fi
;;
Stop
Kill-9 cat/var/run/inotify.pid >/dev/null 2>&1
Pkill inotifywait
Sleep 2
If [' Ps-ef|grep inotify|grep-v grep|wc-l '-eq 0];then
Action "INotify service is stopped"/bin/true
Else
Action "INotify service is stopped"/bin/false
Fi
;;
*)
Usage: $ {start|stop}
Exit 1
Esac

#!bin/bash
#chkconfig: 2345 38 46
#######################
#
#######################
. /etc/init.d/functions

If [$#-ne 1];then
Usage: $ {start|stop}
Exit 1
Fi

Case "$" in
Start
/bin/bash/server/scripts/inotify.sh &
Echo $$ >/var/run/inotify.pid
If [' Ps-ef|grep inotify|wc-l '-gt 2];then
Action "INotify service is started"/bin/true
Else
Action "INotify service is started"/bin/false
Fi
;;
Stop
Kill-9 cat/var/run/inotify.pid >/dev/null 2>&1
Pkill inotifywait
Sleep 2
If [' Ps-ef|grep inotify|grep-v grep|wc-l '-eq 0];then
Action "INotify service is stopped"/bin/true
Else
Action "INotify service is stopped"/bin/false
Fi
;;
*)
Usage: $ {start|stop}
Exit 1
Esac

chmod +X/ETC/INIT.D/SYNCD
Chkconfig--add SYNCD
Chkconfig SYNCD on
1
2
3
chmod +X/ETC/INIT.D/SYNCD
Chkconfig--add SYNCD
Chkconfig SYNCD on
INotify parameter optimization
The following two commands need to be added to boot/etc/rc.local


echo "50000000" >/proc/sys/fs/inotify/max_user_watches
echo "50000000" >/proc/sys/fs/inotify/max_queued_events
Max_queued_events #队列容纳事件数量
The number of Max_user_instances #每个用户可以运行wait watch
Max_user_watches #最大监控文件数量
1
2
3
4
5
echo "50000000" >/proc/sys/fs/inotify/max_user_watches
echo "50000000" >/proc/sys/fs/inotify/max_queued_events
Max_queued_events #队列容纳事件数量
The number of Max_user_instances #每个用户可以运行wait watch
Max_user_watches #最大监控文件数量
rsync+intity pressure test effect
Below 200 files per second concurrency, basically no difference.

Rsync+inotify Advantages and Disadvantages
Advantages:

Monitor file system event changes and synchronize tools for real-time data synchronization

Disadvantages:

If concurrency is greater than 200 files (10-100k), synchronization will have a delay.
We wrote in front of the script, each time is all pushed once, but it is incremental, can also only synchronize the changes in the file, do not change the ignore.
After monitoring the event, invoking Rsync synchronization is a single process (plus & concurrency), Sersync multi-process synchronization.
A real-time synchronization scheme for high concurrency data
INotify (sersync) +rsync file level
DRBD file system level, block-based synchronization disadvantage: Backup node data is not available
Third-party software sync features: MySQL sync, Oracle MongoDB
Program double write, direct write two server
Business logic Resolution ("Write primary NFS, read backup reads and writes separation, read Master) prevents delays, discards NFS schemes, and uses the web as a standby node for NFS." A lot of efficiency improvements
NFS Cluster (145 scenario Consolidation) (dual write primary storage, inotify (Sersync) +rsync for standby storage, no Brahma to resolve latency issues)

Go Real-time file synchronization via Rsync and INotify (asynchronous file system event monitoring mechanism) under Linux

Related Article

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.