Real-time Inotify + Rsync file synchronization in Linux

Source: Internet
Author: User
I. Preface * with the development of the company's business, a single web server cannot meet users' access needs. Multiple web servers need to be added to achieve balanced and high-availability access. In order to unify the data of various web servers at any time, the manual method is definitely not desirable. Considering the need for real-time synchronization, we can use the inotify + rsync solution for synchronization! Configuration environment: ce

I. Preface *

With the development of the company's business, a single web server cannot meet users' access needs. Therefore, multiple web servers need to be added to achieve balanced and high-availability access. In order to unify the data of various web servers at any time, the manual method is definitely not desirable. Considering the need for real-time synchronization, inotify + rsy is used.NcTo synchronize!

Configuration environment: centos 5.3 x86_64

RSyncVersion: rsync-0.3.7

Inotify version: inotify-tooLs-3.14

Server: 192.168.2.79 [Also rsync + inotify server]

Client: 192.168.2.100 [server to be synchronized to ensure data consistency]

First download the required software:

Http://blog.mgcrazy.com/download/rsync-3.0.7.tar.gz

Http://blog.mgcrazy.com/download/inotify-tools-3.14.tar.gz

Ii. formal installation:

Log on to the server 192.168.2.79 and download the file to:/usr/src:

Cd/Usr/src & wget http://blog.mgcrazy.com/download/{inotify-tools-3.14.tar.gz,rsync-3.0.7.tar.gz} & tar xzf rsync-3.0.7.tar.gz & cd rsync-3.0.7 &./configure? Prefix =/usr/local/rsync & make install & cd .. /& tar xzf inotify-tools-3.14.tar.gz & cd inotify-tools-3.14 &&. /configure & make install

Since then, rsync and inotify-tools have been installed! [Copy and execute directly]

3. Configure ssh authentication:

[Purpose: To avoid password input during real-time synchronization]

Run this command on the serverCommandGenerate public key: ssh-keygen one-way carriage return: as shown in Figure

Then, remotely copy the public key to the client server and rename it as authoriz.Ed_ Keys:

SCp-R/root/. ssh/Id_ Rsa. pub root@192.168.2.100:/root/. ssh/authorized_keys!

4. configure a Real-Time Synchronization script:

#!/bin/shsrc=/home/webapps/wwwdes=/home/webapps/ip=192.168.2.100 inotifywait -mrq ?timefmt ‘%d/%m/%y-%H:%M’ ?format ‘%T %w%f’ -e modify,delete,create,attrib ${src} | while read filedofor i in $ipdo/usr/local/rsync/bin/rsync -aP ?delete $src root@$ip:$desdonedone

Save as rsync. sh and grant the execution permission:ChmodO + x rsync. sh the path to be synchronized and Client ip address can be modified as needed: 192.168.2.100 is the Client ip address.

V. Test results:

The rest is to keep the script in the background: You can use: screen to execute the script in the background; then. /rsync. sh; crtl + a + d Exit, modify and create a file on the server, which is equivalent to an action that triggers inotify. If there is no action, it is not synchronized by default, then the system will automatically execute rsync to synchronize to the client. You can view the data on the client!

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.