Table of Contents
-
Understanding Backup and Recovery Basics
- What is a Backup?
- Key Objectives: RPO and RTO
- Backup vs. Recovery
-
Types of Backups: Choosing the Right Approach
- Full, Incremental, and Differential Backups
- File-Level vs. Block-Level Backups
- Local vs. Remote/Cloud Backups
-
- rsync: The Swiss Army Knife of File Sync
- tar: Archiving with Compression
- dd: Block-Level Disk Imaging
- BorgBackup: Deduplication & Encryption
- Timeshift: System Snapshots for Desktops/Servers
- Enterprise Tools: Amanda, Bacula, and Restic
-
Backup Planning: Laying the Foundation
- Data Assessment: What to Back Up?
- Defining RPO and RTO
- Storage Options: Local, Network, Cloud
- Security: Encryption and Access Control
-
Step-by-Step Implementation Guide
- Example 1: Home User Backup (rsync + Timeshift)
- Example 2: Server Backup (BorgBackup + Remote Storage)
-
Recovery Procedures: Restoring Your Data
- File-Level Restore (rsync/tar)
- System Snapshot Restore (Timeshift)
- Bare-Metal Recovery (dd)
- Troubleshooting Common Recovery Issues
-
- Scheduling Backups with cron
- Systemd Timers: An Alternative to cron
- Monitoring Backup Success: Alerts and Logs
-
Best Practices for Reliable Backups
- The 3-2-1 Rule
- Regularly Test Restores
- Encrypt Everything
- Minimize Downtime During Backups
1. Understanding Backup and Recovery Basics
What is a Backup?
A backup is a copy of data created to preserve it in case the original is lost, corrupted, or inaccessible. Backups can range from simple copies of files to full disk images, and they serve as the foundation for recovery.
Key Objectives: RPO and RTO
To design an effective strategy, define two critical metrics:
- Recovery Point Objective (RPO): The maximum amount of data you can afford to lose (e.g., “I can tolerate losing 1 hour of data”).
- Recovery Time Objective (RTO): The maximum time allowed to restore data and resume operations (e.g., “I need to be back online within 4 hours”).
Backup vs. Recovery
- Backup: The process of creating and storing copies of data.
- Recovery: The process of restoring data from backups to its original or alternate location. A backup is useless if recovery fails—always test restores!
2. Types of Backups: Choosing the Right Approach
Full, Incremental, and Differential Backups
- Full Backup: Copies all selected data.
- Pros: Fastest restore (single copy).
- Cons: Time/space-intensive; redundant for unchanged files.
- Incremental Backup: Copies only data changed since the last backup (full or incremental).
- Pros: Saves time/space.
- Cons: Restores require the full backup + all incrementals (slower).
- Differential Backup: Copies data changed since the last full backup.
- Pros: Faster than full, simpler restore than incremental (full + latest differential).
- Cons: Larger than incrementals over time.
File-Level vs. Block-Level
- File-Level: Backs up individual files/directories (e.g.,
rsync,tar). Ideal for selective restores. - Block-Level: Backs up raw disk blocks (e.g.,
dd, LVM snapshots). Captures the entire disk, including OS and hidden data.
Local vs. Remote/Cloud
- Local: External drives, NAS (Network-Attached Storage). Fast, but vulnerable to on-site disasters.
- Remote/Cloud: Offsite servers, AWS S3, Backblaze. Protects against physical loss but depends on network reliability.
3. Essential Linux Backup Tools
Linux offers a rich ecosystem of tools for every use case. Here are the most critical:
rsync: The Swiss Army Knife
rsync is a powerful utility for file-level backups, known for delta transfers (only syncs changed data).
- Features: Compression, encryption (via
ssh), checksum verification, and delete support (mirroring). - Example: Sync
/home/userto an external drive:rsync -av --delete /home/user/ /mnt/external_drive/backup/-a: Archive mode (preserves permissions, timestamps).-v: Verbose output.--delete: Removes files in the backup that no longer exist in the source.
tar: Archiving with Compression
tar (tape archive) creates compressed archives of files/directories.
- Example: Create a gzipped archive of
/var/www:tar -czf /backup/www_backup_$(date +%Y%m%d).tar.gz /var/www-c: Create archive.-z: Compress with gzip.-f: Specify output file.
dd: Block-Level Disk Imaging
dd copies raw disk blocks, making it ideal for bare-metal recovery.
- Example: Clone
/dev/sdato an external drive/dev/sdb:dd if=/dev/sda of=/dev/sdb bs=4M status=progress- Caution:
ddis destructive—double-checkif(input file) andof(output file)!
- Caution:
BorgBackup: Deduplication & Encryption
Borg (formerly Attic) is a deduplicating backup tool with built-in encryption. It stores only unique data blocks, saving space.
- Example Workflow:
- Initialize an encrypted repository:
borg init --encryption=repokey /mnt/backup/borg_repo - Create a backup:
borg create --compression zstd /mnt/backup/borg_repo::"backup-{now}" /home/user - List backups:
borg list /mnt/backup/borg_repo
- Initialize an encrypted repository:
Timeshift: System Snapshots
Timeshift is a GUI/CLI tool for creating restore points (like Windows System Restore) using rsync or Btrfs snapshots. Popular for desktops (Ubuntu, Fedora) and servers.
- Key Feature: Restores the OS, settings, and applications without affecting user files.
Enterprise Tools
- Amanda/Bacula: Open-source, enterprise-grade solutions for large-scale networks (supports tape libraries, deduplication).
- restic: Similar to Borg, with S3/cloud support and cross-platform compatibility.
4. Backup Planning: Laying the Foundation
Data Assessment
Identify critical data:
- System Data: OS, configurations (
/etc,/var). - User Data: Documents, photos, databases.
- Avoid: Temporary files (
/tmp), caches, and large non-essential data (e.g., downloaded ISOs).
Define RPO and RTO
- For a home server: RPO = 1 day, RTO = 2 hours.
- For a business database: RPO = 15 minutes, RTO = 30 minutes.
Storage Options
- Local: USB drives, internal disks (fast, but risk of theft/fire).
- Network: NAS (Synology, QNAP) or Samba shares (centralized, accessible over LAN).
- Cloud: AWS S3, Backblaze B2, or self-hosted (e.g., Nextcloud). Use
rcloneto sync to cloud providers.
Security
- Encryption: Encrypt backups at rest (Borg’s
repokey,gpgfortararchives) and in transit (usessh/sftpfor remote transfers). - Access Control: Restrict backup storage permissions (e.g.,
chmod 700 /backupto limit access).
5. Step-by-Step Implementation Guide
Example 1: Home User Backup (rsync + Timeshift)
Goal: Protect personal files and system settings.
1. Set Up Timeshift (System Snapshots)
- Install Timeshift:
sudo apt install timeshift # Debian/Ubuntu sudo dnf install timeshift # Fedora - Launch the GUI, select a storage device (e.g.,
/dev/sdb1), and configure:- Schedule: Weekly snapshots (retain 4).
- Include: System files (default).
- Exclude:
/home/user/Downloads(non-critical).
2. Backup Personal Files with rsync
- Automate daily sync of
/home/userto an external drive:rsync -av --delete /home/user/ /mnt/external_drive/user_backup/
Example 2: Server Backup (BorgBackup + Remote Storage)
Goal: Encrypt and back up a web server’s /var/www and /etc to a remote NAS.
1. Prepare the Remote Repository
- On the NAS, create a directory:
mkdir /mnt/nas/server_backups - Mount the NAS locally via
sshfs:sshfs user@nas_ip:/mnt/nas/server_backups /mnt/remote_backup
2. Initialize Borg Repo with Encryption
borg init --encryption=repokey /mnt/remote_backup/borg_repo
(Store the recovery key securely—without it, data is lost!)
3. Create a Backup Script
Save as backup_server.sh:
#!/bin/bash
REPO="/mnt/remote_backup/borg_repo"
BACKUP_NAME="server-backup-$(date +%Y%m%d)"
borg create --compression zstd \
$REPO::$BACKUP_NAME \
/var/www /etc \
--exclude /var/www/cache
borg prune --keep-daily=7 --keep-weekly=4 $REPO # Retain 7 daily, 4 weekly backups
- Make executable:
chmod +x backup_server.sh
6. Recovery Procedures: Restoring Your Data
File-Level Restore with rsync
To restore a deleted file from the rsync backup:
rsync -av /mnt/external_drive/user_backup/Documents/lost_file.txt /home/user/Documents/
Restore a tar Archive
Extract a single file from a tar archive:
tar -xzf /backup/www_backup_20240520.tar.gz var/www/index.html -C / # Extract to /var/www
System Restore with Timeshift
- Launch Timeshift GUI, select a snapshot, and click “Restore”.
- For CLI:
sudo timeshift --restore --snapshot '2024-05-20_12-00-00' --target /
Bare-Metal Recovery with dd
To restore a disk image to a new drive:
dd if=/backup/disk_image.img of=/dev/sda bs=4M status=progress
7. Automation and Monitoring
Scheduling with cron
Use cron to run backups automatically. Edit the crontab:
crontab -e
Add a daily Borg backup at 3 AM:
0 3 * * * /path/to/backup_server.sh >> /var/log/borg_backup.log 2>&1
Systemd Timers (Alternative to cron)
For more control (e.g., dependencies, retry logic), use systemd:
- Create a service file (
/etc/systemd/system/backup.service):[Unit] Description=Daily Borg Backup [Service] Type=oneshot ExecStart=/path/to/backup_server.sh - Create a timer file (
/etc/systemd/system/backup.timer):[Unit] Description=Run backup daily at 3 AM [Timer] OnCalendar=*-*-* 03:00:00 Persistent=true [Install] WantedBy=timers.target - Enable and start the timer:
sudo systemctl enable --now backup.timer
Monitoring
- Logs: Check
/var/log/borg_backup.logfor errors. - Alerts: Use
mailor tools likentfyto send notifications on failure:if ! borg create ...; then echo "Backup failed!" | mail -s "Backup Alert" [email protected] fi
8. Best Practices for Reliable Backups
The 3-2-1 Rule
- 3 Copies: Original + 2 backups.
- 2 Media Types: E.g., external drive + cloud.
- 1 Offsite Copy: Protect against fires/floods (use cloud or offsite server).
Regularly Test Restores
- Monthly: Restore a random file to verify backups are intact.
- Quarterly: Perform a full system restore test (use a VM for safety).
Encrypt Everything
- Even local backups: If your external drive is stolen, encryption prevents data leaks.
Minimize Downtime
- Use LVM snapshots to create consistent backups without stopping services:
lvcreate --size 10G --snapshot --name snap /dev/vg0/lv_root mount /dev/vg0/snap /mnt/snap rsync -av /mnt/snap/ /backup/ umount /mnt/snap lvremove -f /dev/vg0/snap
9. Conclusion
Implementing reliable backups on Linux requires a mix of tools, planning, and discipline. By combining the right backup types (full, incremental), secure storage (local + cloud), automation (cron/systemd), and regular testing, you can ensure data resilience against any disaster. Remember: the best backup is one that works when you need it most.