Operations 18 min read

Mastering Rsync: From Basics to Real‑Time Sync with Inotify

This guide explains rsync's fundamentals, usage in ssh and daemon modes, detailed configuration options, and how to combine it with inotify‑tools for real‑time, bidirectional file synchronization on Linux systems, including practical command examples and scripting tips.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Rsync: From Basics to Real‑Time Sync with Inotify

Introduction

Rsync (remote synchronize) is a fast file‑synchronization utility that works over LAN or WAN. It transfers only the differences between source and destination files using the rsync algorithm, which makes it bandwidth‑efficient. Rsync runs on most Unix‑like systems (Linux, Solaris, BSD) and has Windows ports such as cwRsync.

Principles

Rsync was created to replace rcp and is maintained at rsync.samba.org. Its configuration file rsync.conf uses a syntax similar to Samba’s main configuration file. Rsync can operate over rsh, ssh, or as a daemon listening on TCP port 873.

Key Features

Can mirror entire directory trees.

Preserves permissions, timestamps, symbolic links and hard links.

Does not require special privileges to install.

Optimized transfer flow for high efficiency.

Supports rsh, ssh, or direct socket connections.

Allows anonymous transfers.

SSH Mode

Local‑to‑Local Sync

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

LAN Sync

# mkdir src
# touch src/{a,b,c,d}
# mkdir dest
# rsync -av 172.16.22.12:/data/test/src/ dest/   # pull from remote host
# rsync -av src 172.16.22.12:/data/test/dest/   # push to remote host
# rsync -av src 172.16.22.12:/data/test/dest/   # sync whole 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

Sync as Specific User

# 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 george
# rsync -av 172.16.22.12:/home/george/test/g* -e "ssh -l george -p 22" dest/   # remote → local

Daemon Mode

Service Startup

# yum install rsync xinetd               # install packages
# /usr/bin/rsync --daemon                # start standalone daemon (heavy load)
# vim /etc/xinetd.d/rsync                # xinetd configuration, set "disable = no"
# /etc/init.d/xinetd start                # start xinetd (light load)
# chkconfig rsync on                     # enable at boot
# netstat -ntpl | grep 873                # verify daemon is listening on port 873

Configuration Overview

Global Parameters

address

– IP address for standalone mode (ignored by xinetd). port – Listening port (default 873). motd file – Message displayed to clients on connection. pid file – File that stores the daemon PID. log file – Path for daemon log output. syslog facility – Syslog priority for daemon messages. socket options – Custom TCP socket options. lockfile – Path for lock file. timeout = 600 – Inactivity timeout in seconds.

Module Parameters

path

– Required; directory to be exported. comment – Description shown to clients. use chroot – Default yes; improves security but requires root. uid / gid – User/group for file transfers (default nobody). max connections – Maximum concurrent clients (0 = unlimited). read only / write only – Control upload/download permissions. hosts allow / hosts deny – IP‑based access control. auth users – List of usernames allowed to connect. secrets file – File containing "user:password" pairs. exclude / include – Pattern lists for filtering files. log format – Customizable log fields (e.g., %o, %h, %a, %m, %u, %f, %l, %p, %P, %t, %b, %c).

Server Configuration Example

# 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

Testing

Client Setup

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

Basic Sync Command

# rsync -avzP [email protected]::web1 /data/test/

Common options: -a = -rlptgoD (recursive, preserve links, perms, times, group, owner, devices). -z – compress data during transfer. -P – show progress and keep partially transferred files. --delete – remove files on destination that no longer exist on source. --exclude="*3*" – skip files whose names contain "3".

Password‑File Sync

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

Automated Sync via Cron

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

Real‑Time Sync with inotify‑tools

Installation

# yum install -y gcc
# mkdir /usr/local/inotify
# tar -xf inotify-tools-3.14.tar.gz -C /usr/local/inotify --strip-components=1
# cd /usr/local/inotify
# ./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 Options

-m

– keep listening (default exits after first event). -r – watch directories recursively. -e – specify events (modify,create,delete,move,attrib, etc.). --exclude – regex to exclude files. --format – custom output format.

One‑Way Real‑Time Sync Script (source host)

#!/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

Make the script executable, run it in the background, and add the command to /etc/rc.local for automatic start on boot.

Bidirectional Sync

# rsync -a --delete /data/test/dest/ 192.168.22.11:/data/test/src

Running the above on the destination machine mirrors changes back to the source, achieving two‑way 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.

LinuxdaemonrsyncShell scriptinginotifyfile 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.