Essential Linux Remote Data Sync with Rsync: A Complete Guide
This article explains how to use rsync for fast, incremental file synchronization over LAN/WAN, covering its algorithm, supported platforms, command‑line options, SSH and daemon modes, detailed configuration parameters, and real‑time syncing with inotify‑tools.
Introduction
Rsync (remote synchronize) is a data‑synchronization tool that quickly syncs files between multiple hosts over LAN or WAN. It uses the rsync algorithm to transfer only the differing parts of files, which makes subsequent synchronizations fast.
Rsync runs on most Unix‑like systems (Linux, Solaris, BSD) and has Windows ports such as cwRsync and Sync2NAS.
Principle
Originally created to replace rcp, rsync is maintained at rsync.samba.org and its configuration file format resembles Samba’s main config.
Rsync can operate via rsh or ssh, or run as a daemon listening on port 873. After the first full transfer, later runs perform incremental backups.
Key Features
Can mirror entire directory trees and file systems.
Preserves original permissions, timestamps, and symbolic/hard links.
Installs without special privileges.
Optimized workflow yields high transfer efficiency.
Supports rsh, ssh, or direct socket connections.
Allows anonymous transfers.
SSH Mode Examples
Local‑to‑Local Sync
# mkdir src</code>
<code># touch src/{1,2,3,4}</code>
<code># mkdir dest</code>
<code># rsync -av src/ dest/ # sync contents of src into dest (exclude src itself)</code>
<code># rsync -av src dest/ # sync src directory itself into dest</code>
<code># rsync -avR src/ dest/ # same effect even with trailing slashLAN Sync
# mkdir src</code>
<code># touch src/{a,b,c,d}</code>
<code># mkdir dest</code>
<code># rsync -av 172.16.22.12:/data/test/src/ dest/ # remote → local (enter root password)</code>
<code># rsync -av src/ 172.16.22.12:/data/test/dest/ # local → remote</code>
<code># rsync -av src 172.16.22.12:/data/test/dest/ # sync whole directory</code>
<code># rm -rf src/d # delete a file</code>
<code># rsync -av --delete src/ 172.16.22.12:/data/test/dest/ # delete extraneous files on targetLAN Sync with Specified User
# useradd george</code>
<code># passwd george</code>
<code># mkdir /home/george/test</code>
<code># touch /home/george/test/g{1,2,3,4}</code>
<code># rsync -av src '-e ssh -l george' 172.16.22.12:/home/george # local → remote as george</code>
<code># rsync -av 172.16.22.12:/home/george/test/g* '-e ssh -l george -p 22' dest/Daemon Mode
Service Startup
Heavy‑load server (stand‑alone daemon)
# yum install rsync xinetd</code>
<code># /usr/bin/rsync --daemonLight‑load server (xinetd)
# yum install rsync xinetd</code>
<code># vim /etc/xinetd.d/rsync # set "disable = no"</code>
<code># /etc/init.d/xinetd start</code>
<code># chkconfig rsync on</code>
<code># netstat -ntpl | grep 873 # verify serviceConfiguration Details (rsyncd.conf)
Global parameters (apply to all modules unless overridden):
address – IP address for stand‑alone 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 log.
syslog facility – syslog priority for messages.
socket options – custom TCP options.
lockfile – location of lock file.
timeout = 600 – inactivity timeout (seconds).
Module parameters (define which directories are exposed):
path – required, the directory to sync.
comment – description shown to clients.
Module control parameters (examples):
use chroot = true – chroot to path before transfer (requires root).
uid / gid – run as specified user/group (default nobody).
max connections – maximum concurrent connections (0 = unlimited).
read only / write only – control upload/download permissions.
ignore errors – skip I/O errors.
hosts allow / hosts deny – whitelist/blacklist client IPs.
auth users – list of allowed usernames (stored in secrets file).
Server Configuration Example
# vim /etc/rsyncd.confuid = 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] # module name
# mkdir /data/test/src</code>
<code># mkdir /data/test/src/george</code>
<code># touch /data/test/src/{1,2,3}</code>
<code># echo "web:123" > /etc/web.passwd</code>
<code># chmod 600 /etc/web.passwd</code>
<code># service xinetd restartTesting
Client Setup
# yum -y install rsync</code>
<code># mkdir /data/testBasic Sync Command
# rsync -avzP [email protected]::web1 /data/test/Option explanations:
-a – archive mode (equivalent to -rlptgoD).
-r – recursive.
-l – copy symlinks as symlinks.
-i – list files on the server.
-p – preserve permissions.
-t – preserve timestamps.
-g – preserve group.
-o – preserve owner.
-D – preserve device files.
-z – compress during transfer.
-P – show progress.
-v – verbose output.
# rsync -avzP --delete [email protected]::web1 /data/test/</code>
<code># rsync -avzP --delete /data/test/ [email protected]::web1</code>
<code># rsync -avzP --delete /data/test/ [email protected]::web1/george</code>
<code># rsync -ir --password-file=/tmp/rsync.password [email protected]::web1</code>
<code># rsync -avzP --exclude="*3*" --password-file=/tmp/rsync.password [email protected]::web1 /data/test/Password‑File Sync
# echo "123" > /tmp/rsync.password</code>
<code># chmod 600 /tmp/rsync.password</code>
<code># rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/Automated Client Sync (cron)
# crontab -e</code>
<code>10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::web1 /data/test/</code>
<code># crontab -lReal‑Time Sync with Inotify‑tools
Overview
Inotify‑tools provide C libraries and command‑line utilities ( inotifywait, inotifywatch) to monitor filesystem events on Linux.
Installation
# yum install -y gcc</code>
<code># mkdir /usr/local/inotify</code>
<code># tar -xf inotify-tools-3.14.tar.gz</code>
<code># cd inotify-tools-3.14</code>
<code># ./configure --prefix=/usr/local/inotify/</code>
<code># make && make installEnvironment Setup
# vim /root/.bash_profile</code>
<code>export PATH=/usr/local/inotify/bin/:$PATH</code>
<code># source /root/.bash_profile</code>
<code># echo '/usr/local/inotify/lib' >> /etc/ld.so.conf</code>
<code># ldconfig</code>
<code># ln -s /usr/local/inotify/include /usr/include/inotifyCommon Inotify Options
-m – keep listening (default exits after an event).
-r – recursive monitoring.
-q – quiet output.
-e – specify events (access, modify, attrib, open, delete, create, move, etc.).
--exclude, --excludei – pattern‑based exclusion.
--timefmt – format timestamps.
--format – custom output format.
One‑Way Real‑Time Sync Test
Monitor /data/test/src on the source machine and push changes to the destination via SSH.
# mkdir /data/test/dest/ # on destination</code>
<code># mkdir /data/test/src/ # on source</code>
<code># rsync -av --delete /data/test/src/ 192.168.22.12:/data/test/dest # test command</code>
<code># vim /data/test/test.sh</code>
<code>#!/bin/bash</code>
<code>/usr/local/inotify/bin/inotifywait -mrq -e modify,create,move,delete,attrib /data/test/src \</code>
<code> | while read events; do</code>
<code> rsync -a --delete /data/test/src/ 192.168.22.12:/data/test/dest</code>
<code> echo "$(date +'%F %T') event: $events" >> /tmp/rsync.log 2>&1</code>
<code>done</code>
<code># chmod 755 /data/test/test.sh</code>
<code># /data/test/test.sh &</code>
<code># echo '/data/test/test.sh &' >> /etc/rc.local # start on bootFor bidirectional sync, a similar script can be placed on the target machine swapping source and destination paths.
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.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
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.
