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

Inotify + rsync real-time synchronization

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1.1 introduction to inotify

Inotify is a powerful, fine-grained, asynchronous file system time monitoring mechanism. The Linux kernel has added inotify support from 2.6.13. Through inotify, it can monitor all kinds of events such as adding, deleting, modifying, moving and so on in the file system pool. Using this kernel interface, third-party software can monitor various changes of files under the file system, and inotify-tools officially implements such monitoring software. Chinese Zhou Yang developed a similar real-time synchronization software sersync in Jinshan Company.

1.1.1 install inotify

Please make sure that your Linux kernel reaches 2.6.13 before installing inotify-tools, and turn on the CONFIG_INOTIFY option when editing, or you can check it by using the following command.

Install inotify default conditions:

The yum installation method source information must be set correctly:

Default yum source (software repository): base+extras+updates

Domestic yum source (software warehouse): http://mirrors.aliyun.com/help/centos

Extended yum feeds (extended repositories): epel (extended repositories for enterprise Linux yum feeds)

Ali Cloud epel source: obtained by accessing Ali yum source image address

Install the Epel source directly: yum install epel-release

1.2 introduction to data synchronization principle 1.2.1 inotify+rsync Real-time synchronization

The real-time synchronization software is responsible for monitoring changes to the disk file system. For example, specify a / data directory, and the real-time synchronization software service process will monitor changes in data in this / data directory in real time, that is, changes in file system block and inode information.

Real-time synchronization software service process monitoring / data directory file system data once changed, the rsync command will be executed to push the changed file data information to the corresponding backup directory of the backup server, so as to complete the real-time backup of the data.

Rsync transmission efficiency is very high, but rsync also has shortcomings, the biggest problem is that each execution of the rsync command will traverse the target directory, when there are not many files, this is no problem, once the file count to a certain size, then each traversal will consume a lot of resources, if only the frequently changed directory synchronization, you can also ignore this problem, if each change of the directory is larger, then you will use inotify.

The inotify+rsync approach makes it more efficient, synchronizing only files that change frequently

The prerequisite for installing inotify software is that the rsync service is installed successfully, and you can push and pull data on the client.

In addition: inotify is installed in the client configuration of the software, do not install it wrong

1.2.2 timing task crond+rsync data synchronization

The way of rsync+ timing task will regularly synchronize the changed files in the system to the server, but the minimum unit of timing task is sub-unit. This is clearly intolerable for the financial industry, where data is changing fast and data is important. Therefore, this approach has some limitations.

1.3 introduction to inotify knowledge 1.3.1 main tools and commands of inotify software

[root@nfs01 ~] # rpm-ql inotify-tools # # query which packages are included in inotify-tools

/ usr/bin/inotifywait

/ usr/bin/inotifywatch

/ usr/lib64/libinotifytools.so.0

/ usr/lib64/libinotifytools.so.0.4.1

/ usr/share/doc/inotify-tools-3.14

/ usr/share/doc/inotify-tools-3.14/AUTHORS

/ usr/share/doc/inotify-tools-3.14/COPYING

/ usr/share/doc/inotify-tools-3.14/ChangeLog

/ usr/share/doc/inotify-tools-3.14/NEWS

/ usr/share/doc/inotify-tools-3.14/README

/ usr/share/man/man1/inotifywait.1.gz

/ usr/share/man/man1/inotifywatch.1.gz

[root@nfs01 ~] #

Inotifywait: waits for a specific file system time (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.

Inotifywatch: collect the monitored file system usage statistics, which refers to the statistics of the number of file system events occurring.

Description: yum can be used directly after installation, if you need to go to the bin directory of the corresponding software directory to use if you need to compile and install.

1.3.2 check whether the current system supports inotify

[root@nfs01 etc] # uname-r

2.6.32-642.el6.x86_64

[root@nfs01 etc] # ls-l / proc/sys

Sys/ sysrq-trigger sysvipc/

[root@nfs01 etc] # ls-l / proc/sys/fs/inotify/

Total 0

-rw-r--r-- 1 root root 0 May 12 16:26 max_queued_events

-rw-r--r-- 1 root root 0 May 12 16:26 max_user_instances

-rw-r--r-- 1 root root 0 May 12 16:26 max_user_watches

[root@nfs01 etc] # shows that the above three files show that inotify is supported

[root@nfs01 ~] # cd/proc/sys/fs/inotify/

[root@nfs01 inotify] # ls

Max_queued_events max_user_instances max_user_watches

[root@nfs01 inotify] # catmax_user_watches

8192

[root@nfs01 inotify] # catmax_user_instances

one hundred and twenty eight

[root@nfs01 inotify] # catmax_queued_events

16384

[root@nfs01 inotify] #

Key parameters description:

Max_queued_events: sets the number of events that the inotify instance event (event) queue can hold.

Max_user_instances: sets the number of processes that each user can run inotifywait or inotifywatch commands.

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

1.3.3 the working mechanism of inotify (belonging to the trigger mechanism)

The client starts the inotify service process to detect data changes in the shared directory

Get a change in the shared directory data and notify the execution of the rsync command to synchronize the data.

Send the change data to the shared directory on the rsync server (with the rsync daemon server turned on).

1.3.4 inotify synchronization command details parameters of 1.3.4.1 inotify command

Command parameter

Parameter description

-m |-- monitor

Always keep the event listening state

-d |-- daemon

Similar to the-m parameter, it only runs the command in the background, records the triggered event information in the specified file, and uses the-- outfile parameter to define the program log to use-- syslog parameter.

-r (important parameter)

Recursively monitor changes in directory data information

-o |-- outfile

Print events to a file, which is equivalent to standard correct output

-s |-- syslog

Sending an error to syslog is equivalent to standard error output

-Q |-- quiet

Less output information (only print event information)

-- exclude |

Case-insensitive when excluding files or directories

-- timefmt

Specify the format of the event output

-- format

Printing uses a format string similar to the specified output, that is, the contents of the actual monitoring output.

-e

Specifies to listen for the specified event. If omitted, it means that all events are monitored.

Get the monitoring event information through the / usr/local/inotify-tools/bin/inotifywait-- help command.

1.3.4.2 events for inotify

Event name

Event description

Access

The contents of a file or directory are read

Modify

The contents of a file or directory are written

Attrib

File or directory attribute change

Close_write

The file or directory is closed and closed after the write mode is turned on.

Close_nowrite

The file or directory is closed and closed after the read-only mode is turned on.

Close

File or directory closed, regardless of read or write mode

Open

The file or directory is opened

Moved_to

The file or directory is moved to the monitored directory

Moved_from

The file or directory is moved from the monitored directory.

Move

An event is triggered regardless of whether the file or directory is moved to or out of the monitoring directory

Create

The file or directory is created in the monitored directory.

Delete

The file or directory is deleted in the monitored directory

Delete_self

File or directory deleted

Unmount

The file system contains files or directories that cannot be unmounted

1.4 deploy rsync+inotify process 1.4.1 deploy rsync daemon mode (1)

Deploy rsync daemon mode

Server configuration

01: check if rsync is installed

02: configure rsyncd.conf profile

03: create rsync process management user

04: create the storage directory of the module

05: modify the owner and group permissions of the storage directory

06: configure authentication password file with authorization of 600 permissions

07: start the rsync daemon service

08: set the rsync service to boot automatically

09: conduct a test

Configuration of the client

01: create a password file and grant 600 permissions

02: testing by the client

Server code file:

[root@backup nfsbackup] # vim/etc/rsyncd.conf

(omit part of the content)

[backup] # # Module name

Path = / backup

[nfsbackup] # # create a module

Path = / nfsbackup

"/ etc/rsyncd.conf" 20L 474C written

Create a backup module

[root@backup ~] # mkdir / nfsbackup

Authorized main group

[root@backup] # chown-R rsync.rsync/nfsbackup/

Check the authorization status

[root@backup] # ll-d / nfsbackup/

Drwxr-xr-x 2 rsync rsync 4096 May 1109:22 / nfsbackup/

View password file

[root@backup ~] # cat/etc/rsync.password

Rsync_backup:123456

Check whether the password file permission is 600

[root@backup ~] # ll/etc/rsync.password

-rw- 1 root root 20 May 4 17:51 / etc/rsync.password

[root@backup ~] #

[root@backup ~] # / etc/init.d/xinetdstatus

Xinetd (pid 1810) is running...

[root@backup ~] # / etc/init.d/xinetdstop

Stopping xinetd: [OK]

[root@backup] # rsync-- daemon

[root@backup ~] # ps-ef | grep rsync

Root 4955 1 0 09:25? 00:00:00 rsync-daemon

Root 4959 4860 0 09:25 pts/0 00:00:00 grep rsync

[root@backup ~] #

1.4.2 install and deploy inotify (2) 1.4.2.1 manually test inotify software monitoring events

Open two identical xshell windows to test, and enter the following code in one window:

[root@nfs01 data] # inotifywait-mrq / data/

/ data/ DELETE oldboy.txt

/ data/ CREATE oldboy.txt

/ data/ OPEN oldboy.txt

/ data/ ATTRIB oldboy.txt

/ data/ CLOSE_WRITE,CLOSE oldboy.txt

Perform operations in another same window, such as create, delete, move, etc. Observe the changes.

Test the create event

Turn on the corresponding monitoring function of inotify on the NFS server and output the information

[root@nfs01 backup] # inotifywait-mqr-- timefmt'% d% y% HGV% M'-- format'% T% w% f'- e create / backup # Monitoring backup creation event display information

12/17 23:18 / backup/oldbpy.txt

Then open a NFS server connection window to test and verify the create event.

[root@nfs01 backup] # ls

M

[root@nfs01 backup] # mkdir oldbpy.txt

[root@nfs01 backup] # ls

M oldbpy.txt

[root@nfs01 backup] # rm oldbpy.txt/-fr

[root@nfs01 backup] #

Description: only monitor create creation events, other events are not monitored, other events are generated, and the monitoring service has no information output.

You can simplify commands and output information:

[root@nfs01 backup] # inotifywait-mqr-- format'% w% f'-e create / backup

/ backup/1.txt Monitoring / backup directory creation event display information.

/ backup/2.txt

/ backup/3.txt

/ backup/4.txt

/ backup/5.txt

Then open a NFS server connection window to test and verify the create event.

[root@nfs01 backup] # mkdir {1..5} .txt

[root@nfs01 backup] # ls

1.txt 2.txt 3.txt 4.txt 5.txt m

[root@nfs01 backup] # rm 3.txt/-fr # # deletion event is not monitored, and there is no information output on the other end.

[root@nfs01 backup] # ls

1.txt 2.txt 4.txt 5.txt m

[root@nfs01 backup] #

Test the delete event

Turn on the corresponding monitoring function of inotify on the NFS server and output the information

[root@nfs01 backup] # ls

1.txt 2.txt 4.txt 5.txt m

[root@nfs01 backup] # rm-fr 5.txt/ # # delete file test

[root@nfs01 backup] # rm-fr 2.txt/

[root@nfs01 backup] # mkdir oldboy.txt # # create a file test, but no information output

[root@nfs01 backup] #

Open a NFS server connection window to test and verify the delete event

[root@nfs01 backup] # inotifywait-mqr-- format'% w% f'-e delete / backup

/ backup/5.txt

/ backup/2.txt # # deletion will result in information output

Test the moved event

Turn on the corresponding monitoring function of inotify on the NFS server and output the information

[root@nfs01 backup] # inotifywait-mqr-- format'% w% f'-e move / backup

/ backup/oldboy.txt

/ backup/1.txt

Open a NFS server connection window to test and verify the move event

[root@nfs01 backup] # ls

1.txt 4.txt m

[root@nfs01 backup] # mv oldboy.txt//tmp/

[root@nfs01 backup] # mkdir 4

[root@nfs01 backup] # rm 4.txt/-fr

[root@nfs01 backup] # mv 1.txt/ / tmp/

[root@nfs01 backup] #

Note: only move mobile events are monitored, other events are not monitored, and other events are generated, and the monitoring service has no information output, so monitoring mobile events can be used to monitor the movement of files or directories in and out of the directory.

Knowledge points conclusion:

Through the above test experiments, it can be found that some monitoring events already contain the monitoring functions of other corresponding events, so the corresponding monitoring events can be simplified according to the requirements.

Important event

Include events

Remarks

Close

Close_write, close_nowrite

File or directory closed, regardless of read-write or write mode; that is, include write-close or read-close

Close_write

Create

Contains file creation events, but does not include directory creation events

Move

Move_to move_from

Files or directories trigger events regardless of whether they are moved to or out of the monitoring directory, that is, events that contain information to move into or remove from the monitoring directory.

Summary of important parameters: according to the above instructions, in actual use, you only need to monitor the following events: create creation, delete deletion, moved_to move in, close_write modification.

1.4.2.2 rsync+inotify combines with scripts

You need to push the change file information in the directory through the corresponding script. The scripting idea evolves as follows:

Environment preparation: add the nfsbackup module and nfsbackup directory on the server, and authorize the group and owner.

[root@backup nfsbackup] # cat/etc/rsyncd.conf

Tart##

Uid = rsync

Gid = rsync

Use chroot = no

Max connections = 200

Timeout = 300

Pid file = / var/run/rsyncd.pid

Lock file = / var/run/rsync.lock

Log file = / var/log/rsyncd.log

Ignore errors

Read only = false

List = false

Hosts allow = 172.16.1.0 Compact 24

Hosts deny = 0.0.0.0amp 32

Auth users = rsync_backup

Secrets file = / etc/rsync.password

[backup] # # Module name

Path = / backup

[nfsbackup] # # Module name

Path = / nfsbackup

[root@backup nfsbackup] #

[root@backup backup] # chown rsync.rsync / nfsbackup/

A script is written on the client to monitor the changes of information in the directory in real time and obtain the change information.

# step 1: write monitoring commands

#! / bin/bash

# desc: monitor / data and pushdatainfo to backup_server

/ usr/bin/inotifywait-mrq-- format "% w% f"-e moved_to,delete,create,close_write / data |\

Step 2: write a script, write a push command rsync for indifference synchronization-- delete / data/

While read line

Do

Rsync-az-delete / data/rsync_backup@172.16.1.41::nfsbackup-password-file=/etc/rsync.password

Done

Implement the script to monitor directory information events and let the script run in the background.

[root@nfs01 data] # sh/server/scripts/monitor.sh & > / dev/null &

[1] 9891

[root@nfs01 data] #

Advantages and disadvantages of 1.5 inotify

Monitor the changes of file system events and achieve real-time data synchronization through synchronization tools.

If the concurrency is greater than 200 files (4-100k), there will be a delay in synchronization.

The script written earlier is all pushed once each time, but it is really incremental, or you can only synchronize the changed files and ignore the changes.

After monitoring the event, use rsync synchronization is single-process (plus & concurrent), sersync is multi-process synchronization.

Inotify service optimization

[root@nfs01 etc] # ls-l / proc/sys/fs/inotify/

Total 0

-rw-r--r-- 1 root root 0 May 12 16:26 max_queued_events

-rw-r--r-- 1 root root 0 May 12 16:26 max_user_instances

-rw-r--r-- 1 root root 0 May 12 16:26 max_user_watches

[root@nfs01 inotify] # catmax_user_watches

8192

[root@nfs01 inotify] # catmax_user_instances

one hundred and twenty eight

[root@nfs01 inotify] # cat max_queued_events

16384

[root@nfs01 inotify] #

[root@nfs01 data] # cd/proc/sys/fs/inotify/

[root@nfs01 inotify] # ls

Max_queued_events max_user_instances max_user_watches

[root@nfs01 inotify] # echo "50000000" > / proc/sys/fs/inotify/max_user_watches

[root@nfs01 inotify] # echo "50000000" > / proc/sys/fs/inotify/max_queued_events

[root@nfs01 inotify] # Note: the above configuration restart may fail and need to be placed in the rc.local file.

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