Real-time synchronization of data inotify + rsync

Production needs: companies generally have more than one web in order to ensure data consistency and files on the web server or samba on nfs, convenient unified management,

Meanwhile, in order to find a secure data server can be real-time synchronization of data can be achieved by inotify + rsync + script

image.png

I. Introduction inotify monitoring software

        inotify can monitor changes to synchronize data server directory information, using asynchronous file system event monitoring mechanism, the use of event-driven

Mechanisms, without having to get through a polling mechanism, such as cron and other events, linux kernel from 2.6.13 onwards inotify support, through inotify can monitor

Control the file system to add, delete, modify, move, and other events.

image.png

ll / proc / sys / fs / inotify # files listed below, indicates that the server kernel supports inotify

-rw-r - r-- 1 root root 0 Dec 7 10:10 max_queued_events inotify event queue Maximum Length Default: 16384

-rw-r - r-- 1 root root 0 Dec 7 10:10 max_user_instances inotify instance created for each user Maximum Default: 128

-rw-r - r-- 1 root root 0 Dec 6 05:54 max_user_watches number of files that can be monitored (single-process), the default values: 8192


inotify kernel-level functions required by the user space program, installation package:

    yum install inotify-tools based source epel

inotify-tools package main documents:

1, inotifywait command

     Waiting for a specific file system events (open closedelete, etc.) occur on the monitored file or directory, commonly used in real-time synchronization

Directory monitor directly without using a one-time option to monitor, when the operation will end when the directory secondary monitor

image.png

inotifywait command common options

    -m, --monitor always an event listener

     -d, --daemon in daemon mode execution, and -m similar, with the use -o

    -r, --recursive recursive directory monitor data changes

    -q, --quiet output a small amount of event information

    --exclude <pattern> exclude specified file or directory, the extended use of the regular expression matching mold

    Type realization

    --excludei <pattern> and exclude similar insensitive

     -o, --outfile <file> print events to a file, right equivalent to the standard output

     -s, --syslogOutput send error corresponds to the standard error output syslog

     --timefmt <fmt> Specifies the time the output format

            --timefmt <fmt> Time Format, a reference man 3 strftime

            % Y Year information, including information Century

            % Y year information does not include information Century

            % M month display, range 01-12

            % D day of the month, range 01-31

            % H hours information, using 24-hour clock, range 00-23

            % M min, range 00-59

            Example:

            --timefmt "%Y-%m-%d %H:%M"

     --format <fmt> specified output format; i.e., the actual content monitoring output

    -e specify the listener specified event, if omitted, it indicates that all events are listening

             示例: -e create,delete,moved_to,close_write, attrib

Common combinations

Continuous background monitoring and logging

inotifywait -mrq  /data/www -o /root/inotify.log  --timefmt "%Y-%m-%d %H:%M" --format "%T %w%f event: %e"

image.png

Continuous background monitoring specific events

inotifywait -mrq /data -o /root/inotify.log  --timefmt "%F %H:%M" --format "%T %w%f event: %;e" -e create,delete,moved_to,close_write,attrib

2、inotifywatch:

Collecting the monitored file system statistics, statistics refers to the number of file system events

Second, the specific implementation

192.168.12.27 inotify server

192.168.12.57 rsync server

Monitoring inotify server data automatically synchronized to change rsync server

192.168.12.57 :

1, the installation package

yum install rsync  

2, the server configuration file to modify rsync

we /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.12.0/24
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass

3、生成服务器验证文件

echo "rsyncuser:123456" > /etc/rsync.pass

chmod 600 /etc/rsync.pass

4、创建文件用于存放备份文件

mkdir /backup

chmod 600 /backup/

5、启动rsyncd服务

rsync --daemon     可加入/etc/rc.d/rc.local实现开机启动

systemctl start rsyncd

192.168.12.27:

1、安装软件包,创建需要监控备份的文件夹

yum install inotify-tools

image.png

2、生成密码文件,如果没有会变成交互式命令,创建后rsync会使用该密码自动同步

echo "123456" > /etc/rsync.pass

chmod 600 /etc/rsync.pass

3、测试能否同步到rsync服务器

rsync -avz --password-file=/etc/rsync.pass /data/www/ [email protected]::backup

image.png

4、创建实时监控脚本

vi inotify_rsync.sh

#!/bin/bash
#
#********************************************************************
#Author:        swh
#QQ: 786529083
#Date: 2019-08-01
#FileName:inotify_rsync.sh
#blog: https://blog.51cto.com/14322729
#Description:The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
SRC='/data/www/'
DEST='[email protected]::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done

image.png


Guess you like

Origin blog.51cto.com/14322729/2425782
Recommended