How to Back Up and Restore a Raspberry Pi SD Card Image Safely



Let's suppose you've finally got a Pi set up exactly how you want it -- services configured, dotfiles tweaked, that one cron job that took three tries to get right -- and now you want to try something risky on it. Before you do, clone the whole SD card to a file. If it goes wrong, you're back to a working state in minutes instead of redoing a weekend's work.

What you'll need

  • The SD card you want to back up, and either a spare computer with an SD card reader, or another Pi to run the backup from
  • Enough free disk space for a full image of the card (an 8GB card needs roughly 8GB free, before compression)
  • A second SD card, if you're restoring onto new media rather than the same one

dd backup and restore flow between two SD cards and an image file Working SD pi_backup .img New SD card dd if=/dev/sdX of=pi_backup.img dd if=pi_backup.img of=/dev/sdX

Finding the right device -- carefully

This is the step where people wipe the wrong disk. Before touching dd, list your drives and make absolutely sure which one is the SD card:

lsblk
# or, on macOS:
diskutil list

You're looking for the device as a whole (/dev/sdb or /dev/mmcblk0, for example), not one of its partitions (/dev/sdb1). Check the size against the card's actual capacity to confirm you've got the right one -- dd doesn't ask twice.

Backing up the card

Unmount the card's partitions first, then image the whole device to a file:

sudo umount /dev/sdb1 /dev/sdb2
sudo dd if=/dev/sdb of=~/pi_backup.img bs=4M status=progress conv=fsync

bs=4M speeds things up with larger reads, status=progress gives you a running total so you're not staring at a blank terminal, and conv=fsync makes sure everything's actually written to disk before the command returns. For an 8GB card over USB 2.0, expect this to take ten to twenty minutes.

The resulting .img file is the full size of the card, mostly empty space. If you want to shrink it down before archiving it, PiShrink trims the image to just the data actually on it:

sudo ./pishrink.sh pi_backup.img

Restoring it onto a new card

There are two ways to put that image back. With dd directly, same device-naming caution applies -- this time it's the target you need to get right, not the source:

sudo dd if=pi_backup.img of=/dev/sdb bs=4M status=progress conv=fsync

Or, if you'd rather not touch a terminal for this part, Raspberry Pi Imager accepts a local .img file as a custom image: open Imager, choose Operating System -> Use custom, point it at your pi_backup.img, pick the new SD card as the target, and write.

Checking it actually worked

Boot the new card in the Pi and confirm the hostname, installed services and your files are all exactly as they were:

hostname
systemctl list-units --type=service --state=running

If anything looks off, you've still got the original card untouched and the .img file to try again from. More detail on imaging and restoring cards is in the official Raspberry Pi getting-started documentation.

Leave a comment

Anonymous comments are published after review.