Incremental Backups
Strategies
| Strategy | Flag | How It Works |
|---|---|---|
| Incremental | --link-dest | Hard-links unchanged files, copies changed ones |
| Differential | --compare-dest | Copies only files that differ from reference |
| Full | (none) | Copies everything every time |
Commands
# Incremental snapshot (most common)
rsync -av --link-dest=/backup/latest \
/var/www/html/ /backup/$(date +%F)/
ln -sfn /backup/$(date +%F) /backup/latest
# Differential from last full backup
rsync -av --compare-dest=/backup/full-baseline/ \
/var/www/html/ /backup/diff-$(date +%F)/
# Full backup
rsync -av /var/www/html/ /backup/full-$(date +%F)/
# Verify backup matches source
rsync -avnc /var/www/html/ /backup/latest/
# Restore from snapshot
rsync -av /backup/2024-01-15/ /var/www/html/
# Cleanup old backups (keep 14 days)
find /backup/ -maxdepth 1 -type d -name "20*" -mtime +14 -exec rm -rf {} \;
Complete Backup Script
#!/bin/bash
set -euo pipefail
SRC="/var/www/html/"
BASE="/backup"
TODAY="$BASE/$(date +%F)"
rsync -av --link-dest="$BASE/latest" "$SRC" "$TODAY/"
ln -sfn "$TODAY" "$BASE/latest"
find "$BASE" -maxdepth 1 -type d -name "20*" -mtime +14 -exec rm -rf {} \;
tip
With --link-dest, every snapshot looks like a full backup but only uses extra space for changed files.