Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Real-time synchronization tool principle explanation and environment preparation inotify

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Real-time synchronization ensures that the risk of data loss is minimized when the primary server fails. The principle is that when the primary server receives data, it is copied to another server at the same time to ensure that there is an extra copy of data. Tools used: inotify or sersync, this time demonstrated using inotify.

The file system (inode, block, etc.) is monitored at all times when inotify is running

When inotify monitors that there are add, modify, delete and move operations to the directory, it will call the rsync command to transfer the directory to the hot standby.

Deployment:

Install inotify and check if the system supports inotify

The Linux kernel has joined inotify since 2.6.13.

Support is shown as follows

There is no inotify with ISO, so you need to download it yourself.

Https://github.s3.amazonaws.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

C compiler is required to install inotify, and gcc compiler needs to be installed in advance.

Otherwise, this error may occur.

Can you use echo $? Check to see if there is an error 0 no error 1 has an error

Installation commands:

Tar zxf inotify-tools-3.14.tar.gz

Cd inotify-tools-3.14/

. / configure-- prefix=/usr/local/inotify-tools-3.14

Echo $?

Make & & make install

Echo $?

Ln-s / usr/local/inotify-tools-3.14/ / usr/local/inotify-tools

Ls / usr/local/inotify-tools

History

A total of 2 tools (commands) are installed, inotifywait and inotifywatch

Inotifywait: waits for a specific file system event (open, close, delete, etc.) to occur on the monitored file or directory, and is blocked after execution, which is suitable for use in shell scripts. (monitor file status)

Inotifywatch: collect monitored file system usage statistics, which refers to the number of times the file system time occurs.

Inotifywait

Parameter resolution:

-r Recursive query directory

-Q print very little information, only print information about monitoring events

-m always keeps the event listening state

-- excludei is case-insensitive when excluding files or directories

-- timefmt specifies the format of the time output

-format:

W represents the directory where the event occurred

% f represents the file in which the event occurred

% e indicates the event that occurred

Xe events are separated by "X"

% T uses the time format defined by-timefmt

-e event

The following options are followed by-e

Test:

Open 2 creation, monitor and execute all the time.

[root@nfs-server ~] # / usr/local/inotify-tools/bin/inotifywait-mrq-- timefmt'% d/%m/%y% HGV% M'-- format'% T% w% f'- e create / data

The parameters are explained as follows:

-r Recursive query directory

-Q print very little information, only print information about monitoring events

-m always keeps the event listening state

Timefmt timestamp format "day, month, year and hour"

-format:

W represents the directory where the event occurred

% f represents the file in which the event occurred

% T uses the time format defined by-timefmt

When I create a file, it will be recorded automatically.

[root@nfs-server ~] # / usr/local/inotify-tools/bin/inotifywait-mrq-- timefmt'% d/%m/%y% HRV% M'-- format'% T% eVO% w% f'- e create / data

22-11-19 11:21 CREATE:/data/12345.txt

Create a script to automatically synchronize when monitoring changes to the file.

#! / bin/bash

Inotify=/usr/local/inotify-tools/bin/inotifywait

$inotify-mrq-- format'% w% f'-e create,close_write,delete / data\

| | while read file |

Do

Cd / data & &

Rsync-az. /-- delete rsync_backup@192.168.24.32::backup\

-- password-file=/etc/rsync.password

Done

Run the script in the full path background and write to rc.local

/ bin/sh / rsync.sh &

After this step is completed, basically whenever there are additions, deletions or changes to the / data directory, it will be automatically synchronized to the / backup/data directory on the backup server.

This solution (for small and medium-sized enterprises) can be used within 200 to 300 concurrency in application scenarios.'

Real-time synchronization optimization and application limit suggestion

There are three files in the / proc/sys/fs/inotify directory, which have certain restrictions on the inotify' mechanism.

Max_user_watches: sets the number of files that can be monitored by the inotifywait or inotifywatch command (single process)

Max_user_instances: sets the number of processes each user can allow for inotifywait or inotifywatch commands

Max_queued_events: sets the amount of time that the inotify instance time (event) queue can hold.

Advantages of Inotify:

1) Real-time data synchronization

Disadvantages of Inotify:

1) if the concurrency is greater than 200 files (10-100K), synchronization will be delayed.

2) after monitoring the event, calling rsync synchronization is single-process (plus-concurrency), and sersync multi-process synchronization. If you have inotify-tools, why do you need sersync?

Sersync has many features:

1) configuration file.

2) the real daemon socket.

3) the failed file can be retransmitted regularly (scheduled task).

4) third-party HTTP interface.

5) Multi-thread synchronization by default.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report