Linux uses Rsync+Inotify to achieve real-time synchronization of local and remote data

Linux uses Rsync+Inotify to achieve real-time synchronization of local and remote data

0x0 Test Environment

The headquarters production server and the branch backup server require remote data backup. The environment is as follows

**centos 6.5**
Production server directory: /home/zytest/files
Backup server directory: /home/zytest/files
User/Password: zytest/zytest

0x1 Production server environment construction

0x1.1 Install gcc compiler and rsync

yum install gcc rsync -y

0x1.2 Copy inotify to the server and decompress it

cd /root
tar xfvz inotify-tools-3.13.tar.gz

0x1.3 Enter the inotify directory to install

 cd inotify-tools-3.13
 ./configure
 make
 make install     
 
 /usr/local/bin/inotifywait ##Check whether the installation is successful

0x2 Backup server environment construction

0x2.1 Install xinetd and rsync

yum install xinetd rsync -y

0x3 The following contents are synchronized between the two servers

useradd -u 600 zytest 
 passwd zytest    
 zytest
 su - zytest -c 'mkdir /home/zytest/files' ##Create a synchronization directory

0x4 Configure rsyncd on the backup server

0x4.1 Edit /etc/xinetd.d/rsync and modify it according to the following content

disable = yes ==> disable = no
flags = IPv6 ==> flags = IPv4
server_args = --daemon ==> server_args = --daemon --config=/etc/rsyncd.conf

0x4.2 Edit /etc/rsyncd.conf and add the following script information

uid = root
gid = root
use chroot = no
max connections = 1000
strict mode = yes
port = 873
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsyncd.lock 
log file = /var/log/rsyncd.log
# following for user "zytest", change for other users
[zytest]
path = /home/zytest
ignore errors
auth users =zytest
secrets file = /home/rsync-dst.ps
read only = no
list = false

Ps: The rsyncd configuration file is on xinetd, so the backup server installs xinetd

0x4.3 Write the password to the called password file and grant permissions

echo zytest:zytest >> /home/rsync-dst.ps
chmod 600 /home/rsync-dst.ps

0x4.4 Start rsync via xinetd

 /etc/rc.d/init.d/xinetd restart

0x5 Configure the inosync script file on the primary server

0x5.1 ** ##Edit /root/inosync and add script code **

#!/bin/sh
#chkconfig: 3 78 10

#This file exists from compile
if [ ! -f /usr/local/bin/inotifywait ]
then
 echo "cannot start. file inotifywait NOT exist!"
 exit
fi

#This file is a runnable shell script
if [ ! -f /usr/local/bin/inosync.so.1 ]
then
 echo "contact administrator. inosync.so.1 NOT exist!"
 exit
fi

case "$1" in
 'start')
 /usr/local/bin/inosync.so.1 &
;;

'stop')
 pid=`ps -ef | grep -v grep | grep "inotifywait" | awk '{print $2}'`
 kill -9 $pid 2>&1
;;

'restart')
 $0 stop
 $0 start
;;

esac

0x5.2 Grant script permissions and set it to start at boot

chmod a+x /root/inosync
 cp /root/inosync /etc/rc.d/init.d

0x5.3 Configuration call main script file /root/inosync.so.1

rhost=**Backup server IP**
user=zytest
src=/home/zytest/files
dst=zytest
#dst corresponding to [zytest] in file /etc/rsyncd.conf on dst server
log=/root/inosync.log
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M:%S' --format '%T %w%f %e' -e close_write,create,move,delete,attrib $src | while read files
 do
 echo == rsync begin == >> $log
 rsync -avP --password-file=/home/rsync-src.ps --delete $src $user@$rhost::$dst >> $log 2>&1
 echo -- rsyncd -- >> $log
 date >> $log
 echo "${files} was rsynced " >> $log 2>&1
 done

PS: There is a space after %T and there is also a space between %f and %e

0x5.4 Grant permissions to the inosync.so.1 script and copy it to /usr/local/bin

chmod a+x /root/inosync.so.1
cp /root/inosync.so.1 /usr/local/bin

0x5.5 Write the password to the called password file and grant permissions

echo zytest >> /home/rsync-src.ps
 chmod 600 /home/rsync-src.ps

0x6 The target server sets inosync to automatically start and turns on the inosync service

chkconfig --level 3 inosync on
/etc/rc.d/init.d/inosync start

0x7 Test END

Create files and folders in the /home/zytest/files directory on the production server and check whether the backup storage has also synchronized the files and folders. If so, synchronization is successful.

The process can be viewed through the log

tail -f /root/inosync.log

Summarize

This is the end of this article about how to use Rsync+Inotify in Linux to achieve real-time synchronization of local and remote data. For more information about how to use rsync+inotify to achieve remote real-time synchronization, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to Use rsync in Linux
  • Rsync beats all other backup tools. Can you manually block certain directories?
  • Python rsync folder synchronization script between servers
  • Python+rsync accurately synchronizes files in the specified format
  • Automatic refresh starts with BrowserSync
  • Rsync+crontab regular synchronization backup under centos7
  • Using Rsync to complete automatic backup in Linux
  • Java Rsync concurrent data migration and verification detailed explanation

<<:  Detailed explanation of the encapsulation and use of the Vue image magnifier component

>>:  Tutorial on installing MYSQL8.0 on Alibaba Cloud ESC

Recommend

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

Beginners learn some HTML tags (3)

Beginners who are exposed to HTML learn some HTML...

Vue implements tab navigation bar and supports left and right sliding function

This article mainly introduces: using Vue to impl...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

How to solve the problem of blurry small icons on mobile devices

Preface Previously, I talked about the problem of...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

Nginx location matching rule example

1. Grammar location [=|~|~*|^~|@] /uri/ { ... } 2...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Summary of the differences between count(*), count(1) and count(col) in MySQL

Preface The count function is used to count the r...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...