I've written it before. Timed backups via rsync: Windows2008 and Centos7.0 via rsync for Updates (backup), and now the need for real-time synchronous backups via rsync and INotify. The first step: Getting Ready for work 1. INotify Introduction
Inotify is a Linux feature that monitors file system operations such as read, write, and create. The Inotify is responsive, very simple to use, and much more efficient than the busy polling of cron tasks. Learn how to integrate INotify into your application and discover a set of command-line tools that you can use to further automate system governance. (from Baidu Encyclopedia)
2. Rsync Introduction
Rsync is a data mirroring Backup tool--remote Sync under Unix-like systems. A fast incremental backup tool remote sync that supports local replication or synchronizes with other SSH and rsync hosts. (from Baidu Encyclopedia)
3. Rsync and INotify Real-time synchronization schematic diagram 4. Environment Deployment
(1) database server (inotify-master) ip:192.168.221.131
(2) Backup server (Inotify-slave) ip:192.168.221.136
Step Two: Deploy the backup server Inotify-slave
Here is the deployment of the Inotify-slave environment, configuring the Rsync daemon mode of Operation 1. Check if rsync is installed
2. Add rsync users and module catalogs and change their user groups
1 |
Useradd rsync-s/sbin/nologin-m #添加rsync用户 |
1 |
Mkdir/jhonse/back #创建rsync Daemon Module directory for working mode |
1 |
Chown Rsync.rsync/jhonse/back #更改模块目录的用户组 |
3. Configure the Rsync configuration file/etc/rsyncd.conf
01 |
#/etc/rsyncd:configuration file for rsync daemon mode |
03 |
# See rsyncd.conf Mans page for more options. |
05 |
# Configuration Example: |
11 |
# pid File =/var/run/rsyncd.pid |
12 |
# exclude = lost+found/ |
13 |
# Transfer logging = yes |
15 |
# Ignore nonreadable = yes |
16 |
# dont compress = *.gz *.tgz *.zip *.z *. Z *.rpm *.deb *.bz2 |
20 |
# comment = FTP export area |
22 |
#工作中指定用户 (requires a specified user) |
26 |
#相当于黑洞. Error targeting |
34 |
PID file =/var/run/rsyncd.pid |
36 |
Lock file =/var/run/rsync.lock |
38 |
Log file =/var/log/rsyncd.log |
48 |
#表示网络权限可写 (Local control is truly writable) |
53 |
Hosts allow = 192.168.221.0/255 |
54 |
#拒绝链接的地址, the following represents a link that is not rejected. |
55 |
Hosts Deny = 0.0.0.0/32 |
58 |
Auth users = Rsync_backup |
60 |
Secrets file =/etc/rsync.password |
4. Configure the virtual user's password file
Create a Rsync.password file in the/etc directory and add a virtual account and password
Format: Account: Password
5. Rsync.password The password file to increase security
1 |
chmod 600/etc/rsync.password |
6. Start the Rsync service
1 |
NETSTAT-LNUTP |grep rsync |
Step Three: Deploy the database server (inotify-master)
Description: INotify was installed and executed by the Rsync client 1. See if the current system supports inotify
1 |
ll/proc/sys/fs/inotify/ |
If max_queued_events, Max_user_instances, max_user_watches are shown, support inotify
1 |
/proc/sys/fs/inotify/max_queued_evnets |
2 |
Represents the maximum value assigned to the number of events that can be queued in inotify instance when calling Inotify_init, an event that exceeds this value is discarded, but the In_q_overflow event is triggered. |
3 |
/proc/sys/fs/inotify/max_user_instances |
4 |
Represents the maximum number of inotify instatnces that each real user ID can create. |
5 |
/proc/sys/fs/inotify/max_user_watches |
6 |
Represents the maximum number of directories that can be monitored per inotify instatnces. If you monitor a large number of files, you need to increase the size of this value appropriately, depending on the situation. |
7 |
Example: Echo 30000000 >/proc/sys/fs/inotify/max_user_watches |
2. Download the inotify source package and compile the installation
1 |
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下载inotify源码包 |
2 |
ll inotify-tools-3.14.tar.gz |
3 |
TAR-ZXF inotify-tools-3.14.tar.gz |
5 |
./configure--prefix=/usr/local/inotify-tools-3.14 #配置inotify, and specify the installation path to/usr/local/inotify-3.14 |
3. INotify's inotifywait command common parameters detailed
01 |
cd/usr/local/inotify-tools-3.14/ |
02 |
./bin/inotifywait--help |
03 |
-r|--recursive Watch directories recursively. #递归查询目录 |
04 |
-q|--quiet print (only print events). #打印监控事件的信息 |
05 |
-m|--monitor Keep listening for events forever. Without this option, inotifywait would exit after one event is received. #始终保持事件监听状态 |
06 |
--excludei <pattern> like--exclude but case insensitive. #排除文件或目录时, case insensitive. |
07 |
--TIMEFMT <fmt> strftime-compatible format string for use with%T in--format string. #指定时间输出的格式 |
08 |
--format <fmt> Print using a specified printf-like format string; Read the man page for more details. |
10 |
-e|--event <event1> [-e|--event <event2> ...] Listen |