Skip to content

🛡️ Tutorial: System Backup Using the dd Command 🛡️

The dd command is a powerful tool in Unix-like operating systems for copying and converting data. This tutorial will guide you through understanding the dd command, creating scripts with common usages, exploring its options through tables, and performing various types of system backups.


📜 Table of Contents

  1. What is the dd Command?
  2. Common dd Command Usage Script
  3. Common dd Command Options
  4. Creating System Backups Using the dd Command
  5. 1. System Backup 🖥️
  6. 2. Application Backup 📦
  7. 3. Database Backup 💾
  8. 4. Filesystem Backup 📁
  9. 5. Disk Backup 💿

1. What is the dd Command? 🤔

The dd command stands for Data Duplicate and is a low-level utility used to copy and convert files at the byte level. It's ideal for tasks such as:

  • Creating exact copies of disks or partitions 🪟
  • Backing up and restoring boot sectors 🔄
  • Creating disk images 📸
  • Writing ISO images to USB drives 💾➡️💿

⚠️ Caution: Due to its powerful nature, improper use of dd can result in data loss. Always double-check commands before execution.


2. Common dd Command Usage Script 📄

Here's a bash script demonstrating common dd command usages for different backup scenarios. Ensure you have the necessary permissions and verify device names to prevent data loss.

#!/bin/bash

# 🖥️ Define variables
SOURCE_DEVICE="/dev/sdX"          # 🔄 Replace with your source device
DESTINATION_DEVICE="/dev/sdY"     # 🔄 Replace with your destination device
BACKUP_IMAGE="/path/to/backup.img"
ISO_FILE="/path/to/image.iso"
USB_DEVICE="/dev/sdZ"             # 🔄 Replace with your USB device

# 1. Create a Disk Image from a Device
create_disk_image() {
    echo "📂 Creating disk image from $SOURCE_DEVICE..."
    dd if="$SOURCE_DEVICE" of="$BACKUP_IMAGE" bs=4M status=progress
    echo "✅ Disk image created at $BACKUP_IMAGE."
}

# 2. Restore a Disk Image to a Device
restore_disk_image() {
    echo "🔄 Restoring disk image to $DESTINATION_DEVICE..."
    dd if="$BACKUP_IMAGE" of="$DESTINATION_DEVICE" bs=4M status=progress
    echo "✅ Disk image restored to $DESTINATION_DEVICE."
}

# 3. Create a Bootable USB from an ISO File
create_bootable_usb() {
    echo "💿 Writing ISO $ISO_FILE to USB device $USB_DEVICE..."
    dd if="$ISO_FILE" of="$USB_DEVICE" bs=4M status=progress oflag=sync
    echo "✅ Bootable USB created at $USB_DEVICE."
}

# 4. Backup the Master Boot Record (MBR)
backup_mbr() {
    echo "📝 Backing up MBR from $SOURCE_DEVICE..."
    dd if="$SOURCE_DEVICE" of="mbr_backup.bin" bs=512 count=1
    echo "✅ MBR backed up to mbr_backup.bin."
}

# 5. Restore the Master Boot Record (MBR)
restore_mbr() {
    echo "🔄 Restoring MBR to $SOURCE_DEVICE..."
    dd if="mbr_backup.bin" of="$SOURCE_DEVICE" bs=512 count=1
    echo "✅ MBR restored to $SOURCE_DEVICE."
}

# Execute Functions
create_disk_image
# restore_disk_image
# create_bootable_usb
# backup_mbr
# restore_mbr

🔍 Notes:

  • if: Input file/device
  • of: Output file/device
  • bs: Block size (e.g., 4M for 4 Megabytes)
  • status=progress: Shows progress during the operation
  • oflag=sync: Ensures all writes are synchronized

3. Common dd Command Options 📊

Option Description Example Usage
if= Input file/device if=/dev/sda
of= Output file/device of=/dev/sdb
bs= Block size (e.g., 1M for 1 Megabyte) bs=4M
count= Number of blocks to copy count=100
skip= Number of blocks to skip from the input skip=10
seek= Number of blocks to skip on the output seek=20
status= Level of information to display (progress for live) status=progress
conv= Conversion options (e.g., sync, noerror) conv=sync,noerror
oflag= Output flags (e.g., sync, direct) oflag=sync

🔧 Example Command:

dd if=/dev/sda of=/backup/sda_backup.img bs=4M status=progress

4. Creating System Backups Using the dd Command 🗄️

The dd command can be used to create various types of backups. Below are five different backup types:

1. System Backup 🖥️

Purpose: Create a complete backup of the entire system, including the operating system, applications, and data.

Command Example:

dd if=/dev/sda of=/backup/system_backup.img bs=4M status=progress

Explanation:

  • /dev/sda: The entire disk
  • /backup/system_backup.img: Destination image file

2. Application Backup 📦

Purpose: Backup specific applications by copying their installation directories and configuration files.

Script Example:

#!/bin/bash

# 📁 Define variables
APP_DIR="/opt/myapp"
BACKUP_DIR="/backup/apps/myapp_backup.img"

# 🔄 Create application backup
dd if="$APP_DIR" of="$BACKUP_DIR" bs=4M status=progress
echo "✅ Application backup completed."

🔍 Note: For file-based backups, tools like tar might be more efficient. Use dd primarily for block-level backups.

3. Database Backup 💾

Purpose: Create a backup of database files. Caution: Ensure the database is properly shut down or use database-specific backup tools to ensure data consistency.

Command Example:

# 🛑 Ensure the database service is stopped
sudo systemctl stop mysql

# 💾 Backup the database files
dd if=/var/lib/mysql of=/backup/mysql_backup.img bs=4M status=progress

# 🔄 Restart the database service
sudo systemctl start mysql
echo "✅ Database backup completed."

🔍 Note: It's recommended to use database-specific backup tools (e.g., mysqldump for MySQL) for consistency.

4. Filesystem Backup 📁

Purpose: Backup specific filesystems or partitions.

Command Example:

dd if=/dev/sda1 of=/backup/sda1_backup.img bs=4M status=progress

Explanation:

  • /dev/sda1: Specific partition
  • /backup/sda1_backup.img: Destination image file

5. Disk Backup 💿

Purpose: Create an exact copy of an entire disk, useful for cloning or migrating systems.

Command Example:

dd if=/dev/sda of=/dev/sdb bs=4M status=progress

Explanation:

  • /dev/sda: Source disk
  • /dev/sdb: Destination disk

⚠️ Warning: Ensure the destination disk (/dev/sdb) is correct to avoid overwriting important data.


🎯 Summary

  • dd Command: A versatile tool for low-level data copying and conversion.
  • Usage Scenarios: Disk cloning, system backups, boot sector backups, creating bootable USBs.
  • Caution: Always verify device names and command parameters to prevent data loss.
  • Alternative Tools: For file-based backups, consider tools like tar, rsync, or database-specific backup utilities for better efficiency and data integrity.