Backup Using rsync
rsync is the most widely used backup tool on Linux. It copies files efficiently by transferring only the parts that have changed (delta transfer), preserves all file attributes, works both locally and over SSH, and is scriptable. Understanding rsync well enough to set up reliable automated backups is one of the most valuable skills for an Ubuntu administrator.
How rsync works
rsync process:
1. Source and destination compare file lists
2. For each file: if missing or different (size, time, checksum), transfer it
3. Transfer only changed blocks within files (delta algorithm)
4. Set permissions, timestamps, ownership to match source
5. Optionally delete files at destination that are gone from source
Result: destination is an exact copy of source.
On subsequent runs: only changed files are transferred (fast!).Basic rsync syntax
# Core options for reliable backups
# -a = archive mode: -rlptgoD (recursive, links, perms, times, group, owner, devices)
# -v = verbose output
# -z = compress data during transfer (useful over slow networks)
# --progress = show per-file progress
# --delete = delete files at destination that don't exist at source
# -h = human-readable sizes
# The trailing slash matters:
# Source with slash: copies CONTENTS of directory
rsync -av /home/irfan/ /backup/irfan/ # Copies files FROM irfan/ INTO backup/irfan/
# Source without slash: copies the directory ITSELF
rsync -av /home/irfan /backup/ # Creates backup/irfan/
⚠️ WARNING: The trailing slash on the source path is critical.
rsync -av /home/irfan/ /backup/irfan/syncs the contents of irfan/ into backup/irfan/.rsync -av /home/irfan /backup/irfan/creates/backup/irfan/irfan/. Test with--dry-runfirst to verify what will be copied where.
Local backups
# Basic local backup
sudo rsync -av --progress /home/ /backup/home/
# With deletion (mirror the source exactly)
sudo rsync -av --delete /home/ /backup/home/
# Always dry-run first to see what would change
sudo rsync -av --dry-run --delete /home/ /backup/home/
# Backup entire server (excluding virtual/special file systems)
sudo rsync -av --delete --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/tmp --exclude=/mnt --exclude=/media / /backup/full/
Remote backups over SSH
# Push backup from local to remote server
rsync -avz --delete /home/ backup-server:/backup/home/
# Pull backup from remote server to local
rsync -avz --delete backup-server:/etc/ /backup/remote-etc/
# Specify SSH port (if not 22)
rsync -avz -e "ssh -p 2222" /home/ backup-server:/backup/home/
# Use SSH key for automated/cron backups (no password prompt)
rsync -avz -e "ssh -i /root/.ssh/backup-key" /data/ backup-server:/backup/data/
# Limit bandwidth (in KB/s) — useful to avoid saturating the link
rsync -avz --bwlimit=10000 /data/ backup-server:/backup/data/
Incremental snapshot backups
Hard-link snapshots create daily backups that appear to be full copies but share unchanged files. This means you have many historical restore points without the storage cost of full duplicates.
# Incremental snapshot backup using --link-dest
# Each daily backup looks like a full copy but shares unchanged files via hard links
BACKUP_DIR="/backup/daily"
TODAY=$(date +%Y-%m-%d)
YESTERDAY=$(date -d "1 day ago" +%Y-%m-%d)
rsync -av --delete --link-dest="${BACKUP_DIR}/${YESTERDAY}" /home/ "${BACKUP_DIR}/${TODAY}/"
Result: daily snapshots that are cheap on storage
/backup/daily/
2024-06-01/ ← Full copy (first backup)
2024-06-02/ ← Looks full, but unchanged files are hard links
2024-06-03/ ← Looks full, but only changed files take extra space
2024-06-04/ ← Today's backup
Total space used: Much less than 4 full copies because of hard links
# Save this as a cron job for daily snapshots
sudo nano /etc/cron.daily/rsync-backup
Excluding files
# Exclude specific files or patterns
rsync -av --exclude="*.log" --exclude="*.tmp" /home/ /backup/home/
# Exclude a directory
rsync -av --exclude="cache/" --exclude=".git/" /var/www/ /backup/www/
# Use an exclude file
cat > /etc/rsync-excludes.txt << 'EOF'
*.log
*.tmp
*.swp
.git/
node_modules/
__pycache__/
.cache/
EOF
rsync -av --exclude-from=/etc/rsync-excludes.txt /home/ /backup/home/
Conclusion
rsync is the standard tool for file-level backups on Ubuntu. Use -av --delete for accurate mirrors. Use --link-dest for space-efficient daily snapshots with full restore points. Always use SSH key authentication for automated backups so scripts don’t require passwords. Always test new rsync commands with --dry-run before running for real. Combine rsync with cron for automated daily backups and monitor the cron output to catch failures early.
FAQ
Is Backup Using rsync important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support