Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Last active October 26, 2025 15:30
Show Gist options
  • Save kdmukai/cb1d787cbf2d915785af25f2682646f4 to your computer and use it in GitHub Desktop.
Save kdmukai/cb1d787cbf2d915785af25f2682646f4 to your computer and use it in GitHub Desktop.
Make a disk image from a Raspi SD card

Backup and Restore a Raspi SD Card

Only works on linux. I've repeatedly tried to find a sequence that works on macOS but haven't had any success.

Set up PiShrink

This script will remove empty space from the backup img which ensures that you'll be able to write the img back out to an SD card that's the same size as the source SD card.

git clone https://github.com/Drewsif/PiShrink.git

# install PiShrink dependencies (per the PiShrink docs)
sudo apt update && sudo apt install -y wget parted gzip pigz xz-utils udev e2fsprogs

Clone the source SD card

# List all the disks (sd card is usually /dev/sdb on my system)
lsblk -p

# Unmount the sd card, if necessary
sudo umount /dev/sdb

# Do a full uncompressed clone
sudo dd if=/dev/sdb bs=4M of=seedsigner_dev_2025-10-25.img status=progress conv=fsync

Shrink the img

./PiShrink/pishrink.sh seedsigner_dev_2025-10-25.img seedsigner_dev_2025-10-25_shrunk.img

Write the image to the target SD card

Swap the target SD card in (double check the disk name) and then flash it:

sudo dd if=seedsigner_dev_2025-10-25_shrunk.img bs=4M of=/dev/sdb status=progress conv=fsync

Note: Raspberry Pi Imager or Balena Etcher should also be able to use the shrunk img to flash to a new SD card, but I haven't tried.

Cleanup

Delete the full-sized img.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment