Realization of real-time file synchronization between Linux servers

Realization of real-time file synchronization between Linux servers

Usage scenarios

For existing servers A and B, if the content in the specified directory of server A (for example, /home/paul/rsync/ ) changes (additions, deletions, modifications, and attribute changes), these changes will be synchronized to the target directory of server B (for example, /home/paul/rsync/ ) in real time.

Data mirroring backup tool Rsync

Rsync is an extremely fast and flexible file copying tool. It supports file copying between the local machine and a remote server. Rsync uses a delta-transfer algorithm, which only needs to transfer the difference between the source and target files, greatly reducing the consumption of network bandwidth and the time spent on copying. Rsync is mostly used for data backup and mirroring.

Rsync uses a fast check algorithm to determine whether a file needs to be synchronized by comparing changes in file size or last modification time.

There are two ways to connect to a remote host using Rsync: using ssh or using rsync daemon. Rsync is used here to implement remote file backup.

Installation and operation of Rsync

Install Rsync

Execute in the terminals of Server A and Server B respectively:

sudo yum install rsync

After the installation is complete, you will find that the rsync configuration file is located in etc/rsyncd.conf. This file is required when daemon is used for synchronization and is not introduced here.

Configure password-free login between servers A and B

Server A executes:

ssh-keygen
ssh-copy-id IP address of server B

Create source and target directories

In Server A:

mkdir /home/paul/rsync

In Server B:

mkdir /home/paul/rsync

Create a test file on Server A

echo "Hello from Server A" >> /home/paul/rsync/demo.txt

Execute file transfer command

On Server A, run:

# (1)
rsync -avPz --progress /home/paul/rsync 192.168.100.130:/home.paul/rsync
# (2)
rsync -avPz --delete --progress /home/paul/rsync 192.168.100.130:/home.paul/rsync

You will find that demo.txt also appears in the /home/paul/rsync directory on server B.

Command analysis:

(1) Copy the files in the /home/paul/rsync directory on server A to /home.paul/rsync on server B (192.168.100.130).

(2) Compare the files on the target side and the files on the source side. If the file on the target side does not exist on the source side, delete the file on the target side.

Problems with Rsync

Rsync is only a file copy tool, it cannot monitor the addition, deletion, and modification operations of source files. After making changes on the source side, you need to execute the rsync command to synchronize the changes to the target side.

Rsync needs to scan the entire directory before each synchronization. If there are many files in the source directory, scanning may take a long time.
In order to meet the requirements of real-time monitoring, we need to introduce another tool: inotify.

File system event monitoring tool inotify

inotify-tools provides a simple interface to inotify. It is a library written in C language and also includes command line tools.

For a detailed introduction to inotify-tools, please click: https://github.com/rvoicilas/inotify-tools/wiki

Installation of inotify-tools

For centos7 system, execute in sequence:

yum install -y epel-release
yum --enablerepo=epel install inotify-tools

Use the inotifywait command to monitor events

The monitoring script is as follows (inotifywait-rsync.sh):

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /home/paul/rsync/ | while read file
do
rsync -avPz --progress /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/
rsync -avPz --delete /home/paul/rsync/ 192.168.100.130:/home/paul/rsync/
echo "${file} was synchronized"
done

Parameter parsing

  • -m keeps monitoring. If this parameter is not set, inotifywait will exit after monitoring one event.
  • -r monitor directories recursively.
  • -q Quiet mode, print less output.
  • --timefmt specifies the output format of time.
  • --format Specifies the format of event output.
  • -e Set the event type to monitor. Here we monitor additions, deletions, modifications and metadata changes.

For each triggered listening time, inotifywait executes the code between do and done. Here, we call the rsync command mentioned earlier to synchronize files.

Add the monitoring script to crontab

crontab -e
* * * * * sh /home/paul/inotifywait-rsync.sh

References
https://rsync.samba.org
https://github.com/rvoicilas/inotify-tools/wiki

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux uses the scp command to copy files to the local computer and copy local files to the remote server
  • How to delete folders, files, and decompress commands on Linux servers
  • How to use Samba to build a shared file service on a Linux server
  • How to upload and download files between Linux server and Windows system
  • Python reads files on Linux server
  • How to upload files and folders to Linux server via SSH
  • How to transfer files between Linux local and server and write commands for uploading and downloading files on Linux server
  • Automatic file synchronization between two Linux servers

<<:  What to do if you forget your Linux/Mac MySQL password

>>:  jQuery implements a simple comment area

Recommend

MySQL 8.0.21 installation tutorial under Windows system (illustration and text)

Installation suggestion : Try not to use .exe for...

Python writes output to csv operation

As shown below: def test_write(self): fields=[] f...

Vue realizes screen adaptation of large screen pages

This article shares the specific code of Vue to a...

How to use jconsole to monitor remote Tomcat services

What is JConsole JConsole was introduced in Java ...

Detailed explanation of 5 solutions for CSS intermediate adaptive layout

Preface When making a page, we often encounter co...

Build a server virtual machine in VMware Workstation Pro (graphic tutorial)

The VMware Workstation Pro version I use is: 1. F...

Facebook's nearly perfect redesign of all Internet services

<br />Original source: http://www.a-xuan.cn/...

JS realizes the card dealing animation

This article example shares the specific code of ...

Explanation of the working principle and usage of redux

Table of contents 1. What is redux? 2. The princi...

Detailed analysis of the principles and usage of MySQL views

Preface: In MySQL, views are probably one of the ...

Example of implementing the skeleton screen of WeChat applet

Table of contents What is a skeleton screen How t...

Centos7 installation and configuration of Mysql5.7

Step 1: Get the MySQL YUM source Go to the MySQL ...

Tutorial on migrating mysql from phpstudy to Linux

Project Purpose Migrate the data in MySQL 5.5.53 ...