Configuration method for inotify tools +rsync real time synchronization files

  • 2020-05-06 12:09:06
  • OfStack

Server A:
The main server of the forum, running DZ X2 forum program; Server B: the forum needs to synchronize X2 image attachments and MySQL data from A master server to B server in real time.

Because general RSYNC CRON is required to run periodically SH script to achieve synchronization, it will bring some problems. Such as user from the master server to upload a picture, need at least a minute can be displayed from the server. Since the 2.6 kernel Linux, support the inotify mechanism, when some files or folders have changed, a corresponding event, in this way, as long as third-party applications to subscribe to these events, you can deal with the corresponding operation. At this time, so long as has the file has been changed, and will perform a RSNYN, Just upload the modified file to another server.

I use google inotify - tools, relatively simple. There is a very powerful similar application, but good complex. In addition it is important to note: if you use inotify tools to realize real-time synchronization, our main server - the source file server (i.e., server A) implementation is RSYNC from the server, our goal from the server - synchronization server (server B) is the primary server RSYNC. Don't be confused oh.

Start with the master server A and determine if your system supports inotify:


ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_queued_events
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_instances
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_watches

The ability to output such results is supported.

Download and install inotify-tools :


wget --no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xzvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr
make
make install

Complete the installation of inotify-tools.

Next you need to write two SH scripts, inotify_init.sh and inotify_monitor.sh :
Es80en_init.sh is used for the first initialization, i.e. running a full RSYNC synchronise.

vi /root/inotify_init.sh


#!/bin/sh
SRC=/ The primary server A Directories that need to be synchronized / # Remember to add at the end / Otherwise, RYNC A layer of directories is automatically added 
DES=bbsatt
IP= From the server B the IP
USER=rsync
#DST=/etc/rsyncd  The remote rsync Directory under the module 
INWT=/usr/bin/inotifywait
RSYNC=/usr/bin/rsync
$RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES

Save exit.

Set the executable permission :
chmod +x /root/inotify_init.sh

Next is inotify_monitor.sh, which is used to subscribe to the file modification event.inotify_monitor.sh

vi /root/inotify_monitor.sh


#!/bin/bash
##---------------------
sync[0]='/ The directory that the primary server needs to synchronize with / , from the server B the IP . bbsatt . rsync' # localdir . host . rsync_module . auth_user
INWT=/usr/bin/inotifywait
RSYNC=/usr/bin/rsync
PASS=/root/rsync.pwd
##---------------------
for item in ${sync[@]}; do
dir=`echo $item | awk -F" . " '{print $1}'`
host=`echo $item | awk -F" . " '{print $2}'`
module=`echo $item | awk -F" . " '{print $3}'`
user=`echo $item | awk -F" . " '{print $4}'`
$INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
--event CLOSE_WRITE . create . move $dir | while read date time file event
do
#echo $event'-'$file
case $event in
MODIFY|CREATE|MOVE|MODIFY . ISDIR|CREATE . ISDIR|MODIFY . ISDIR)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
cmd="$RSYNC -zahqzt --exclude='*' --password-file=$PASS \
--include=$file $dir $user@$host::$module > /dev/null 2>1&"
echo $cmd
$cmd
fi

MOVED_FROM|MOVED_FROM . ISDIR|DELETE . ISDIR)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file \
$dir $user@$host::$module > /dev/null 2>1&"
echo $cmd
$cmd
fi

esac
done &
done

chmod +x /root/inotify_monitor.sh

Set RSYNC automatic login authentication password
vi /root/rsync.pwd
xxxxxx
Save and exit

Set permissions that only ROOT can view.
chmod 0600 /root/rsync.pwd

The following is the configuration of standby slave B :
Install RSYNC
yum rsync -y

#-- configure RSNYD service

vi /etc/rsyncd.conf
The content is as follows, Apache needs to be changed to the user name of your website. Mine is because apache was used before, although now Nginx is used, the user name has not been changed to


uid = apache
gid = apache
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[bbsatt]
path = / From the server B Local directory to hold backups 
ignore errors
read only = no
list = false
hosts allow =  The primary server A the IP
auth users = rsync
secrets file = /etc/rsync.pas

vi /etc/rsync.pas
rsync:xxxxxx

chmod 0600 /etc/rsync.pas

Start the RSYNCD
rsync --daemon
Add boot auto startup service :
vi /etc/rc.local
rsync --daemon
Back to the main server A,
vi /etc/rc.local
Add the following content, real-time boot automatic synchronization :


/root/inotify_init.sh
/root/inotify_monitor.sh

Save exit

Run:


/root/inotify_init.sh
/root/inotify_monitor.sh

This allows you to synchronize your image files in real time. Test this by creating a new file in the sync directory of A on the main server.


Related articles: