Operations 18 min read

Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Integration

This guide explains what Rsync is, its underlying algorithm, key features, and provides step‑by‑step commands for local, LAN, and user‑specific synchronization, daemon configuration, testing scenarios, and how to combine Rsync with inotify‑tools for real‑time bidirectional file replication.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Integration

Introduction

Rsync (remote synchronize) is a fast data‑sync utility for LAN/WAN that transfers only the differing parts of files using the rsync algorithm. It runs on most Unix‑like systems (Linux, Solaris, BSD) and has Windows ports such as cwRsync.

Principles

Originally created to replace rcp, rsync is maintained at rsync.samba.org. Its configuration syntax resembles rsync.conf from Samba. Rsync can operate over rsh, ssh, or in daemon mode listening on TCP port 873.

Key Features

Mirrors entire directory trees.

Preserves permissions, timestamps, symbolic and hard links.

Installs without special privileges.

Optimized transfer flow for high efficiency.

Supports rsh, ssh, or direct socket connections.

Allows anonymous transfers.

SSH Mode

Local synchronization

# mkdir src
# touch src/{1,2,3,4}
# mkdir dest
# rsync -av src/ dest/          # sync contents of src to dest (src itself excluded)
# rsync -av src dest/           # sync src directory itself to dest
# rsync -avR src/ dest/         # same effect with trailing slash

LAN synchronization

# mkdir src
# touch src/{a,b,c,d}
# mkdir dest
# rsync -av 172.16.22.12:/data/test/src/ dest/   # pull from remote (requires password)
# rsync -av src/ 172.16.22.12:/data/test/dest/   # push local files to remote
# rsync -av src 172.16.22.12:/data/test/dest/    # sync entire directory
# rm -rf src/d                                   # delete a file locally
# rsync -av --delete src/ 172.16.22.12:/data/test/dest/  # delete extraneous files on target

User‑specific LAN synchronization

# useradd george
# passwd george
# mkdir /home/george/test
# touch /home/george/test/g{1,2,3,4}
# rsync -av src -e ssh -l george 172.16.22.12:/home/george          # local → remote as user george
# rsync -av 172.16.22.12:/home/george/test/g* -e ssh -l george -p 22 dest/  # pull specific files

Daemon Mode

Service startup

Standalone daemon

# yum install rsync xinetd
# /usr/bin/rsync --daemon

Via xinetd (lightweight)

# yum install rsync xinetd
# vim /etc/xinetd.d/rsync   # set "disable = no"
# /etc/init.d/xinetd start
# chkconfig rsync on
# netstat -ntpl | grep 873   # verify listening on port 873

Configuration file ( rsyncd.conf )

The file uses the same syntax as Samba’s main configuration and contains global parameters, module definitions, and optional control/access settings.

Global parameters address: IP address for standalone mode (ignored by xinetd). port: Listening port (default 873). pid file, lockfile, log file: Paths for PID, lock, and log files. timeout: Connection timeout (default 600 s).

Module parameters path (required): Directory exported by the module. comment: Human‑readable description shown to clients.

Control parameters use chroot: Default yes; improves security but requires root. uid / gid: Run module as specified user/group (default nobody). max connections: Maximum concurrent connections (0 = unlimited). read only / write only: Access mode flags. auth users and secrets file: Define allowed usernames and password file (mode 600).

Access control hosts allow: Whitelist of client IPs (default *). hosts deny: Blacklist of client IPs.

Example server configuration

# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
   path = /data/test/src
   ignore errors = yes
   read only = no
   write only = no
   hosts allow = 192.168.22.12
   hosts deny = *
   list = yes
   auth users = web
   secrets file = /etc/web.passwd
# mkdir -p /data/test/src/george
# touch /data/test/src/{1,2,3}
# echo "web:123" > /etc/web.passwd
# chmod 600 /etc/web.passwd
# service xinetd restart

Testing

Client setup

# yum -y install rsync
# mkdir /data/test

Common options

Typical flags: -a (archive, equivalent to -rlptgoD), -v (verbose), -z (compress), -P (progress + partial), --delete (remove extraneous files), --exclude, etc.

# rsync -avzP [email protected]::web1 /data/test/                     # pull module
# rsync -avzP --delete [email protected]::web1 /data/test/          # exact mirror
# rsync -avzP --exclude="*3*" [email protected]::web1 /data/test/ # exclude files containing "3"

Password file

# echo "123" > /tmp/rsync.password
# chmod 600 /tmp/rsync.password
# rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/

Cron automation

# crontab -e
10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/

Real‑time synchronization with inotify‑tools

Installation

# yum install -y gcc
# mkdir /usr/local/inotify
# tar -xf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify/
# make && make install

Environment variables

# vim /root/.bash_profile
export PATH=/usr/local/inotify/bin/:$PATH
# source /root/.bash_profile
# echo '/usr/local/inotify/lib' >> /etc/ld.so.conf
# ldconfig
# ln -s /usr/local/inotify/include /usr/include/inotify

Common inotify options

-m

: Keep listening (default exits after one event). -r: Recursive monitoring. -e: Specify events (access, modify, attrib, open, delete, create, move). --exclude / --excludei: Regex filters.

Real‑time sync script

#!/bin/bash
/usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete,attrib /data/test/src \
  | while read events; do
    rsync -a --delete /data/test/src/ 192.168.22.12:/data/test/dest
    echo "$(date +'%F %T') Event: $events" >> /tmp/rsync.log 2>&1
done
chmod 755 /data/test/test.sh
/data/test/test.sh &
# Add to /etc/rc.local for boot‑time start

Running the same script on the destination machine with source/destination paths swapped enables bidirectional real‑time synchronization.

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.

Backupcrondaemonrsyncinotifyfile synchronization
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.