Operations 9 min read

Achieve Real-Time File Sync with rsync + inotify: Step-by-Step Guide

Learn how to replace traditional backup methods with rsync and inotify for fast, secure, incremental file synchronization, covering rsync fundamentals, command options, server and client configuration, inotify monitoring scripts, and testing procedures to achieve real‑time data replication between Linux servers.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Achieve Real-Time File Sync with rsync + inotify: Step-by-Step Guide

Introduction

Compared with traditional cp or tar backup methods, rsync offers higher security, fast backup, and incremental backup support. It can meet data backup needs without strict real‑time requirements, but as file counts grow, rsync alone cannot satisfy real‑time sync, leading to the use of rsync + inotify.

rsync Overview

rsync (remote sync) is a fast incremental backup tool that supports local copying and remote synchronization via SSH or other rsync hosts.

Features

Can mirror an entire directory tree or file system.

High data transfer efficiency.

Secure transfer using SSH.

Supports anonymous transfer.

Operating Modes

Shell mode (local mode).

Remote shell mode using SSH.

List mode (only list source contents, -nv).

Daemon mode (rsync runs as a daemon receiving client requests).

Command Options

-n

: dry‑run, no actual synchronization. -v: verbose output. -q: quiet mode. -c: enable checksum verification. -r: recursive copy. -a: archive mode, preserve attributes. -p: preserve permissions. -t: preserve timestamps. -l: preserve symbolic links. -g: preserve group. -o: preserve owner. -D: preserve device files. -e ssh: use SSH as transport. -z: compress during transfer. --progress: show progress bar. --stats: display transfer statistics.

Note: When the source path ends with a slash, rsync copies the contents of the directory; without the slash, it copies the directory itself.

Real‑Time Sync with inotify

inotify monitors a directory for metadata changes and triggers rsync to achieve real‑time synchronization.

Deployment Example

Scenario: Two web servers (primary 172.16.10.100, secondary 172.16.10.212). Updates on the primary should automatically replicate to the secondary. The primary acts as rsync client, the secondary as rsync daemon.

Server‑Side Configuration

Install xinetd, enable rsync daemon, create /etc/rsyncd.conf with appropriate settings (uid/gid, no chroot, connection limits, authentication, module definition for web path, etc.). Create /etc/rsync.passwd with user scholar:scholar and set permissions to 600.

Client‑Side Configuration

Create authentication file and test synchronization using rsync daemon syntax.

Install inotify‑tools

Extract, configure, compile, and install inotify‑tools.

[root@scholar ~] # tar xf inotify-tools-3.14.tar.gz
[root@scholar ~] # cd inotify-tools-3.14
[root@scholar inotify-tools-3.14] # ./configure
[root@scholar inotify-tools-3.14] # make && make install

rsync Watch Script

#!/bin/bash
SRC=/web/
DEST=web
HOST=172.16.10.212
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $SRC |
while read files; do
  rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $SRC scholar@$HOST::$DEST
done
# inotifywait options:
# -m : keep listening
# -r : recursive
# -q : quiet output
# -e : events to monitor (modify, delete, create, attrib)
# --timefmt : time format
# --format : output format for changed files

Enable Service at Boot

Testing the Sync

Add a new site on the primary server and verify that it appears on the secondary server.

Synchronization succeeds, completing the rsync + inotify real‑time data sync setup.

Conclusion

The guide demonstrates a simple method to achieve real‑time file synchronization using rsync and inotify, and can be extended to multiple slave servers by adding more rsync daemons and adjusting the client script.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackupSystem Administrationrsyncinotifyreal-time-sync
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.