Skip to main content

Backup and Recovery Strategies

In today's digital world, data is one of your most valuable assets. After learning about automation with cron jobs in the previous lesson, we'll now explore how to protect your system and data through effective backup and recovery strategies. This lesson will teach you practical methods to safeguard your Ubuntu system against data loss.

Learning Goals:

  • Understand different backup types and strategies
  • Learn to create automated backups using common tools
  • Practice data restoration techniques
  • Implement system recovery procedures

Understanding Backup Types

Full vs Incremental vs Differential Backups

Backups come in three main flavors, each with different trade-offs:

Full Backup: Contains all selected data

  • Pros: Complete restoration from single backup
  • Cons: Time-consuming and storage-intensive

Incremental Backup: Only backs up changed data since last backup

  • Pros: Fast and storage-efficient
  • Cons: Requires all incremental backups for restoration

Differential Backup: Backs up data changed since last full backup

  • Pros: Faster restoration than incremental
  • Cons: Larger than incremental backups
tip

For most home users, a weekly full backup combined with daily incremental backups provides the best balance of protection and efficiency.

Using rsync for File Backups

rsync is a powerful file synchronization tool that's perfect for creating backups. It only copies changed files, making it efficient for regular backups.

Basic rsync Backup

Create a simple backup
# Backup Documents folder to external drive
rsync -av ~/Documents /media/external-drive/backup/

Automated rsync Backup Script

backup-script.sh
#!/bin/bash
BACKUP_SOURCE="$HOME/Documents"
BACKUP_DEST="/media/external-drive/backups"
DATE=$(date +%Y-%m-%d)

# Create dated backup directory
mkdir -p "$BACKUP_DEST/$DATE"

# Perform backup
rsync -av --delete "$BACKUP_SOURCE/" "$BACKUP_DEST/$DATE/"

# Create symlink to latest backup
ln -sfn "$BACKUP_DEST/$DATE" "$BACKUP_DEST/latest"

echo "Backup completed: $DATE"

Tar Archives for System Backups

For complete system backups or archiving, tar (tape archive) is your go-to tool.

Creating Compressed System Backups

Backup important system directories
# Backup configuration files
sudo tar -czpf /backup/etc-backup-$(date +%Y%m%d).tar.gz /etc

# Backup home directories (excluding cache and temp files)
sudo tar -czpf /backup/home-backup-$(date +%Y%m%d).tar.gz \
--exclude=cache --exclude=tmp --exclude=*.tmp /home

Incremental Backups with Tar

incremental-backup.sh
#!/bin/bash
FULL_BACKUP="/backup/full-backup-$(date +%Y%m%d).tar.gz"
INCREMENTAL_LIST="/backup/incremental-list"

# Create full backup on first run
if [ ! -f "$INCREMENTAL_LIST" ]; then
sudo tar -czpf "$FULL_BACKUP" \
--listed-incremental="$INCREMENTAL_LIST" \
/home /etc
echo "Full backup created"
else
# Create incremental backup
INCREMENTAL_BACKUP="/backup/inc-backup-$(date +%Y%m%d).tar.gz"
sudo tar -czpf "$INCREMENTAL_BACKUP" \
--listed-incremental="$INCREMENTAL_LIST" \
/home /etc
echo "Incremental backup created"
fi

Automating Backups with Cron

Combine your backup scripts with cron for automated protection.

Setup automated daily backup
# Edit crontab
crontab -e

# Add this line for daily backups at 2 AM
0 2 * * * /home/$USER/scripts/daily-backup.sh
daily-backup.sh
#!/bin/bash
LOG_FILE="/var/log/backup.log"
BACKUP_DIR="/backup/daily"

# Rotate backups - keep only last 7 days
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +7 -delete

# Perform backup
tar -czf "$BACKUP_DIR/backup-$(date +%Y%m%d).tar.gz" \
/home /etc /var/www 2>> "$LOG_FILE"

# Log result
if [ $? -eq 0 ]; then
echo "$(date): Backup successful" >> "$LOG_FILE"
else
echo "$(date): Backup failed" >> "$LOG_FILE"
fi

Database Backups

If you're running databases, they require special handling for consistent backups.

MySQL database backup
#!/bin/bash
BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y%m%d)

# Backup all databases
mysqldump --all-databases | gzip > "$BACKUP_DIR/alldb-$DATE.sql.gz"

# Backup specific database
mysqldump myapp_db | gzip > "$BACKUP_DIR/myapp-$DATE.sql.gz"

Recovery Procedures

File Restoration from rsync Backup

Restore files from backup
# Restore entire directory
rsync -av /media/external-drive/backup/latest/Documents/ ~/Documents/

# Restore specific files
rsync -av /media/external-drive/backup/latest/Documents/important-file.txt ~/Documents/

Extracting Tar Archives

Restore from tar backup
# List contents without extracting
tar -tzf /backup/etc-backup-20231215.tar.gz

# Extract entire archive
sudo tar -xzf /backup/etc-backup-20231215.tar.gz -C /

# Extract specific files
sudo tar -xzf /backup/etc-backup-20231215.tar.gz -C / etc/hosts etc/nginx

Database Restoration

Restore MySQL database
# Restore from compressed backup
gunzip < /backup/mysql/myapp-20231215.sql.gz | mysql myapp_db

# Or restore interactively
mysql -u root -p myapp_db < /backup/mysql/myapp-20231215.sql
warning

Always test your backups! A backup that hasn't been verified is as good as no backup at all. Schedule regular recovery drills to ensure your backup process works correctly.

System Recovery Strategies

Boot Repair

Using Boot-Repair tool
# Install boot-repair
sudo apt update
sudo apt install boot-repair

# Run the tool (graphical interface will open)
sudo boot-repair

File System Repair

Check and repair filesystems
# Check filesystem (unmounted)
sudo fsck /dev/sda1

# Force check on next reboot
sudo touch /forcefsck
sudo reboot

Common Pitfalls

  • No backup testing: Backups that aren't regularly tested may fail when needed most
  • Single location storage: Storing backups only on the same physical machine defeats the purpose
  • Ignoring database consistency: Database backups without proper locking can create inconsistent backups
  • Forgetting configuration files: Applications often store critical data in /etc and hidden home directories
  • No retention policy: Unlimited backup growth can fill storage and make management difficult
  • Missing encryption: Sensitive data in backups should be encrypted, especially for offsite storage

Summary

Effective backup and recovery strategies are essential for system reliability. We've covered using rsync for file-level backups, tar for system archives, database-specific backup methods, and automation through cron jobs. Remember the 3-2-1 rule: keep 3 copies of your data, on 2 different media, with 1 copy offsite. Regular testing of your recovery procedures ensures you're prepared for actual data loss scenarios.

Quiz

Show quiz
  1. What is the main advantage of incremental backups over full backups?
  2. Which rsync option ensures deleted files are also removed from the backup destination?
  3. Why should database backups use tools like mysqldump instead of file-level copying?
  4. What command would you use to create a compressed tar archive of the /etc directory?
  5. How can you schedule a backup script to run every Sunday at 3 AM using cron?

Answers:

  1. Incremental backups are faster and use less storage space since they only copy changed files
  2. The --delete option synchronizes deletions from source to destination
  3. Database files can be in inconsistent state if copied while running; mysqldump creates consistent logical backups
  4. sudo tar -czf backup.tar.gz /etc
  5. 0 3 * * 0 /path/to/backup-script.sh (0 for Sunday, or you could use 0 3 * * Sun)