rsync push data rsync+inotify real-time synchronization

 

 

Synchronize the /var/www/html directory on the [root @localhost 1 ~] source server to the /web-back directory on the [root @localhost ~2] target host in real time.

 

Overview

The Linux kernel has provided the inotify notification interface since version 2.6.13, which is used to monitor various changes in the file system, such as file access, deletion, and movement. Using this mechanism, it is very convenient to realize file change alarms, incremental backups, and respond in time to changes in directories or files. Using the combination of the rsync tool and the inotify mechanism can realize triggered backup (real-time synchronization). As long as the document in the original location changes, the incremental backup operation will be started immediately, otherwise it will be in a static waiting state. Fixed periodic backups have problems such as delays and over-dense cycles.

 

Environment System centos/6.9

inotify-tools source: xuegod-63 IP: 192.168.153.128

Target: [root @localhost ~2] IP: 192.168.153.133

rsync  包  http://mirrors.163.com/centos/6.9/os/x86_64/Packages/rsync-3.0.6-12.el6.x86_64.rpm

inotify package http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
 

Check whether inotify is supported, and it has been officially incorporated into the kernel since kernel 2.6.13.

 

 

[root@localhost 1 ~]# uname -r

2.6.32-220.el6.x86_64

[root@localhost 1 ~]# ls /proc/sys/fs/inotify/

max_queued_events  max_user_instances  max_user_watches

Note:

In the linux kernel, the default inotify mechanism provides three control parameters:

max_queued_events #represents the monitoring event queue

max_user_instances #Indicates the maximum number of monitored instances

max_user_watches #Indicates the maximum number of monitored files per instance

 

View specific parameter values:

[root@localhost 1 ~]# cat  /proc/sys/fs/inotify/max_queued_events 

16384

[root@localhost 1 ~]# cat  /proc/sys/fs/inotify/max_user_instances 

128

[root@localhost 1 ~]#  cat  /proc/sys/fs/inotify/max_user_watches

8192

Note: When the number of directories and files to be monitored is large or changes frequently, increase the values ​​of these three parameters.

For example, you can directly modify the /etc/sysctl.conf configuration file, set the management queue to 32768, the number of instances to 1024, and the number of monitoring to 9000000 (recommended to be greater than the total number of files of the monitoring target)

install rsync

[root@localhost 1 ~]# tar zxvf rsync-3.1.2.tar.gz 
[root@localhost 1 ~]# cd rsync-3.1.2
[root@localhost rsync-3.1.2]# ./configure && make  && make install

Custom configuration file

[root@localhost 1 ~]# vim /etc/syscrl.conf #At the end of the file, add the following

fs.inotify.max_queued_events = 32768

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 90000000

[root@localhost 1 ~]# sysctl -p #Make the modified sysctl.conf file take effect

 

Install inotify-tools-3.13.tar : You will have inotifywait and inotifywatch auxiliary tool programs to monitor and summarize file system changes.

[root@localhost 1 ~]# tar -zxvf inotify-tools-3.13.tar.gz #Decompress the files

[[root@localhost 1 ~]# cd inotify-tools-3.13

[root@localhost ~1 inotify-tools-3.13]# ./configure 

[root@localhost ~1 inotify-tools-3.13]# make

[root@localhost ~1 inotify-tools-3.13]# make  install

 

test:

Use the inotifywait command to monitor changes to the website directory /var/www/html. Then add and move files to the /var/www/html directory in another terminal, and view the screen output.

 

[root@localhost ~1 ]# inotifywait -h   

Common parameters:

-e is used to specify which events to monitor. 

These events include: create, move, delete, modify, modify file content, and attrib attribute changes.

-m means continuous monitoring

-r means recurse the entire directory

-q Simplify output information.

 

report an error

inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open

 

shared object file: No such file or directory

 

[root@localhost 1 ~]# find / -name libinotifytools.so.0

/root/inotify-tools-3.14/libinotifytools/src/.libs/libinotifytools.so.0

/usr/local/lib/libinotifytools.so.0

 

 

There are two possibilities

not installed on the operating system

The operating system cannot find the library

As shown in the figure, there are installation instructions that the operating system cannot find

Method 1:
echo "/usr/lib64/mysol" >> /etc/ld.so.conf 

ldconfig

ld.so.conf dynamic link library loader, the operating system will find the library according to the path written here.

ldconfig tells the operating system to introduce new variables and needs to be re-identified.

Method Two:

export LD_LIBRARY_PATH=/usr/local/inotify/lib:$LD_LIBRARY_PATH 

/usr/local/inotify/lib is the path of the library used by the source package. When the environment variable is introduced, the operating system will look for the class library under the variable.

 

find it now

 

 

Open two terminals on 1.1

1 Enable monitoring on the terminal

[root@localhost ~1 ]# inotifywait -mrq -e create,move,delete,modify /var/www/html/

#Enable monitoring has the functions of creating, moving, deleting and modifying, monitoring data changes under /var/www/html/'

 

2 Create, move, delete and other commands on the terminal

[root@localhost 1 ~]# echo aaa > /var/www/html/a.htal

[root@localhost 1 ~]# mkdir /var/www/html/test

[root@localhost 1 ~]# cp /etc/passwd /var/www/html/test/

[root@localhost 1 ~]# rm -rf /var/www/html/test/passwd

 

View the data information changes on the 1 terminal

[root@localhost 1 ~]# inotifywait -mrq -e create,move,delete,modify /var/www/html/

/ var / www / html / test / CREATE passwd

/ var / www / html / test / MODIFY passwd

/ var / www / html / test / DELETE passwd

#We can see that the meaning of the command on the 2 terminal just now is presented on the 2 terminal, so that the data changes of /var/www/html/ are successfully monitored! In the monitoring results output by inotifywait, each line of records includes directories, events, and files in turn. From this, changes can be identified.

Writing Triggered Synchronization Scripts

 

first step,

No password is required when syncing:

After [root@localhost 1 ~] changes, directly synchronize the changed data to [root@localhost ~2], no password is required during synchronization

sync key

ssh-keygen -t rsa ##Generate the public key of this host

 

 

The public key is successfully generated and stored in /root/.ssh/id_rsa.pub

 

After generation, a ".ssh" folder will be generated in the user's root directory 

 Entering ".ssh" will generate the following files

 Authorized_keys: store the public key for remote password-free login, mainly through this file to record the public keys of multiple machines

  id_rsa : generated private key file

  id_rsa.pub : the generated public key file

  know_hosts : List of known host public keys

    If you want the ssh public key to take effect, at least the following two conditions must be met:

      1) The permissions of the .ssh directory must be 700 

     2) .ssh/authorized_keys file permissions must be 600

 

 

[[email protected]]# scp -p  /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys

Copy your public key to the server 153.133 you want to log in to.

 

test

[root@localhost .ssh]# ssh 192.168.153.133

Last login: Thu Sep 14 16:25:04 2017 from 192.168.153.128

[root@localhost ~]# exit

logout

Connection to 192.168.153.133 closed.

 

#You have successfully logged in to the remote client without a password

 

 

Step 2:

Writing Triggered Synchronization Scripts

[root@localhost 1 ~]# vim a.sh

#!/bin/bash

inotifywait -mrq -e create,move,delete,modify /var/www/html/  | while read a b c

do

rsync -azP --delete /var/www/html/ [email protected]:/web-back

done

 

 

Notice:

The rsync link here is the default ssh port 22 link, so if the ssh port is changed, it will be changed to

rsync -azP --delete -e 'ssh -p xxx' /var/www/html/ [email protected]:/web-back

Add class execute permission to the script

[root@localhost 1 ~]# chmod +x a.sh

 

Clear everything under /var/www/html/

[root@localhost 1 ~]# rm -rf  /var/www/html/*

 

1 Execute the script on the terminal

[root@localhost 1 ~]# ./a.sh

 

2 Test on the terminal - copy files and create directories

[root@localhost 1 ~]# cp -r /boot/grub/ /var/www/html/

[root@localhost 1 ~]# mkdir /var/www/html/test

[root@localhost 1 ~]# mkdir /var/www/html/111

 

1 View the execution script on the terminal

[root@localhost 1 ~]# ./a.sh

wKiom1j4b6qD1080AAA1X7CBDhA744.png 

See

View directory on 1.64

[root@localhost 2 ~]# ls /web-back/

111  grub  test

 

Startup monitoring

[root@localhost 1 ~]# echo '/root/a.sh &' >> /etc/rc.local

 

Optimize this script:

[root@localhost ~1 ]# vim a.sh 

#!/bin/sh

SRC = / var / www / html

[email protected]:/web-back

inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F  

        do 

                /usr/bin/rsync -ahqzt --delete $SRC $DST

        done

[root@localhost ~1 ]# cp /root/a.sh /opt/inotify_rsync.sh

[root@localhost ~1 ]# echo '/opt/inotify_rsync.sh &'  >> /etc/rc.local

 

 

 There are more complete other operations 

https://www.linuxidc.com/Linux/2017-05/143462.htm

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325062808&siteId=291194637
Recommended