Rsync+inotify How to implement real-time synchronization of files

Source: Internet
Author: User
Tags chmod config inotify mkdir rsync

Environment Description: Existing two servers, to achieve real-time synchronization of MP3 files. You can use rsync to do synchronization, but not real time, in order to achieve real-time synchronization, we use INotify to monitor the changes that need to be synchronized, INotify is just a module in the kernel to monitor the changes in files, provides the API to monitor file changes, and to connect to this API, The Inotify-tools tool needs to be installed. Can achieve the new file, delete, modify, change attributes, etc., the function is very powerful. Now need a server as the content publishing side, to change the file, through rsync real-time synchronization to the content server.

==========================================

Content Server: 1.1.1.1 (Ubuntu 12.04 x86-64)

Publisher: 1.1.1.2 (Ubuntu 12.04 x86-64)

==========================================

First, configure Content Server 1.1.1.1

Install Rsync

# sudo apt-get install rsync

Provide rsync configuration files

# mkdir/etc/rsyncd/

# vim/etc/rsyncd/rsyncd.conf

# section 1:global settings 
port = 873
uid = root 
gid = root with 
chroot = yes 
Read Only = no 
max Connections = 7
pid file =/var/run/rsyncd.pid 
log file =/var/log/rsyncd.log 
hosts allow = * 
transfer lo gging = yes 
log format =%t%a%m%f%b 
syslog facility = local3 timeout 
=
      
# section 2:directory To is synced 
[MP3] 
path =/data/mp3 
list = False
Ignore errors = yes 
auth users = Syncuser 
s Ecrets file =/etc/rsyncd/rsyncd.pass

Provides the password file/etc/rsyncd/rsyncd.pass specified by Secrets file, which resembles the following:

Syncuser:iamok

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/

Where the colon before the user name, after the colon is the corresponding user's password. This file cannot be accessed by any other user, so you can modify it with the following command:

# chmod 600/etc/rsyncd/rsyncd.pass

Start the Rsync service

#/usr/bin/rsync--daemon--config=/etc/rsyncd/rsyncd.conf

Verify that the service is an account

root@node1:/etc/rsyncd# NETSTAT-NUTLP | grep:873

TCP 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2357/rsync

Join power-on Boot

# echo "/usr/bin/rsync--daemon--config=/etc/rsyncd/rsyncd.conf" >>/etc/rc.local

Second, configure the publishing server 1.1.1.2

Installing Rsync and Inotify-tools

# sudo apt-get install rsync inotify-tools

# mkdir/etc/rsyncd/

# Vim/etc/rsyncd/rsyncd.pass

Iamok

Set up automatic synchronization scripts, use Inotifywait Monitor directory, call rsync sync changed files when monitoring directory changes

# vim/var/tmp/auto_sync.sh

#!/bin/bash 
# 
desthost=1.1.1.1
desthostdir=/data/mp3/ 
srcdir=/data/mp3/ 
      
INOTIFYWAIT-MR-- Timefmt '%d/%m/%y%h:%m--format '%T%w%f '-e close_write,modify,delete,create,attrib $SRCDIR |  While read DATE time DIR FILE; Do
      
       filechange=${dir}${file} 
      
       rsync-avz--password-file=/etc/rsyncd/rsyncd.pass $FILECHANGE syncuser@${ Desthost}::mp3 &>/dev/null && \
       echo "at ${time} on ${date}, file $FILECHANGE is backed up via rsync" >>/var/log/mp3sync.log Done 

chmod u+x/var/tmp/auto_sync.sh

#/var/tmp/auto_sync.sh &

Boot Auto Run

# echo '/var/tmp/auto_sync.sh & ' >>/etc/rc.local

Summary: The above can achieve real-time synchronization of files, if the two ends of the file inconsistencies, it is recommended to use rsync to do a full synchronization, because the method written in this article, only to change the file synchronization, and not to scan the entire directory.

If you want to monitor multiple directory changes on the same machine, inotifywait is also supported, with multiple directories separated by spaces. Synchronization, but also can sync to more than one server, the script to write more than rsync on it.

This article comes from "Acridine a Pooh" blog, please be sure to keep this source http://gm100861.blog.51cto.com/1930562/916246

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.