Complete Disk Backup and Restore with dd Command
The dd command is a powerful Linux utility for creating exact bit-by-bit copies of disks, partitions, or files. This guide covers how to safely create full disk backups and restore them when needed.
⚠️ Important Safety Warning
dd is dangerous! A single typo can permanently destroy your data. Always:
- Double-check your input (
if=) and output (of=) devices - Ensure you’re backing up the correct disk
- Never run dd on a mounted filesystem
- Keep backups on a separate physical drive
- Test your backups before you need them
The dd command is nicknamed “disk destroyer” for good reason—use it carefully!
Prerequisites
- Root or sudo access
- Sufficient disk space for backup (at least equal to source disk size)
- External drive or network storage for backup storage
- Basic understanding of Linux disk naming (/dev/sda, /dev/sdb, etc.)
Finding Your Disks
List All Disks
1 | # List all disks and partitions |
Understanding disk names:
/dev/sda- First SATA/SCSI disk (entire disk)/dev/sda1- First partition on first disk/dev/nvme0n1- First NVMe SSD/dev/mmcblk0- SD card/dev/sdb- Second disk (usually external USB)
Important: Back up the entire disk (e.g., /dev/sda), not just a partition (e.g., /dev/sda1). This ensures bootloader and partition table are included.
Identify Your Source Disk
1 | # Check mounted disks |
Creating a Disk Backup
Basic Disk to Image Backup
Create a complete disk image file:
1 | # Backup entire disk to image file |
Parameters explained:
if=/dev/sda- Input file (source disk to backup)of=full_disk_backup.img- Output file (backup image)bs=4M- Block size of 4 megabytes (faster than default)status=progress- Shows progress during copy
Example with real paths:
1 | # Backup to external USB drive |
Direct Disk to Disk Backup
Clone one disk directly to another (faster, no intermediate file):
1 | # Copy disk /dev/sda to disk /dev/sdb |
Warning: This will completely erase /dev/sdb! Triple-check your device names!
Compressed Backup (Saves Space)
Compress the backup on-the-fly using gzip or pigz:
1 | # Compress with gzip (slower, better compression) |
Compression comparison:
- No compression: Fastest, but huge file (entire disk size)
- gzip -9: Slowest, smallest file (~30-50% reduction)
- pigz -1: Good balance of speed and size
Backup Only Used Space (with dd_rescue)
For large disks with little data, use ddrescue to skip empty blocks:
1 | # Install ddrescue |
Optimizing dd Performance
Choosing Block Size
Block size affects speed significantly:
1 | # Too small (slow) |
Recommendation: Use bs=4M for most cases, bs=8M or bs=16M for very large disks.
Monitor Progress
If you forgot status=progress, monitor dd in another terminal:
1 | # Find dd process ID |
Speed Test
Benchmark your disk before backing up:
1 | # Write speed test (creates 1GB test file) |
Restoring from Backup
Restore Image to Disk
1 | # Restore uncompressed image |
Critical reminders:
/dev/sdbwill be completely overwritten- Unmount the target disk first
- For boot disks, connect only ONE boot disk at a time
- Verify device names with
lsblkbefore running
Restore Specific Partition
To restore just one partition:
1 | # Backup single partition |
Verification
Always verify your backups!
Compare Backup to Original
1 | # Calculate checksums |
Mount and Test Backup
1 | # Create loop device from backup |
Safety Best Practices
Before Creating Backup
1 | # 1. Unmount the source if possible |
Before Restoring Backup
1 | # 1. List all disks to confirm target |
Common Use Cases
Backup Before System Upgrade
1 | # Backup boot disk before major upgrade |
Clone Disk to Larger Disk
1 | # 1. Clone to larger disk |
Backup SD Card (Raspberry Pi)
1 | # Find SD card device (usually /dev/mmcblk0 or /dev/sdb) |
Create Bootable USB from ISO
1 | # Copy ISO to USB drive |
Troubleshooting
“No space left on device”
Problem: Destination doesn’t have enough space.
Solution:
1 | # Check available space |
“Device is busy”
Problem: Disk is mounted or in use.
Solution:
1 | # Check what's using the device |
“Operation not permitted”
Problem: Insufficient permissions.
Solution:
1 | # Run with sudo |
dd Seems Stuck
Problem: No progress visible.
Solution:
1 | # Check if dd is actually running |
Backup File Too Large
Problem: Image file is huge.
Solution:
1 | # Use compression |
Alternative Tools
While dd is powerful, consider these alternatives for specific needs:
For system backups:
rsync- Incremental backups, faster than dd for used space onlytar- Archive specific directoriesClonezilla- GUI tool, intelligent cloningtimeshift- System snapshots (like macOS Time Machine)
For disk cloning:
ddrescue- Recovers data from failing diskspartclone- Copies only used blocks (smaller backups)fsarchiver- Filesystem-level backup
For disk imaging:
partimage- Creates partition imagesg4u- Ghost for Unixredo rescue- Live CD for backups
Quick Reference
1 | # Basic backup |
Conclusion
The dd command is an essential tool for complete disk backups and disaster recovery. Key takeaways:
- Always verify device names before running dd
- Use compression to save space (
gziporpigz) - Add
status=progressto monitor progress - Test your backups before you need them
- Keep backups on separate physical drives
- Consider alternatives like
rsyncfor incremental backups
Remember: dd is powerful but dangerous. One wrong command can destroy your data permanently. Always double-check your commands before pressing Enter!