// Sleep better. Restore easier.
DATA IS CHEAP. DATA LOSS IS EXPENSIVE.
Every year, people lose irreplaceable photos, important documents, and years of work to hardware failures, theft, ransomware, or simple mistakes. The sad truth: most people don't back up until they've already lost data.
BACKUP ONCE, RESTORE CONFIDENTLY.
A proper backup strategy isn't about being paranoidβit's about being prepared. The 3-2-1 rule (3 copies, 2 media types, 1 offsite) has saved countless people from disaster. This guide will help you implement it.
AUTOMATE OR FORGET.
Manual backups don't work. You need automated systems that run reliably without your intervention. We'll cover tools and strategies that make backup as painless as possible.
12 lessons. Complete backup control.
The 3-2-1 rule. Types of backups. Full, incremental, differential.
BeginnerWhat to back up. Prioritizing data by importance.
BeginnerRsync basics. Hard links. Local backup strategies.
BeginnerDeduplication, compression, encryption. Borg basics and config.
IntermediateRepository model. Retention policies. Multiple backends.
IntermediateBackblaze, rsync.net, cloud sync. Getting data off-site.
IntermediateUnderstanding RAID levels. When RAID fails. RAID is not backup.
IntermediateFull system backup. Clonezilla, Timeshift, system restore.
IntermediateMySQL, PostgreSQL, MongoDB. Point-in-time recovery.
AdvancedVerifying backups work. Regular restore testing. Documentation.
AdvancedAlerting on failures. Verification scripts. Status reporting.
AdvancedPlanning for the worst. DR procedures. Business continuity.
AdvancedThe gold standard of backup strategy:
Categorize your data by importance:
# Find largest directories
du -sh /* 2>/dev/null | sort -hr | head -20
# Find large files
find /home -type f -size +100M -exec ls -lh {} \;
# Count files by type
find /home -type f | wc -l
# Basic rsync syntax rsync -av /source/ /destination/ # Key options: # -a: archive mode (preserves permissions, timestamps, etc.) # -v: verbose # -h: human-readable sizes # -n: dry run (don't actually copy) # --delete: delete files in dest that don't exist in source # --exclude: exclude patterns
Use rsync with --link-dest for efficient versioning:
#!/bin/bash
# daily-backup.sh
SOURCE="/home/"
DEST="/backup/"
LINK_DEST="$DEST/$(ls -td $DEST/backup-* | head -1)/"
rsync -avh --delete \
--link-dest="$LINK_DEST" \
"$SOURCE" "$DEST/backup-$(date +%Y-%m-%d)/"
This creates daily snapshots that only store changed files. Unchanged files are hard-linked.
#!/bin/bash
# backup-home.sh
BACKUP_DIR="/media/backup/backup-$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
rsync -av \
--exclude='.cache' \
--exclude='.local/share/Trash' \
--delete \
/home/ "$BACKUP_DIR/"
echo "Backup complete: $BACKUP_DIR"
# Debian/Ubuntu sudo apt install borgbackup # macOS brew install borgbackup
# Initialize repository (with encryption)
borg init --encryption=repokey /backup/borg-repo
# Create backup
borg create /backup/borg-repo::backup-{now} /home
# List backups
borg list /backup/borg-repo
# Mount backup
borg mount /backup/borg-repo::backup-2024-01-15 /mnt/restore
# Restore
borg extract /backup/borg-repo::backup-2024-01-15 --destination /restore
#!/bin/bash
# borg-backup.sh
export BORG_PASSPHRASE='your-passphrase'
borg create \
--compression lz4 \
--exclude '*/.cache' \
/backup/borg-repo::daily-{now} \
/home
# Prune old backups
borg prune \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=12 \
/backup/borg-repo
# Install sudo apt install restic # Initialize repository restic init --repo /backup/restic # Or with password file restic init --repo /backup/restic --password-file /etc/restic/password
# Backup restic -r /backup/restic backup /home # List snapshots restic -r /backup/restic snapshots # Restore latest snapshot restic -r /backup/restic restore latest --target /restore # Check repository integrity restic -r /backup/restic check # Remove old snapshots restic -r /backup/restic forget --keep-daily 7 --keep-weekly 4 --prune
# Backblaze B2 export B2_ACCOUNT_ID=your-id export B2_ACCOUNT_KEY=your-key restic -r b2:bucket-name:/path backup /home # S3 restic -r s3:https://s3.amazonaws.com/bucket-name backup /home
Local backups protect against:
Offsite backups additionally protect against:
# rsync to remote (over SSH) rsync -avz -e ssh /local/backup/ user@remote:/backup/ # Use rclone for cloud sync restic -r s3:backblaze:bucket-name backup /home # rclone sync rclone sync /local/ encrypted-remote:backup -v
# Clonezilla live or installed # Can clone: # - Disk to disk # - Partition to partition # - Disk to image # - Image to disk # Command line example: sudo clonezilla -c -e1 -j2 -md -r -fsck -p choose savedisk example_image sda
# Install sudo apt install timeshift # Create snapshot via CLI sudo timeshift --create --comments "Before upgrade" --tags D # List snapshots sudo timeshift --list # Restore sudo timeshift --restore --snapshot '2024-01-15_10-00'
# Single database mysqldump -u root -p dbname > dbname.sql # All databases mysqldump -u root -p --all-databases > alldbs.sql # With compression mysqldump -u root -p dbname | gzip > dbname.sql.gz # Restore mysql -u root -p dbname < dbname.sql
# Single database pg_dump -U postgres dbname > dbname.sql # All databases pg_dumpall -U postgres > all_databases.sql # Custom format (for point-in-time recovery) pg_dump -U postgres -Fc dbname > dbname.dump # Restore pg_restore -U postgres -d dbname dbname.dump
# Single database mongodump --db dbname --out /backup/ # With authentication mongodump --db dbname --out /backup/ -u user -p # Restore mongorestore /backup/
A backup you can't restore is useless. Test regularly:
#!/bin/bash # verify-backup.sh # Test Borg backup borg check /backup/borg-repo # Test Restic backup restic -r /backup/restic check # Test file backup rsync -avn --checksum /source/ /backup/test-restore/
# Check last backup time ls -la /backup/ | tail -5 # Check backup size change du -sh /backup/* # Log backup completion echo "$(date): Backup complete" >> /var/log/backup.log
#!/bin/bash
# backup-with-alert.sh
LOGFILE=/var/log/backup.log
if borg create /backup/repo::backup-{now} /data 2>> $LOGFILE; then
echo "$(date): Backup succeeded" | tee -a $LOGFILE
else
echo "$(date): Backup FAILED" | tee -a $LOGFILE
# Send alert
curl -s -X POST https://notify.example.com/webhook -d "message=Backup failed!"
fi
Ask yourself:
Restore from latest backup. Time: Minutes.
Replace drive, restore from backup. Time: Hours.
Disconnect machine, clean install, restore from clean backup. Time: Days.
Restore from offsite cloud backup to new hardware. Time: Days to weeks.
You've completed the Backup Mastery guide. You now know:
Remember: