Rsync + inotify Real-time Data Synchronization

Source: Internet
Author: User
Tags inotify rsync ssh server
Remote Sync, remote Synchronization

1. Rsync command usage
Format: rsync [option ..] target directory of the source directory
Difference: synchronization and Replication
Copy: completely copy the source to the target
Synchronization: Incremental copy, only transmitted changed data

2. Local Synchronization
Rsync [option ..] local directory 1 local directory 2 # Synchronize Directory 1 to directory 2
Rsync [option...] local directory 1/local directory 2 # synchronize data under directory 1 to directory 2
-A: Archive mode, equivalent to-rlptgod
-V: displays details.
-Z: Enable compression/Decompression during transmission
-N: test the synchronization process without actual modification # used with-avz
-- Delete: Delete unnecessary documents in the target folder # used with-avz

3. Remote Synchronization
Rsync + SSH
-- Command
Rsync [email protected]: remote directory/# list SSH server resources
Rsync [option ..] [email protected]: remote directory local directory # downstream (download) not commonly used
Rsync [option...] local directory [email protected]: remote directory # commonly used for uploading
-A: Archive mode, equivalent to-rlptgod
-V: displays details.
-Z: Enable compression/Decompression during transmission
-N: test the synchronization process without actual modification # used with-avz
-- Delete: Delete unnecessary documents in the target folder # used with-avz
Tip: Add/after the directory to synchronize the data in the directory. If/is not added to the synchronization directory, the directory is synchronized.

4. Real-Time Synchronization
1) Deploy the public key and private key for SSH password-less Authentication
[[Email protected]/] # ssh-keygen # vm a generates a public key and a private key
[[Email protected]/] # ls/root/. Ssh # location where the public key and private key are generated
Id_rsa id_rsa.pub known_hosts
[[Email protected]/] # ssh-copy-ID [email protected] # pass the public key to the target host (in virtual machine B)
[[Email protected] ~] # Ls/root/. Ssh # view the Public Key uploaded to VM B
Authorized_keys # Public Key File Uploaded to the client

2) Real-time inotify Synchronization
-- Install inotify-tools in source code to monitor directory content changes
[[Email protected]/] # mkdir/myrpm
[[Email protected]/] # tar-XF/tools/inotify-tools-3.13.tar.gz-C/myrpm # Decompression
[[Email protected]/] # Yum-y install GCC make # Install the compiling environment GCC make
[[Email protected]/] # cd/myrpm/inotify-tools-3.13/
[[Email protected] inotify-tools-3.13] #./configure # generate MAKEFILE file
[[Email protected] inotify-tools-3.13] # Make # compile to binary
[[Email protected] inotify-tools-3.13] # make install # Install
[[Email protected] inotify-tools-3.13] # Which inotifywait # view command location
/Usr/local/bin/inotifywait

-- Inotifywait basic usage
Inotifywait [Option] destination folder
-M: continuous monitoring (do not exit after an event is captured)
-R: recursive monitoring, including subdirectories and files
-Q: Reduce screen output information
-QQ: no screen output information
-E: Specifies the monitored event categories such as modify move create Delete attrib.

3) Write shell scripts
-- Use the while loop to perform repeated checks for a single monitoring
Syntax:
While [condition]
Do
Loop execution
Done

-- Implementation
# Vim/jluocc/rsync. Sh
#/Bin/bash
While inotifywait-rqq/OPT
Do
Rsync-Az -- delete/opt/[email protected]:/OPT # synchronize data in the local opt directory to the/OPT directory of another host
Done
# Chmod + x/jluocc/rysnc. Sh # grant the script execution permission
#/Jluocc/rsync. Sh # Run the script to synchronize Real-time Data

4) Implement the data synchronization script at startup
# Vim/etc/rc. Local # Open the default file for system startup
/Jluocc/rsync. Sh # Write the absolute path of the script you just wrote in the file
# Chmod + x/etc/rc. Local # RC. Local does not have the execution permission by default. You must grant the execution permission.

5. Summary
Rsync: Data Synchronization
Inotifywait: monitors data changes under a specified directory, and returns corresponding results when changes occur.
Shell script: Implements real-time inotifywait monitoring. Once data changes, rsync data is synchronized to achieve real-time data synchronization.

Rsync + inotify Real-time Data Synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.