Configuring inotify + rsync real-time synchronization

Rsync command of the service and are not familiar with, you can refer to Bowen through rsync remote synchronization
here is not to say!

Linux kernel version 2.6.13 from the beginning has been to provide a 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, it can be very convenient for file movement alarm, incremental backup, and timely response to changes in the directory or file.

The inotify mechanism combining rsync tool, you can backup (real-time synchronization) departure - as long as the original location of the document changed immediately start an incremental backup, or wait in silence state, as shown:
Configuring inotify + rsync real-time synchronization
In this way, avoid delayed present when a fixed period of backup, the problem period is too dense!

It is precisely because inotify notification mechanisms provided by the Linux kernel, so the main job of this machine monitoring, more suitable for the application in the uplink synchronization triggered backup.

The experiment required package inotify-tools-3.14.tar.gz network disk link: https://pan.baidu.com/s/1Ov006-6MV76VMVWL1LFyAA
extraction code: np88

配置inotify+rsync实时同步大致分为4步:
(1)调整inotify内核参数;
(2)安装inotify-tools软件包;
(3)编写触发式同步脚本;
(4)测试实验效果。

This experiment environment based on secondary blog post: the remote synchronization via rsync

The following describes the sequence and configuration ( the following operations are performed in the server B !):

(1) adjusting kernel parameters inotify

In the Linux kernel, the default inotify regulatory mechanism provides three parameters:
(. 1) max_queue_events: monitoring queue size (operational event);
(2) max_user_instances: a maximum number of monitoring (monitoring directory) Example;
(. 3) max_user_watches: Each examples of monitoring the maximum number of files.

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192
//系统的默认值,如果有需要可以自行修改!
[root@localhost ~]# vim /etc/sysctl.conf
                     …………                //省略部分注释内容
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost ~]# sysctl -p
//立即生效

Number Normally, the set value is greater than the recommended surveillance monitoring target total file!

(2) Installation of package inotify-tools

inotify-tools package inotify-tools can be downloaded from the official website, can also be downloaded via network disk beginning of the article link.
In the client installation:

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

After the installation is complete inotify-tools tools, will produce two commands:
inotifywait: for continuous monitoring, real output;
inotifywatch: for short-term monitoring, then the task is completed the results.

命令所使用的参数有:
-m,持续进行监控
-r,递归监控所有子对象
-q,简化输出信息
-e,指定要监控哪些事件类型

More information for the command can refer to their man pages!

To monitor the site root as an example:

[root@localhost ~]# inotifywait -mrq -e modify,move,create,delete,attrib /var/www/html
//以递归、持续监控整个目录的修改、移动、创建、删除、属性变更等各种事件

(3) write trigger synchronization scripts

Client scripting:

[root@localhost ~]# vim 123.sh
#!/bin/bash
A="inotifywait -mrq -e modify,move,create,delete /var/www/html"
B="rsync -azH --password-file=/root/123.pass /var/www/html/* [email protected]::wwwroot"
$A | while read DIRECTORY EVENT FILE
do
        if
                [ $(pgrep rsync | wc -l) -le 0 ]
        then
                $B
        fi
done
[root@localhost ~]# chmod 777 123.sh
[root@localhost ~]# vim /etc/rc.d/rc.local
             …………         //省略部分内容,编写以下内容
/root/123.sh &
[root@localhost ~]# chmod 777 /etc/rc.d/rc.local
//        /etc/rc.d/rc.local这个文件中主要存放一些开机自启动的脚本

(4) the effect of testing laboratories

Restart the client to test to see whether the content can be synchronized to the server!

[root@localhost ~]# pgrep 123.sh         //查看脚本开机是否已经自动运行
1033
1039
//表示脚本的进程号

Create a file on the client for testing:

[root@localhost ~]# touch /var/www/html/666

To authenticate server!

[root@localhost ~]# ls /var/www/html
666

The server has been synchronized automatically! Completion of the experiment!

Guess you like

Origin blog.51cto.com/14157628/2429904