Mastering Rsync: From Basic Sync to Daemon and Real‑Time Inotify Automation
This guide explains Rsync's fundamentals, SSH‑based and daemon modes, detailed configuration options, practical command examples for local, LAN, and user‑specific synchronization, as well as real‑time file monitoring using inotify‑tools to achieve automated bidirectional replication.
Introduction
Rsync (remote synchronize) is a fast file‑synchronization utility for Unix‑like systems (Linux, Solaris, BSD) and Windows ports (e.g., cwRsync, Sync2NAS). It transfers only the differing blocks of files using the rsync algorithm, which reduces network traffic.
Principles
Originally created to replace rcp, rsync is now maintained at rsync.samba.org. Its configuration file rsync.conf follows a syntax similar to Samba’s main configuration. Rsync can operate over rsh / ssh or as a daemon listening on TCP port 873.
Key Features
Mirrors entire directory trees.
Preserves permissions, timestamps, symbolic and hard links.
Does not require special privileges for installation.
Optimized transfer flow for high efficiency.
Supports rsh, ssh, or direct socket connections.
Allows anonymous transfers.
SSH Mode
Local‑to‑Local Synchronization
# mkdir src
# touch src/{1,2,3,4}
# mkdir dest
# rsync -av src/ dest/ # sync contents of src into dest (exclude src itself)
# rsync -av src dest/ # sync src directory itself into dest
# rsync -avR src/ dest/ # same as above even with trailing slashLAN Synchronization
# mkdir src
# touch src/{a,b,c,d}
# mkdir dest
# rsync -av 172.16.22.12:/data/test/src/ dest/ # pull from remote to local
# 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 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 remoteUser‑Specific Synchronization
# useradd george
# passwd george
# mkdir /home/george/test
# touch /home/george/test/g{1,2,3,4}
# rsync -av -e "ssh -l george" src 172.16.22.12:/home/george # local → remote as george
# rsync -av -e "ssh -l george -p 22" 172.16.22.12:/home/george/test/g* dest/ # remote → localDaemon Mode
Service Startup
# yum install rsync xinetd # install packages
# /usr/bin/rsync --daemon # start as standalone daemon (heavy load)
# vim /etc/xinetd.d/rsync # edit xinetd config, set "disable = no"
# /etc/init.d/xinetd start # start xinetd (light load)
# chkconfig rsync on
# netstat -ntpl | grep 873 # verify daemon is listeningConfiguration (rsyncd.conf)
Global parameters address – IP address for standalone mode (ignored by xinetd). port – listening port (default 873, ignored by xinetd). motd file – message displayed to clients on connection. pid file – file where the daemon writes its PID. log file – path for the daemon’s own log file. syslog facility – syslog priority for messages. socket options – custom TCP options. lockfile – location of the lock file. timeout = 600 – inactivity timeout (seconds).
Module parameters path – required; directory that the module will serve. comment – description shown to clients. use chroot – default yes; chroot to path for security. uid / gid – user/group IDs used for file transfers (default nobody). max connections – maximum concurrent connections (0 = unlimited). lock file – lock file for max‑connections control. list – whether the module appears in the module list (default yes). read only / write only – control upload/download permissions. ignore errors – ignore I/O errors. ignore nonreadable – skip files without read permission. auth users – space‑ or comma‑separated list of allowed usernames (independent of system users). secrets file – file containing “user:password” pairs (mode 600). strict modes – enforce permissions on the secrets file. hosts allow / hosts deny – whitelist/blacklist of client IPs. transfer logging – enable logging of transfer operations. log format – format string for log entries (e.g., “%o %h [%a] %m (%u) %f %l”).
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 restartTesting
Client Setup
# yum -y install rsync
# mkdir /data/testCommon Rsync Options
-a– archive mode (equivalent to -rlptgoD). -r – recursive. -l – copy symlinks as symlinks. -i – list files on the server. -p – preserve permissions. -t – preserve modification times. -g – preserve group. -o – preserve owner. -D – preserve device files. -z – compress data during transfer. -P – show progress and keep partially transferred files. -v – verbose output.
Example Pull from Daemon
# rsync -avzP [email protected]::web1 /data/test/Synchronize with Deletion
# rsync -avzP --delete [email protected]::web1 /data/test/Exclude Pattern
# rsync -avzP --exclude "*3*" [email protected]::web1 /data/test/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 Job for Automatic Sync
# 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 -p /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 installEnvironment Variables
# echo "export PATH=/usr/local/inotify/bin:$PATH" >> /root/.bash_profile
# source /root/.bash_profile
# echo "/usr/local/inotify/lib" >> /etc/ld.so.conf
# ldconfig
# ln -s /usr/local/inotify/include /usr/include/inotifyOne‑Way Sync Script (monitor src and push to remote)
#!/bin/bash
INOTIFY=/usr/local/inotify/bin/inotifywait
SRC=/data/test/src
DEST=192.168.22.12:/data/test/dest
$INOTIFY -mrq -e modify,create,move,delete,attrib $SRC |
while read events; do
rsync -a --delete $SRC/ $DEST/
echo "$(date +'%F %T') event: $events" >> /tmp/rsync.log 2>&1
doneMake the script executable and run it in the background; add the command to /etc/rc.local for start‑up.
Bidirectional Sync
# rsync -a --delete /data/test/dest/ 192.168.22.11:/data/test/src/Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
