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

JavaScript to achieve all or reverse selection function

This article shares the specific code of JavaScri...

DHTML objects (common properties of various HTML objects)

!DOCTYPE Specifies the Document Type Definition (...

Several common methods of CSS equal height layout

Equal height layout Refers to the layout of child...

Example of using JS to determine whether an element is an array

Here are the types of data that can be verified l...

How to run the react project on WeChat official account

Table of contents 1. Use the a tag to preview or ...

Vue gets token to implement token login sample code

The idea of ​​using token for login verification ...

Vue implements simple calculator function

This article example shares the specific code of ...

MySQL5.7.27-winx64 version win10 download and installation tutorial diagram

MySQL 5.7 installation We are learning MySQL data...

JavaScript realizes the generation and verification of random codes

The generation and verification of random codes i...

Detailed process of using nginx to build a webdav file server in Ubuntu

Install nginx Note that you must install nginx-fu...

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...