Inotify+rsync multiple Dir-sync

Source: Internet
Author: User
Tags inotify rsync

System: centos6.5

IP:10.19.21.241 is synchronized to the end

10.19.21.242 Synchronous End


I. Configuring Rsync (synchronous side)

1. Install rsync

# yum Install-y rsync

2. Write rsync configuration

# vi/etc/rsyncd.confuid = Rootgid = RootUse chroot = Nomax connections = 0 #最大连接数不限制log file =/var/log/rsyncd.logpi d file =/var/run/rsyncd.pidlock File =/var/run/rsyncd.lockhosts allow = * #允许所有主机 [Test1]path =/home/test1/comment = Rsync from Everyoneread only = Nolist = Noauth users = rsyncusersecrets File =/etc/rsync.pas[test2]path =/home/test2/c Omment = rsync from Everyoneread only = Nolist = Noauth users = rsyncusersecrets file =/etc/rsync.pas

3. Create a password file

# echo "rsyncuser:123456" >/etc/rsync.pas

4. Change Password file permissions

# chmod 600/etc/rsync.pas

5. Start the service

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

Two. Configuring the INotify (synced side)

1. Install rsync

# yum Install-y rsync

2. Installing INotify

# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz# Tar zxf inotify-tools-3.13.tar.gz# CD inotify-tools-3.13#./configure--preifx=/usr/local/inotify# make && make Install

3. Create a inotify_rsync_dirs.sh script

#!/bin/bashinotify_rsync_fun  () {    dir= ' echo $1 | awk -f ","   ' {print $1} '     ip= ' echo $1 | awk -f ', '   ' {print  $ '     des= ' echo $1 | awk -f ', '   ' {print $3} '      user= ' echo $1 | awk -f ', '   ' {print $4} '     /usr/ local/inotify/bin/inotifywait -mr --timefmt  '%d/%m/%y %h:%m '  --format  '%T % W %f '  -e modify,delete,create,attrib ${dir} |while read date time  DIR FILE        do                 FILECHAGE=${DIR}${FILE}                 /usr/bin/rsync -av -- progress --delete  --password-file=/etc/rsync.pas ${dir} ${user}@${ip}::${des} && echo  "At ${ time} on ${date}, file  $FILECHAGE  was backed up via rsync "  >> /var/log/rsyncd.log        done}count=2# localdir, host,rsync_module,user of rsync_module,sync1= "/home/test1/,10.19.21.242,test1,rsyncuser" sync2= "/Home /test2/,10.19.21.242,test2,rsyncuser "############################################################# #maini = 0while [ ${i} -lt ${count} ]do    i= ' expr ${i} + 1 '     tmp= ' sync ' $i     eval  sync=\$ $tmp      inotify_rsync_fun  "$sync"  &done

The relevant comments are as follows:

/usr/local/inotify/bin/inotifywait-mr

-M is to keep listening

-R is a recursive view directory

-Q is the print out event

-e modify,delete,create,attrib refers to "Listen create move Delete Write permission"

/usr/bin/rsync-av--progress--delete--password-file=/etc/rsync.pas

-A archive mode

-delete Delete redundant files

--password-file Password file

4. Add a password file

# echo 123456 >/etc/rsync.pas

5. Add Permissions

# chmod +x inotify_rsync_dirs.sh

6. Execution of commands

#./inotify_rsync_dirs.sh

Three. Testing

241 Create directory [[email protected] home]# mkdir/home/test1/dir{1,2,3}242 view [[email protected] home]# ll test1 total Usage 12drwxr-xr-x 2 Root root 4096 May 18:33 dir1drwxr-xr-x 2 root root 4096 May 18:33 dir2drwxr-xr-x 2 root root 4096 May 18:33 dir3 241 Create file [[email protected] home]# touch/home/test2/file{1,2,3}242 view [[email protected] home]# ll test2 total usage 0-rw-r--r--1 Root root 0 May 10:49 file1-rw-r--r--1 root root 0 May 10:49 file2-rw-r--r--1 root root 0 May 10:49 file3



This article is from the "Charlie_cen" blog, make sure to keep this source http://charlie928.blog.51cto.com/3741218/1651187

Inotify+rsync multiple Dir-sync

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.