centos 7 configuration inotify + rsync real-time synchronization

Linux kernel provides notification inotify interface used to monitor a variety of changes in the file system, such as file access, delete, move, modify, and so on. Using this mechanism, you can very easily implement file movement alarm, incremental backup, and timely response to changes in the directory or file.

As long as there is a change in the position of the original document, then immediately start the incremental backup operation, or in a silent state, so, to avoid the periodic backup - rsync will inotify mechanisms and tools combined, can achieve triggered backups (real-time synchronization) the latency period is too dense and so on.

在Linux内核中,默认的inotify机制提供了三个调控参数:
[root@localhost myweb]# cat /proc/sys/fs/inotify/max_queued_events 
16384                   #监控事件队列数
[root@localhost myweb]# cat /proc/sys/fs/inotify/max_user_instances 
128                       #最多监控实例数
[root@localhost myweb]# cat /proc/sys/fs/inotify/max_user_watches 
8192                      #每个实例最多监控文件数

When the directory to be monitored, when the number of files or large changes frequently, it is recommended by modifying the " /etc/sysctl.conf " increase of these three parameters (standard multiple of 1024):

[root@localhost myweb]# vim /etc/sysctl.conf 
                 ..............................
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost myweb]# sysctl -p              #更新并查看该参数是否生效
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

The next operation is then configured on an rsync server environments, as well as the configuration, the blog post link: https://blog.51cto.com/14154700/2404358

Now all operations are based server B (192.168.1.2) this server:

1, the installation inotify-tools:
Get Source Package:

Links: https://pan.baidu.com/s/1ts6zSx3W5ybZL58tkS8enA
extraction code: trfg

Unpack and install:

[root@localhost media]# tar zxf inotify-tools-3.14.tar.gz -C /usr/src
[root@localhost media]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure && make && make install

2, the installation can be used, on the monitor now B Server / var / WWW / HTML directory:

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

Specific options for this command is explained as follows:

-e: Specify which monitor events;
-m: represents the continuous monitoring;
-r: Recursive represent the entire directory;
-q: simplify the output information;
the Modify: Modify;
the Create: create;
the Move: move;
the Delete: Delete;
attrib: property change;

Now switch a terminal, create a directory under surveillance 1.html file, the command under the supervision of the terminal will prompt information, tips are as follows ::

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

3, now you can write a script to achieve real-time synchronization:

[root@localhost /]# vim /opt/inotify_rsync.sh

#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
rsync_cmd="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::wwwroot"
$inotify_cmd | while read A B C
do
        $rsync_cmd
done

[root@localhost /]# chmod +x /opt/inotify_rsync.sh 
[root@localhost /]# echo '/opt/inotify_rsync.sh' >> /etc/rc.local            :设置开机自动运行该脚本

Verify that the script is in effect is as follows:

  1. On this machine server B is running /opt/inotify_rsync.sh script.
  2. Switch to a machine / var / www / html / directory, perform add, delete, modify files and so on.
  3. See server A server / var / www / changes in html / directory.

Precautions:

  • Remember if the test script written in error, you can manually execute the next script, test goes into effect, if the error message, you can copy the script commands out, execute it, to see if an order is wrong, and pay attention to the relevant directory on both servers rights issues, such as whether to read and write and so on.
  • CI shall be provided on the rsync server to read only = no.

Guess you like

Origin blog.51cto.com/14154700/2404424