Practice: rsync + inotify achieves real-time data synchronization and rsyncinotify
The Linux kernel has provided inotify notification interfaces starting from version 2.6.13 to monitor various changes in the file system, such as file access, deletion, and movement. With this mechanism, you can easily generate file change alerts and incremental backups, and respond to directory or file changes in a timely manner. You can monitor the time and actions of a user! Using this kernel interface, third-party software can monitor various file changes in the file system, while inotify-tools is the software that implements monitoring.
The combination of the rsync tool and inotify mechanism enables triggered backup (Real-Time Synchronization). If the document at the original location changes, the Incremental backup is immediately started; otherwise, the backup is in the static waiting state, in this way, you can avoid the delay, periodic password transfer, and other problems in the fixed-cycle backup.
Software: http://sourceforge.net/projects/inotify-tools/ # notify-tools-3.13
Test requirements: Synchronize the directory of test2 host/var/www/html to the directory of test1 host/var/www/html in real time;
Test Topic Diagram
[Root @ test2 ~] # Uname-r # view Linux Kernel
2.6.32-431. el6.x86 _ 64
[Root @ test2 ~] # Yum-y install xinetd rsync; # install the rsync Service
[Root @ test2 ~] # Yum-y install gcc-c ++, openssh-clients
Upload inotify-tools and decompress it for installation.
[Root @ test2 ~] # Tar xvf inotify-tools-3.13.tar.gz-C/usr/local/src/# unzip
[Root @ test2 ~] # Cd/usr/local/src/inotify-tools-3.13/# Switch directory
[Root @ test2 inotify-tools-3.13] #./configure -- prefix =/usr/local/inotify-tools; # compile and install
[Root @ test2 inotify-tools-3.13] # make; make install
Test
Use the inotifywait command to monitor the changes in the website directory/var/www/html, and then add and move files to the/var/www/html directory on another terminal to view the screen output results.
Common inotifywait parameters:
-E is used to specify the events to be monitored. These events include: create, move, delete, modify, and attrib.
-M indicates continuous monitoring
-R indicates recursion of the entire directory
-Q: simplified output information
[Root @ test2 ~] # Inotifywait-mrq-e create, move, delete, modify/var/www/html/
Then open another terminal and enter the/var/www/html directory to test the addition and deletion;
[Root @ test2 html] # mkdir test
[Root @ test2 html] # touch test.txt
[Root @ test2 html] # rm-rf test.txt
Create a triggered script to automatically synchronize data
Ssh password-free Login
[Root @ test2 ~] # Ssh-keygen # generate a key
[Root @ test2 ~] # Ssh-copy-id root@192.168.1.190 # publish a public key
Write scripts
[Root @ test2 ~] # Vim inotify. sh
#! /Bin/bash
SRC =/var/www/html
DST = root@192.168.1.190:/var/www/html
Inotifywait-mrq-e modify, delete, create, attrib $ {SRC} | while read D E F
Do
/Usr/bin/rsync-avz -- delete $ SRC $ DST
Done
[Root @ test2 ~] # Chmod + x inotify. sh # Add execution permission
Test the effect of automatic synchronization
[Root @ test2 ~] #./Inotify. sh # Run the script
Open another terminal to add, delete, and modify actions
[Root @ test2 ~] # Cd/var/www/html/
[Root @ test2 ~] # Touch bb.txt
Then log on to the 192.168.1.190 server to view the backup directory.