Introduction to Dump and Restore

The dump and restore utilities are traditional Unix backup tools that work at the filesystem level. They are particularly effective for backing up and restoring ext2/ext3/ext4 filesystems.

Key Features:

  • Incremental backups
  • Filesystem-level backup
  • Support for tape drives and other backup media
  • Ability to restore individual files or entire filesystems
Note: Dump works at the filesystem level, so it must be used on unmounted filesystems or during a system maintenance period for consistent backups.

Installation

On most Linux distributions, dump and restore utilities need to be installed separately.

For Ubuntu/Debian:

sudo apt update sudo apt install dump

For CentOS/RHEL/Fedora:

sudo yum install dump # or sudo dnf install dump

Verify Installation:

dump --version restore --version

Using the Dump Utility

The dump command is used to create backups of filesystems. It supports full and incremental backups.

Basic Syntax:

dump [options] [arguments] <filesystem>

Common Options:

Option Description
-0 to -9 Dump level (0 = full, 1-9 = incremental)
-f Specify output file or device
-u Update dump record file (/etc/dumpdates)
-j Compress backup (if supported)
-W Show what filesystems need to be dumped

Example Commands:

Full backup of /home to a file:

dump -0uf /backup/home-full.dump /home

Incremental backup (level 1):

dump -1uf /backup/home-incr1.dump /home

Backup to remote server using SSH:

dump -0uf - /home | ssh user@backup-server "cat > /backup/home.dump"
Tip: Always use the -u option to update the /etc/dumpdates file, which tracks when filesystems were last backed up.

Using the Restore Utility

The restore command is used to extract files from backups created with dump.

Basic Syntax:

restore [options] [arguments]

Common Options:

Option Description
-r Restore an entire filesystem
-x Extract specific files or directories
-t List contents of a backup
-f Specify backup file
-i Interactive mode

Example Commands:

List contents of a backup:

restore -tf /backup/home-full.dump

Restore a specific file:

restore -xf /backup/home-full.dump /home/user/document.txt

Interactive restore:

restore -if /backup/home-full.dump
Important: When restoring an entire filesystem, you must restore the full backup first, then incremental backups in order.

Backup Types and Strategies

Dump supports different backup levels for implementing efficient backup strategies.

Backup Levels:

  • Level 0: Full backup
  • Level 1-9: Incremental backups since last same or lower level backup

Example Backup Schedule:

Day Backup Level Type
Sunday 0 Full backup
Monday 1 Incremental since Sunday
Tuesday 2 Incremental since Monday
Wednesday 3 Incremental since Tuesday
Thursday 4 Incremental since Wednesday
Friday 5 Incremental since Thursday
Saturday 6 Incremental since Friday
Strategy: This approach minimizes storage requirements while allowing for point-in-time recovery.

Automating Backups

Automating your backup process ensures consistency and reduces the chance of human error.

Creating a Backup Script:

#!/bin/bash # Variables BACKUP_DIR="/backup" DATE=$(date +%Y-%m-%d) FILESYSTEM="/home" # Create backup directory if it doesn't exist mkdir -p $BACKUP_DIR # Determine backup level if [ ! -f $BACKUP_DIR/last_full_backup ]; then LEVEL=0 echo $DATE > $BACKUP_DIR/last_full_backup else LAST_FULL=$(cat $BACKUP_DIR/last_full_backup) DAYS_SINCE_FULL=$(( ( $(date -d "$DATE" +%s) - $(date -d "$LAST_FULL" +%s) ) / 86400 )) if [ $DAYS_SINCE_FULL -ge 7 ]; then LEVEL=0 echo $DATE > $BACKUP_DIR/last_full_backup else LEVEL=1 fi fi # Perform backup dump -${LEVEL}uf $BACKUP_DIR/${FILESYSTEM##*/}-level${LEVEL}-${DATE}.dump $FILESYSTEM

Scheduling with Cron:

Add to crontab (crontab -e):

# Run backup daily at 2 AM 0 2 * * * /path/to/backup-script.sh # Alternatively, directly in crontab 0 2 * * 0 dump -0uf /backup/home-full-$(date +\%Y-\%m-\%d).dump /home 0 2 * * 1-6 dump -1uf /backup/home-incr-$(date +\%Y-\%m-\%d).dump /home

Restoration Process

Proper restoration procedure is critical for recovering from data loss.

Restoring an Entire Filesystem:

  1. Boot from a live CD/USB if necessary
  2. Create and mount the target filesystem
  3. Change to the mount point directory
  4. Restore the full backup first
  5. Restore incremental backups in order
# Step 4: Restore full backup restore -rf /backup/home-full.dump # Step 5: Restore incremental backups in order restore -rf /backup/home-incr1.dump restore -rf /backup/home-incr2.dump # ... continue for all incremental backups

Restoring Individual Files:

# List contents to find the file restore -tf /backup/home-full.dump | grep filename # Extract the specific file restore -xf /backup/home-full.dump /path/to/filename
Warning: Practice restoration procedures regularly to ensure your backups are working correctly.

Best Practices

Follow these guidelines for a robust backup strategy.

General Guidelines:

  • Test backups regularly with restoration drills
  • Store backups offsite or on separate media
  • Use encryption for sensitive data
  • Monitor backup jobs for failures
  • Keep multiple generations of backups

Dump-Specific Tips:

  • Always use the -u flag to update /etc/dumpdates
  • Regularly verify backups with restore -t
  • Document your backup and restoration procedures
  • Consider filesystem consistency (run fsck before backup if possible)
Remember: A backup is only as good as your ability to restore from it.

Example Backup Scripts

Here are some practical scripts for implementing a backup system.

Complete Backup Script:

#!/bin/bash # Comprehensive backup script using dump # Configuration BACKUP_ROOT="/backup" LOG_FILE="/var/log/backup.log" FILESYSTEMS=("/home" "/var" "/etc") RETENTION_DAYS=30 # Create backup directory with date BACKUP_DATE=$(date +%Y-%m-%d) BACKUP_DIR="$BACKUP_ROOT/$BACKUP_DATE" mkdir -p "$BACKUP_DIR" # Log function log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } log "Starting backup process" # Check if dump is available if ! command -v dump &> /dev/null; then log "ERROR: dump command not found" exit 1 fi # Perform backup for each filesystem for FS in "${FILESYSTEMS[@]}"; do # Get filesystem name for backup file FS_NAME=$(echo "$FS" | tr -d '/') # Determine backup level if [ ! -f "$BACKUP_ROOT/last_full_$FS_NAME" ]; then LEVEL=0 echo "$BACKUP_DATE" > "$BACKUP_ROOT/last_full_$FS_NAME" else LAST_FULL=$(cat "$BACKUP_ROOT/last_full_$FS_NAME") DAYS_SINCE_FULL=$(( ( $(date -d "$BACKUP_DATE" +%s) - $(date -d "$LAST_FULL" +%s) ) / 86400 )) if [ $DAYS_SINCE_FULL -ge 7 ]; then LEVEL=0 echo "$BACKUP_DATE" > "$BACKUP_ROOT/last_full_$FS_NAME" else LEVEL=1 fi fi # Create backup BACKUP_FILE="$BACKUP_DIR/${FS_NAME}-level${LEVEL}.dump" log "Backing up $FS to $BACKUP_FILE (level $LEVEL)" if dump -${LEVEL}uf "$BACKUP_FILE" "$FS" 2>> "$LOG_FILE"; then log "Successfully backed up $FS" else log "ERROR: Failed to back up $FS" fi done # Clean up old backups find "$BACKUP_ROOT" -type d -name "*-*-*" -mtime +$RETENTION_DAYS -exec rm -rf {} \; 2>/dev/null log "Backup process completed"

Restoration Helper Script:

#!/bin/bash # Interactive restoration script echo "Available backups:" find /backup -name "*.dump" | sort | nl echo -n "Select backup to restore: " read OPTION BACKUP_FILE=$(find /backup -name "*.dump" | sort | sed -n "${OPTION}p") if [ -z "$BACKUP_FILE" ]; then echo "Invalid selection" exit 1 fi echo "Restoring from $BACKUP_FILE" echo "Choose mode:" echo "1) Interactive restore" echo "2) Extract specific files" echo "3) List contents" echo -n "Select option: " read MODE case $MODE in 1) restore -if "$BACKUP_FILE" ;; 2) echo -n "Enter path to extract: " read EXTRACT_PATH restore -xf "$BACKUP_FILE" "$EXTRACT_PATH" ;; 3) restore -tf "$BACKUP_FILE" ;; *) echo "Invalid option" exit 1 ;; esac