Real-Time File Synchronization Between Linux Servers Using Rsync and Inotify

Understanding Rsync and Inotify

Rsync is an efficient file synchronization tool that offers enhanced security, rapid backup capabilities, and support for incremental backups. Traditional backup methods like cp and tar can't match rsync's performance when dealing with large datasets. However, rsync has limitations with massive file collections as it scans all files before determining differences, which can be time-consuming. Additionally, rsync doesn't provide real-time monitoring, potentially causing data inconsistencies between source and destination systems.

Inotify, a kernel-level feature since Linux 2.6.13, provides a powerful, fine-grained, asynchronous mechanism for monitoring file system events. It can track file additions, deletions, modifications, and movements. The inotify-tools package offers a user-friendly interface to these kernel capabilities. By combining rsync with inotify, we can create a real-time synchronization solution that addresses rsync's limitation of not being able to detect file changes immediately.

System Configuration

For this implementation, we'll use two CentOS 6.5 servers:

Host IP Directory
Source Server 192.168.1.21 /tmp
Destination Server 192.168.1.22 /tmp

Source Server Configuration

1. Install Rsync and Inotify

On the source server, both rsync and inotify need to be installed:

# Install rsync and xinetd
yum install rsync xinetd

# Configure xinetd
vi /etc/xinetd.d/rsync
# Change disable = yes to disable = no

# Start xinetd service
service xinetd start

2. Create Authentication File

# Create password file
echo "secure-sync" >/etc/rsync.pass

# Set appropriate permissions
chmod 600 /etc/rsync.pass

3. Install Inotify Tools

# Download and install inotify tools
cd /usr/src
wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install

4. Create Synchronization Script

Create a script named auto_sync.sh in the /tmp directory:

#!/bin/bash
target_host=192.168.1.22
source_dir=/tmp/
module_name=data_sync
sync_user=backupadmin

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $source_dir |
while read files
do
    /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass $source_dir $sync_user@$target_host::$module_name
    echo "Synced: ${files}" >>/var/log/sync_activity.log 2>&1
done

Set execute permissions and run the script:

chmod 755 /tmp/auto_sync.sh
/tmp/auto_sync.sh &

# Add to startup
echo "/tmp/auto_sync.sh" >> /etc/rc.local

Destination Server Configuration

1. Install Rsync

The destination server only needs rsync:

# Install rsync and xinetd
yum install rsync xinetd

# Configure xinetd
vi /etc/xinetd.d/rsync
# Change disable = yes to disable = no

# Start xinetd service
service xinetd start

2. Create Authentication File

# Create password file with username and password
echo "backupadmin:secure-sync" > /etc/rsync.pass

# Set appropriate permissions
chmod 600 /etc/rsync.pass

3. Configure Rsync

Create the configuration file /etc/rsyncd.conf:

uid = root
gid = root
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

[data_sync]
path = /
comment = Data synchronization module
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.21
hosts deny = *
list = false
uid = root
gid = root
auth users = backupadmin
secrets file = /etc/rsync.pass

4. Start Rsync Service

service xinetd start
chkconfig xinetd on

Testing the Configuration

To verify the setup works correctly:

  1. On the source server, create a test file:
touch /tmp/test-sync
  1. Check if the file appears on the destination server:
ls /tmp/test-sync

If the file exists and both directories are identical, the real-time synchronization is functioning correctly.

Tags: Linux rsync inotify File Synchronization server administration

Posted on Fri, 22 May 2026 21:33:26 +0000 by kaos057