How to Create a Backup Image of Your Raspberry Pi

Ben
Ben
@benjislab

Safeguarding the time and effort invested in your Raspberry Pi projects is essential. Whether you’re hosting a server, running home automation, or experimenting with coding, creating a backup image of your Raspberry Pi’s SD card can save you from starting over in the event of corruption, errors, or failed updates.

In this guide, we’ll cover both common backup methods:

  1. Using a separate computer (Windows, macOS, or Linux) to create a backup image.
  2. Creating a backup image directly on the Raspberry Pi itself using an external USB drive—ideal if you don’t have immediate access to another machine.

We’ll also cover how to verify your backup’s integrity and prepare your Raspberry Pi before creating the image.

Why Create a Backup Image?

A backup image provides a “snapshot” of your entire system—operating system, installed software, configurations, and files. With this snapshot, you can quickly restore your setup if something breaks, saving you valuable time.

Prepare Your Raspberry Pi Before Backing Up

Before creating a backup, ensure your Raspberry Pi is in the best possible state:

  1. Update Raspberry Pi OS and Packages:
    Keeping your Pi up-to-date ensures you’re backing up the latest security patches and stable packages.
sudo apt update && sudo apt full-upgrade -y
  1. Clean Up Unnecessary Files:
    Remove temporary files, old caches, or unused packages to reduce the backup image size and ensure a clean snapshot.
sudo apt autoremove -y
sudo apt clean
  1. Safely Shut Down Before Removing the SD Card (if using an external machine):
sudo shutdown now

Wait until the Pi is fully shut down before removing the SD card.

Equipment Needed

  • Raspberry Pi with a microSD card in use.
  • Another computer with an SD card reader (for the external backup method) or
  • An external USB drive connected directly to your Raspberry Pi (for local, on-Pi backup).
  • Imaging software: Win32DiskImager, dd command, ApplePi-Baker, or scripts like pishrink.sh.

Option 1: Backing Up Using a Separate Computer

Recommended Tools

  • Win32DiskImager (Windows): Reliable for reading SD cards to image files.
  • dd command (Linux/macOS): A powerful command-line tool for creating raw disk images.
  • ApplePi-Baker (macOS): A user-friendly tool designed for Raspberry Pi backups.

Steps

  1. Power Off and Remove the SD Card:
    After shutting down your Pi, remove the SD card and insert it into your computer’s SD card reader.

  2. Identify the SD Card on Your Computer:

    • On Windows, note the drive letter (e.g., E:).
    • On macOS or Linux, use diskutil list (macOS) or sudo fdisk -l (Linux) to find the device path (e.g., /dev/disk2 or /dev/sdb).
  3. Using Win32DiskImager (Windows):

    • Install and launch Win32DiskImager.
    • Select the SD card drive letter and specify a destination for the .img file.
    • Click Read to create the backup image.
  4. Using dd (Linux/macOS):
    For example, on Linux:

sudo dd if=/dev/sdb of=~/pi_backup.img bs=1M

Replace /dev/sdb with your actual SD card device and adjust the output path as desired.

  1. Using ApplePi-Baker (macOS):

    • Launch ApplePi-Baker.
    • Select your SD card and choose a destination for the backup image.
    • Click Backup to create the image.

Option 2: Creating a Backup Image Directly on the Raspberry Pi

If you don’t have another computer handy, you can create a backup image on the Pi itself. For this, you’ll need:

  • An External USB Drive: Large enough to store the image of your SD card.
  • Tools like dd and pishrink.sh: pishrink.sh can compress and reduce the size of your backup image, making storage and transfers easier.

Steps

  1. Connect the External USB Drive to Your Raspberry Pi:
    Plug a USB drive into the Pi’s USB port. Wait a moment for the system to recognize it.

  2. Identify the USB Drive and Mount Point:

lsblk

Look for your USB drive (e.g., /dev/sda1) and note its mount point. Often, it might be in /media/pi/[VOLUME_NAME].

  1. Create a Raw Backup Image Using dd:
    First, determine which device is your Pi’s SD card. Typically, it’s /dev/mmcblk0. To create an image of the entire SD card on your USB drive:
sudo dd if=/dev/mmcblk0 of=/media/pi/[VOLUME_NAME]/pi_backup.img bs=1M

Adjust [VOLUME_NAME] to match your USB drive’s mount point.

  1. Shrink and Compress the Image with pishrink.sh (Optional):
    Install pishrink.sh:
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin/

Run pishrink on the newly created image:

sudo pishrink.sh /media/pi/[VOLUME_NAME]/pi_backup.img /media/pi/[VOLUME_NAME]/myimg.img.gz

This will create a compressed, minimized backup image called myimg.img.gz, saving space and making it easier to store or transfer.

Verifying the Integrity of Your Backup Image

After creating a backup, it’s wise to verify it to ensure it’s not corrupted:

  1. Generate a Checksum (e.g., SHA-256):
shasum -a 256 /path/to/pi_backup.img

This command outputs a hash. Save this hash in a text file.

  1. Later, Re-Verify Against the Saved Hash:
shasum -a 256 -c /path/to/saved_hash.txt

If the hash matches, the image is intact and unchanged.

By verifying your backup, you ensure that it can be safely used to restore your Pi without encountering hidden file corruption.

Restoring From Your Backup

To restore, simply reverse the process:

  • From Another Computer (Win32DiskImager):
    Select the backup .img file and click Write to flash it back onto an SD card.

  • Using dd on Linux/macOS:

sudo dd if=~/pi_backup.img of=/dev/sdb bs=1M

(Adjust the input file and device paths accordingly.)

  • On the Raspberry Pi (if you have the .img on a USB drive):
    Just reverse the dd command, writing from the .img file to the SD card device while the Pi is running from another boot source (e.g., USB boot). Be cautious—this will overwrite the card.

Conclusion

Regularly creating and verifying backup images of your Raspberry Pi’s SD card is a crucial practice to ensure data safety and quick recovery from unexpected issues. By:

  • Updating the Raspberry Pi before backing up,
  • Using trustworthy tools like Win32DiskImager or dd,
  • Performing on-device backups with a USB drive if no other computer is available, and
  • Verifying the integrity of your images,

you’ll have a reliable safety net for all your Raspberry Pi adventures. Keep these backups in a secure location—cloud storage, an external hard drive, or both—and rest assured that your Pi projects can always be restored to a known-good state.