Rsync + inotify for Real-time Data Synchronization
Preface:
Rsync can implement triggered file synchronization, but it is triggered by the crontab daemon. the synchronized data is different from the actual data, while inotify can monitor various changes in the file system, when the file changes, rsync synchronization is triggered, which solves the problem of real-time data synchronization.
I. Basic Environment
System: CentOS2.6.32-220. el6.x86 _ 64
Software Package version: rsync-3.0.6-12.el6.x86_64
Inotify-tools-3.14
Download link: http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
Server: 172.16.32.204
Client (client1): 172.16.32.205
(Client2): 172.16.32.206
Ii. Client configuration (172.16.32.205, 172.163.32.206)
1. client1 172.16.32.205 Configuration
Install rsync
# Yum install-y rsync xinetd
Create the rsyncd. conf configuration file in the/etc/directory for editing.
# Vim/etc/rsyncd. conf
Uid = nobody
Gid = nobody
Use chroot = no
Max connections = 10
Strict modes = yes
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Log file =/var/log/rsyncd. log
[Lixuan]
Path =/data/lixuan/# local custom path
Comment = client file
Ignore errors
Read only = no
Write only = no
Hosts allow = 172.16.32.204 # Server IP Address
Hosts deny = *
List = false
Uid = root
Gid = root
Auth users = root
Secrets file =/etc/client1.pass # automatically synchronize password files
Create a/etc/client1.pass File
# Echo "root: 123456">/etc/client1.pass
# Chmod 600/etc/client1.pass
Start rsync
#/Etc/init. d/xinetd start
2. client2 172.16.32.206 Configuration
Install rsync
# Yum install-y rsync xinetd
Create the rsyncd. conf configuration file in the/etc/directory for editing.
# Vim/etc/rsyncd. conf
Uid = nobody
Gid = nobody
Use chroot = no
Max connections = 10
Strict modes = yes
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Log file =/var/log/rsyncd. log
[Lixuan]
Path =/data/lixuan/# local custom path
Comment = client file
Ignore errors
Read only = no
Write only = no
Hosts allow = 172.16.32.204 # Server IP Address
Hosts deny = *
List = false
Uid = root
Gid = root
Auth users = root
Secrets file =/etc/client2.pass # automatically synchronize password files
Save and exit!
Create a/etc/client2.pass File
# Echo "root: 123456">/etc/client2.pass
Chmod 600/etc/client2.pass
Start rsync
#/Etc/init. d/xinetd start
Iii. server configuration (172.16.32.204)
1. Install rsync
# Yum install-y rsync xinetd
Create the rsyncd. conf configuration file in the/etc/directory for editing.
# Vim/etc/rsyncd. conf
Uid = root
Gid = root
Use chroot = no
Max connections = 100
Log file =/var/log/rsyncd. log
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Secrets file =/etc/server. pass
[Lixuan]
Path =/data/lixuan/
Auth users = root
List = no
Read only = no
Secrets file =/etc/servers. pass
Comment = serverdirectory
Save and exit!
Create a/etc/server. pass File
# Echo "root: 123456">/etc/server. pass
# Chmod 600/etc/server. pass
Start rsync
#/Etc/init. d/xinetd start
2. Install inotify
Verify that the kernel supports inotify
# Uname-r
2.6.32-220. el6.x86 _ 64
# Ll/proc/sys/fs/inotify
Total 0
-Rw-r -- 1 root 0 Jun 11 max_queued_events # indicates the maximum number of events that can be queued in inotify instances when inotify_init is called.
-Rw-r -- 1 root 0 Jun 11 max_user_instances # indicates the maximum number of inotify instatnces that can be created by each real user ID
-Rw-r -- 1 root 0 Jun 11 max_user_watches # indicates the maximum number of directories that each inotify instatnces can monitor
Configure the server content publishing script
# Vim/data/sh/inotifyrsync. sh
#! /Bin/bash
Client1 = 172.16.32.205
Client2 = 172.16.32.206
Src =/data/lixuan/
Dst = lixuan
User = root
/Usr/local/bin/inotifywait-mrq -- timefmt '% d/% m/% y % H: % m' -- format' % T % w % f % e'-e close_write, modify, delete, create, attrib $ src | while read files
Do
/Usr/bin/rsync-vzrtopg -- delete -- progress -- password-file =/etc/client. pass $ src $ user @ $ client1: $ dst
/Usr/bin/rsync-vzrtopg -- delete -- progress -- password-file =/etc/client. pass $ src $ user @ $ client2: $ dst
Echo "$ {files} was rsynced">/tmp/rsync. log 2> & 1
Done
Save and exit!
Create a/etc/client. pass File
# Echo "123456">/etc/client. pass
# Chmod 600/etc/client. pass
Grant the script execution permission
# Chmod 755/data/sh/inotifyrsync. sh
Script Execution in the background
# Sh/data/sh/inotifyrsync. sh &
Add this script to the boot auto-start File
# Echo "/data/sh/inotifyrsync. sh &">/etc/rc. local
Iv. Test rsync + inotify Real-time Data Synchronization
Add or delete a directory or file to the/data/lixuan/directory of the server (172.16.32.204), and enter the client (172.16.32.205, 172.16.32.206) in the/data/lixuan/directory to check whether it is consistent with the server data in real time.