In Unix and Linux systems administration, copying large directory structures, maintaining offsite disaster recovery mirrors, and synchronizing code repositories across remote servers demands extreme network and I/O efficiency. Standard file copying tools like cp or scp lack resumption capabilities and blindly overwrite files in their entirety, retransmitting gigabytes of unchanged data during recurring sync cycles.
For high-performance data synchronisation, the standard utility is rsync (remote sync). Powered by the delta-transfer algorithm, rsync compares source and destination files, transmitting only the modified binary byte blocks across the network. Coupled with SSH encryption, bandwidth throttling controls, and hard-link deduplication flags, rsync serves as the backbone of automated backup solutions. This technical guide covers syntax rules, essential flags, trailing slash semantics, bandwidth rate limiting, and mirroring recipes.
Essential rsync Command Flags Reference
The standard syntax structure is: rsync [options] source destination. Master these fundamental flags:
| Flag | Long Name | Operational Role |
|---|---|---|
-a |
--archive |
Archive mode; equals -rlptgoD (recursively copies, preserves symlinks, permissions, timestamps, owner, and group). |
-v |
--verbose |
Increases console output verbosity, printing transferred filenames. |
-z |
--compress |
Compresses file data during transit, reducing network bandwidth over WAN connections. |
-h |
--human-readable |
Outputs transfer sizes in human-readable units (KBs, MBs, GBs). |
-P |
--progress --partial |
Displays live transfer speed/progress bar and keeps partially transferred files to allow resuming. |
--delete |
--delete |
Deletes extraneous files from the destination directory that no longer exist in the source. |
-n |
--dry-run |
Simulates the transfer without modifying any files on disk. |
The Critical Trailing Slash Rule (src/ vs. src)
The single most common source of confusion when using rsync is the presence or absence of a trailing forward slash on the source directory path:
- With Trailing Slash (
/data/src/): Copies the contents of the directory directly into the destination directory without creating an extra parent folder.rsync -avh /var/www/html/ /mnt/backup/Result: Files inside
htmlare placed directly inside/mnt/backup/. - Without Trailing Slash (
/data/src): Copies the directory itself into the destination, creating a nested directory namedsrcinside the destination.rsync -avh /var/www/html /mnt/backup/Result: Creates
/mnt/backup/html/containing the files.
Step 1: Performing Safe Dry-Run Testing (-n Flag)
Before executing destructive mirroring commands containing --delete, always perform a simulation run to verify what files will be copied or removed:
rsync -avhn --delete /data/production/ /backup/mirror/
Review the printed file list. If no unintended file deletions or misdirected directory structures appear, remove the -n flag to commit the synchronisation.
Step 2: Synchronizing Across Remote Servers via SSH (-e ssh)
By default, modern rsync implementations tunnel remote transfers securely over OpenSSH without requiring unencrypted rsync daemons:
Pushing Local Data to a Remote Server
rsync -avzP -e "ssh -p 2222" /local/data/ [email protected]:/remote/backups/
The -e "ssh -p 2222" parameter specifies a custom non-standard SSH port.
Pulling Remote Data to a Local Directory
rsync -avzP [email protected]:/var/log/ /local/logs/archive/
Step 3: Bandwidth Throttling (–bwlimit)
When running massive backups across metered internet connections or production links where unconstrained transfers could saturate business VoIP and web traffic, throttle transfer speeds:
# Limit transfer rate to 5 MB/s (specified in kilobytes per second: 5000 KB/s)
rsync -avzP --bwlimit=5000 /var/data/ [email protected]:/storage/
Step 4: Creating Time-Machine Style Incremental Backups (–link-dest)
Using the --link-dest parameter, rsync can create full-fidelity snapshots that consume disk space only for newly modified files, using hard links to point to unchanged files from a previous backup:
# Set timestamp variables
PREVIOUS_BACKUP="/mnt/backups/2026-10-22"
NEW_BACKUP="/mnt/backups/2026-10-23"
# Execute incremental backup with hard-linking to yesterday's snapshot
rsync -aP --link-dest="$PREVIOUS_BACKUP" /var/www/ "$NEW_BACKUP"
Because unchanged files are hard-linked to the previous inode, the new directory functions as a complete independent snapshot while consuming zero additional disk blocks for unmodified files.
Step 5: Excluding Specific Files and Directories (–exclude)
To omit caches, build artifacts, or temporary files from backups:
rsync -avh --exclude="node_modules" --exclude="*.tmp" --exclude=".git" /src/ /dest/
For extensive exclusion lists, create an exclude file (exclude.txt) and pass it via --exclude-from=exclude.txt.
Operational Best Practices
- Preserving Extended Attributes and ACLs: On modern Linux systems utilizing SELinux or custom POSIX access control lists, add the
-A(preserve ACLs) and-X(preserve extended attributes) flags to archive mode:rsync -aAXvh /etc/ /backup/etc/ - Atomic File Replacement: Use
--delay-updatesto ensure rsync holds newly transferred temporary files in a staging folder until the entire transfer finishes, preventing inconsistent application states if a network connection drops mid-sync.