💊 Quick Pill: SD Card Backup and Restore

🔄 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 blocks
  • status=progress: Shows progress during backup
  • if=/dev/sdb: Input file (your SD card - verify with lsblk!)
  • gzip: Compresses the output on-the-fly
  • date +%d%m%y: Adds date to filename (e.g., retropie_051025.gz)

📥 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 stdout
  • dd bs=1M: Writes in 1MB blocks
  • of=/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) and of= (output) parameters
  • Tested the backup file after creation

📊 Typical Use Cases

ScenarioCommand
Raspberry Pi backupsudo dd if=/dev/sdb | gzip > ~/rpi_backup.gz
RetroPie backupsudo dd if=/dev/sdb | gzip > ~/retropie.gz
Bootable USB backupsudo dd if=/dev/sdc | gzip > ~/bootusb.gz
Clone to larger SDsudo dd if=/dev/sdb of=/dev/sdc bs=1M status=progress

Quick, effective, compressed backups for your SD cards and bootable devices!