💊 Quick Pill: SD Card Backup and Restore
Info
Use case: Backup Raspberry Pi SD cards, bootable USB drives, or any block device with compression.
🔄 Backup SD Card
Create a compressed backup of your SD card:
sudo dd bs=1M status=progress if=/dev/sdb | gzip > ~/Desktop/retropie_`date +%d%m%y`.gz
What it does:
dd bs=1M: Reads the device in 1MB blocksstatus=progress: Shows progress during backupif=/dev/sdb: Input file (your SD card - verify withlsblk!)gzip: Compresses the output on-the-flydate +%d%m%y: Adds date to filename (e.g.,retropie_051025.gz)
Danger
⚠️ Critical: Always verify your device name with
lsblk before running dd. Using the wrong device will destroy your data!📥 Restore SD Card
Restore the compressed backup to an SD card:
sudo gzip -dc ~/Desktop/retropie_051025.gz | dd bs=1M of=/dev/sdb status=progress
What it does:
gzip -dc: Decompresses the backup to stdoutdd bs=1M: Writes in 1MB blocksof=/dev/sdb: Output file (target SD card)status=progress: Shows progress during restore
🔍 Verify Your Device
Before backup or restore, always check which device is your SD card:
# List all block devices
lsblk
# Example output:
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
# sda 8:0 0 500G 0 disk
# └─sda1 8:1 0 500G 0 part /
# sdb 8:16 1 32G 0 disk
# └─sdb1 8:17 1 32G 0 part /media/sdcard
In this example, sdb is the 32GB SD card.
💡 Pro Tips
Show estimated time remaining
# Install pv (pipe viewer) for better progress indication
sudo apt install pv
# Backup with pv
sudo dd bs=1M if=/dev/sdb | pv -s 32G | gzip > ~/Desktop/backup_`date +%d%m%y`.gz
# Restore with pv
sudo gzip -dc ~/Desktop/backup_051025.gz | pv | dd bs=1M of=/dev/sdb
Backup only used space (sparse image)
For ext4 filesystems, you can create a sparse image that skips empty blocks:
# Mount the partition first
sudo mount /dev/sdb1 /mnt
# Create sparse backup
sudo tar -czSpf ~/Desktop/sparse_backup_`date +%d%m%y`.tar.gz -C /mnt .
# Restore sparse backup
sudo tar -xzSpf ~/Desktop/sparse_backup_051025.tar.gz -C /mnt
Verify backup integrity
# Test if the compressed backup is valid
gzip -t ~/Desktop/retropie_051025.gz
# If successful, no output
# If corrupted, you'll see an error message
Backup to remote server via SSH
# Backup directly to remote server
sudo dd bs=1M if=/dev/sdb | gzip | ssh user@server 'cat > ~/backups/retropie_`date +%d%m%y`.gz'
# Restore from remote server
ssh user@server 'cat ~/backups/retropie_051025.gz' | sudo gzip -dc | dd bs=1M of=/dev/sdb
⚠️ Safety Checklist
Before running dd commands:
- Verified device name with
lsblk - SD card is unmounted (not the system disk!)
- Have enough disk space for the backup
- Double-checked
if=(input) andof=(output) parameters - Tested the backup file after creation
Danger
Remember:
dd is nicknamed “disk destroyer” for a reason. One typo can wipe your system drive. Always double-check!📊 Typical Use Cases
| Scenario | Command |
|---|---|
| Raspberry Pi backup | sudo dd if=/dev/sdb | gzip > ~/rpi_backup.gz |
| RetroPie backup | sudo dd if=/dev/sdb | gzip > ~/retropie.gz |
| Bootable USB backup | sudo dd if=/dev/sdc | gzip > ~/bootusb.gz |
| Clone to larger SD | sudo dd if=/dev/sdb of=/dev/sdc bs=1M status=progress |
Quick, effective, compressed backups for your SD cards and bootable devices!