Live sync using rsync+inotify-tools+ssh files in Linux

Source: Internet
Author: User
Tags inotify svn ssh rsync

In some server architectures, you will experience file synchronization issues, such as a site mirroring backup, synchronization of files to multiple CDN nodes, and so on. There are two main issues that need to be addressed

1. How to implement file synchronization problem? 2. When do I perform a sync operation?

File synchronization can be used Rsync+ssh, when to synchronize their own operation? Maybe some people would think of Cron, this is true, can be achieved, but it is a bit wasteful resources, because Cron does not know whether the file has been modified, but the periodic execution, if the file changes and then perform the synchronization operation, do not modify the implementation of that should be better, in fact, inotify can do, As long as your server is not antique level, support inotify, a few words useless words:


Inotify is a Linux feature that monitors file systems and issues relevant event warnings to specialized applications, such as delete, read, write, and uninstall operations. Inotify is highly responsive, simple to use, and much more efficient than the busy polling of cron tasks.

Suppose that there are two Web servers in a server architecture (IP 192.168.1.252 and 192.168.1.254), a code update publisher (IP 192.168.1.251), and the directory that needs to be synchronized is/data/www/, and the chart is as follows:


here are the steps to install

Install INotify on the code publisher (192.168.1.251), and execute the following command

Tar xzvf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14
./configure
Make
Make install
Cd..

Install rsync on all servers (the code publisher and the servers that need to be synchronized) as follows:

Tar zxvf rsync-3.0.9.tar.gz
CD rsync-3.0.9
./configure
Make
Make install

#------The above commands need to be executed on each server---------

Configure the SSH key trust between the code publisher and the server that needs to be synchronized, generating the public and private key on the code publisher
SSH-KEYGEN-T RSA

Direct three times Enter

Add the public key to each host Authorized_keys file that needs to be updated, and then execute on the code update server
Ssh-copy-id-i ~/.ssh/id_rsa.pub root@192.168.1.252
Ssh-copy-id-i ~/.ssh/id_rsa.pub root@192.168.1.254
If you are prompted to enter Yes
Enter password as prompted to return

Then two require a Web server (192.168.1.252 and 192.168.1.254) to reboot SSH
Service sshd Restart
Create inotify_rsync.sh script as root on the code publisher vi/root/inotify_rsync.sh input

#!/bin/sh
src=/data/www/#代码发布服务器目录
dst=/data/www/#目标服务器目录
ip= "192.168.1.252 192.168.1.254" #目标服务器IP, multiple separated by spaces
User=root
The/usr/local/bin/inotifywait-mrq--timefmt '%y-%m-%d%h:%m '--format '%T%w%f '%e '--exclude ' (. swp|. Swx|. SVN) "\
-E Create,move,delete,close_write,attrib $SRC | While read files
Todo
For I in $IP
Todo
/usr/local/bin/rsync-ahqzt--exclude Runtime--delete $SRC $USER @ $i: $DST
echo $files >>/tmp/rsync.log 2>&1
Done
Done

Related explanations:

The Uusr/local/bin/inotifywait-mrq--timefmt '%y-%m-%d%h:%m '--format '%T%w%f '%e '--exclude ' (. swp|. Swx|. SVN) "\
-E Create,move,delete,close_write,attrib $SRC | While read files

-M is to keep listening all the time
-R is a recursive view of the directory
-Q is printing out events
-e Create,move,delete,close_write,attrib refers to the "Listen for Create move delete Write permission" event

/usr/local/bin/rsync-ahqzt--exclude Runtime--delete $SRC $USER @ $i: $DST
-A archive mode
-H Save Hard Connection
-Q Stop non-error messages
-Z Transfer after compressing file data
-T Maintenance modification time
-delete deleted in redundant files
--exclude to exclude synchronized files

then give the script executable permissions

chmod +x/root/inotify_rsync.sh

Set up boot echo "/root/inotify_rsync.sh &" >>/etc/rc.local
Execute Script/root/inotify_rsync.sh &
Create a file in the/data/www/directory of the code update server to test the effect.

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.