rsync Basics
Bash
rsync [] [] []Essential Options
| Option | Meaning |
|---|---|
-a | archive mode (preserve permissions, owner, timestamps, and symbolic links) |
-v | verbose (print progress) |
-z | compress during transfer (useful for remote transfers) |
-P | show progress and resume partial transfers |
--delete | delete files that exist only on the destination (mirroring) |
--dry-run | preview without actually running |
--exclude | exclude specific files/directories |
Local Synchronization
Bash
# Basic copy (watch the trailing slash)
rsync -av /src/dir/ /dst/dir/
# Without a trailing slash, the directory itself is copied
rsync -av /src/dir /dst/ # → /dst/dir/
rsync -av /src/dir/ /dst/dir/ # → /dst/dir/
# Mirroring (also delete files that disappeared from the destination)
rsync -av --delete /src/dir/ /dst/dir/Remote Server Synchronization
Bash
# Local → remote upload
rsync -avz -P /local/app/ user@server:/opt/app/
# Remote → local download (backup)
rsync -avz user@server:/var/log/ /backup/logs/
# Specify SSH port
rsync -avz -e "ssh -p 2222" /local/ user@server:/remote/
# Exclude specific files
rsync -av --exclude="*.log" --exclude=".git" /src/ /dst/Incremental Backup Script
Bash
#!/bin/bash
BACKUP_ROOT="/mnt/backup"
SOURCE="/var/www"
DATE=$(date +%Y-%m-%d)
LATEST="$BACKUP_ROOT/latest"
DEST="$BACKUP_ROOT/$DATE"
rsync -av --delete \
--link-dest="$LATEST" \
--exclude="*.tmp" \
--exclude="node_modules" \
"$SOURCE/" "$DEST/"
rm -f "$LATEST"
ln -s "$DEST" "$LATEST"
echo "Backup complete: $DEST"--link-dest performs incremental backups using hard links — unchanged files take no extra space.
Performance Tuning
Bash
# Adjust compression level (0–9, default 6)
rsync -av --compress-level=1 /src/ user@server:/dst/
# Checksum-based comparison (instead of timestamps)
rsync -avc /src/ /dst/Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗
Questions & answers
Ask a question about this guide. We'll review and reply.