32. Backup & Recovery
In enterprise infrastructure, hardware failures, ransomware, accidental deletions, and corrupted database migrations are inevitable. The quality of a SysAdmin or DevOps engineer is measured not by how rarely disasters strike, but by how reliably and quickly systems can be restored.
32.1 Core Disaster Recovery Concepts: RPO & RTO
Section titled “32.1 Core Disaster Recovery Concepts: RPO & RTO”Every backup strategy is defined by two fundamental business SLA metrics:
Incident Occurs ↓───[Last Backup]────────┼───────────────────[System Restored]───► Time ◄─── RPO ──────────► ◄────── RTO ───────► (Data Loss Window) (Downtime Duration)1. RPO (Recovery Point Objective)
Section titled “1. RPO (Recovery Point Objective)”- Definition: The maximum acceptable age of files/data that must be recovered for normal operations to resume if data loss occurs.
- In Plain English: “How many hours/minutes of data can the business afford to lose?”
- Example: If you run backups once a day at midnight and the database crashes at 11 PM, your potential data loss is 23 hours. If business SLA requires an RPO of 15 minutes, you must implement continuous write-ahead log (WAL) archiving or snapshotting.
2. RTO (Recovery Time Objective)
Section titled “2. RTO (Recovery Time Objective)”- Definition: The maximum acceptable duration of time that a system can remain offline before catastrophic business impact occurs.
- In Plain English: “How long can the system be down before we are back online?”
- Example: If you have a full backup in AWS Glacier that takes 6 hours to download and 2 hours to import, your recovery time is 8 hours. If the business SLA mandates an RTO of 30 minutes, tape or cold cloud storage alone is insufficient.
32.2 Backup Strategies: Full, Incremental & Differential
Section titled “32.2 Backup Strategies: Full, Incremental & Differential”Day 1 (Sun): [Full Backup: 100 GB]Day 2 (Mon): [Changed Data: 5 GB]Day 3 (Tue): [Changed Data: 4 GB]Day 4 (Wed): [Changed Data: 6 GB]1. Full Backup
Section titled “1. Full Backup”- How It Works: Copies every single file and database record from the source.
- Pros: Fastest and simplest restoration (only requires 1 archive to restore).
- Cons: Slowest to create; consumes the largest amount of storage and network bandwidth.
2. Incremental Backup
Section titled “2. Incremental Backup”- How It Works: Backs up only the files/blocks that have changed since the last backup (Full or Incremental).
- Pros: Extremely fast; minimal storage footprint.
- Cons: Slowest and most fragile restoration. Restoring Day 4 requires:
Full + Day 2 Incremental + Day 3 Incremental + Day 4 Incremental. If one incremental link in the chain is corrupted, the restore fails.
3. Differential Backup
Section titled “3. Differential Backup”- How It Works: Backs up all files/blocks that have changed since the last FULL backup.
- Pros: Fast and resilient restoration (only requires
Full Backup + Most Recent Differential Backup). - Cons: Takes progressively longer and consumes more storage each day until the next full backup runs.
32.3 Essential Linux Backup Tools
Section titled “32.3 Essential Linux Backup Tools”1. tar (Tape Archive)
Section titled “1. tar (Tape Archive)”The foundational Unix archiving utility. Often combined with gzip (-z) or zstd for high-ratio compression.
# Create a compressed archive preserving permissions (-p) and full pathssudo tar -czpvf /backups/system_backup_$(date +%F).tar.gz \ --exclude=/proc \ --exclude=/sys \ --exclude=/dev \ --exclude=/backups \ /etc /var/www /home
# Extract/restore archive to a target directory (-C)sudo tar -xzpvf /backups/system_backup_2026-10-03.tar.gz -C /restore_target/
# Test archive integrity without extractingtar -tzvf /backups/system_backup_2026-10-03.tar.gz > /dev/null && echo "Archive Healthy"2. rsync (Remote Sync)
Section titled “2. rsync (Remote Sync)”The premier tool for fast, incremental file-level backups over the local network or SSH.
# Mirror directory to a backup drive with hard-link incremental deduplicationrsync -avzP --delete \ --exclude="*.tmp" \ /var/www/ \ backupuser@backup-nas.internal:/storage/backups/web/Advanced Trick: Incremental Backups with Hard Links (--link-dest)
Section titled “Advanced Trick: Incremental Backups with Hard Links (--link-dest)”rsync can create hourly full-looking snapshots using zero extra disk space for unchanged files by utilizing filesystem hard links:
PREV_BACKUP="/backups/daily.0"NEW_BACKUP="/backups/daily.$(date +%Y%m%d)"
rsync -a --delete \ --link-dest="${PREV_BACKUP}" \ /var/www/ \ "${NEW_BACKUP}/"Unchanged files share the exact same Inodes as the previous backup, consuming zero duplicate disk blocks!
3. scp (Quick Point-to-Point Secure Copy)
Section titled “3. scp (Quick Point-to-Point Secure Copy)”Useful in simple scripts to move an archive to an offsite jump host:
scp -P 2222 /backups/db_dump.sql.gz backupuser@offsite-storage.example.com:/vault/4. rclone (Syncing to Cloud Object Storage)
Section titled “4. rclone (Syncing to Cloud Object Storage)”rclone is the “rsync for cloud storage” (AWS S3, Google Cloud Storage, Azure Blob, Backblaze B2, SFTP).
# Install rclonesudo apt install rclone -y # or dnf install rclone
# Configure cloud credentials interactivelyrclone config
# Sync local backup directory to an AWS S3 bucketrclone sync /backups/ my-s3-bucket:enterprise-backups/srv01/ --fast-list
# Copy with client-side encryption and bandwidth throttlingrclone copy /backups/ my-s3-bucket:secure-backups/ --bwlimit 10M32.4 The 3-2-1 Backup Strategy & Off-Site Storage
Section titled “32.4 The 3-2-1 Backup Strategy & Off-Site Storage”The golden standard of disaster recovery:
┌────────────────────────────────────────────────────────┐ │ 3-2-1 BACKUP RULE │ ├────────────────────────────────────────────────────────┤ │ 3 Copies of Data : Primary + Local Backup + Offsite│ │ 2 Different Media : e.g. Local NVMe/SSD + NAS/SAN │ │ 1 Offsite Location : Cloud Object Storage / Far VPC │ └────────────────────────────────────────────────────────┘- Why Offsite? Fire, flood, power surges, or compromised internal network credentials will destroy both the primary server and the local backup server if they reside in the same room or subnet.
32.5 Backup Rotation: Grandfather-Father-Son (GFS)
Section titled “32.5 Backup Rotation: Grandfather-Father-Son (GFS)”Never keep endless backups until the disk fills up. Implement an automated retention rotation:
- Son: Daily backups kept for 7 days.
- Father: Weekly backups kept for 4 weeks.
- Grandfather: Monthly backups kept for 12 months.
Automated Retention Cleanup in Bash:
Section titled “Automated Retention Cleanup in Bash:”# Keep only the last 7 daily archives, prune everything olderfind /backups/daily/ -type f -name "*.tar.gz" -mtime +7 -delete
# Keep only the last 30 daily database dumpsfind /backups/db/ -type f -name "*.sql.gz" -mtime +30 -delete32.6 Verification & Restore Testing
Section titled “32.6 Verification & Restore Testing”A backup that has never been restored is a failed backup. Automated corruption tests and disaster recovery drills are mandatory.
1. Checksums & Verification
Section titled “1. Checksums & Verification”Always generate a SHA256 checksum immediately after creating an archive:
# Generate checksumsha256sum backup_2026-10-03.tar.gz > backup_2026-10-03.tar.gz.sha256
# Verify checksum before attempting a restoresha256sum -c backup_2026-10-03.tar.gz.sha256# Expected output: backup_2026-10-03.tar.gz: OK2. Testing Restores (The “Game Day” Drill)
Section titled “2. Testing Restores (The “Game Day” Drill)”Never test restores on the live production server!
- Spin up an ephemeral testing VM or Docker container.
- Download the latest backup from off-site storage.
- Extract the files or import the database dump.
- Run automated smoke tests to confirm data consistency:
Terminal window # Test database restore in a temporary containerdocker run --name test-restore -e POSTGRES_PASSWORD=secret -d postgres:16docker exec -i test-restore psql -U postgres < /backups/db_dump.sqldocker exec -i test-restore psql -U postgres -c "SELECT COUNT(*) FROM users;"docker rm -f test-restore