Matt White

Matt White

developer

Matt White

developer

| blog
| categories
| tags
| rss

Backup and Duplicate Raspberry Pi Images

Setting up a Raspberry Pi from scratch can quickly become tedious - especially if you need to configure a lot of devices in sequence.

If you create backup snapshots of your Raspberry Pi images, you can quickly restore or duplicate the setup across other devices.

For this example, I’ll use a Raspberry Pi that I configured with IP camera software.

The process is very straightforward. You’ll need enough free space to backup the entire card (but we’ll fix the backup image size at the end).

Setup the Micro SD Card

Remove the micro SD card from the device you want to snapshot. Insert it into your system.

Identify the Card

Identify the SD card using lsblk or fdisk -l and find the storage that matches the size of your SD card.

~ $ lsblk
NAME              MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
sda                 8:0    0 931.5G  0 disk  
├─sda1              8:1    0   512M  0 part  
└─sda2              8:2    0   931G  0 part  
nvme1n1           259:0    0  27.3G  0 disk  
...
sdg                 8:96   1  58.2G  0 disk  
├─sdg1              8:97   1   256M  0 part  
└─sdg2              8:98   1    58G  0 part  
...

My SD card is 64GB, which matches the sdg device listed above. Remember that the advertized size uses 1000 to represent a GB and the system uses 1024 to represent a GiB, which accounts for most of the 64GB/58.2GiB size discrepancy.

Unmount the Card, if Necessary

If your system auto-mounted the card, it’s best to unmount it. This prevents data from changing on the card in the middle of backing it up, which could result in corrupted data.

Unmount the device you identified:

sudo umount /dev/sdg*

The asterisk wildcard should catch the individual partitions of the card.

Back up the Image

Run the command below, replacing /dev/sdg with the device you identified and /home/$USER/Documents/rpi-backup.img with your preferred location.

sudo dd bs=4M \
    if=/dev/sdg \
    of=/home/$USER/Documents/rpi-backup.img \
    conv=fsync \
    status=progress

Backing up the image typically takes a long time. The entire disk gets backed up, despite the fact that most of it is likely empty.

After the process completes, I always chown the result

sudo chown $USER: /home/$USER/Documents/rpi-backup.img

Shrink the Image

Download PiShrink

Now to use a handy repository called PiShrink, which abstracts away the complexity of resizing a Pi image behind a shell script.

You can either pull the repository with git:

git clone git@github.com:Drewsif/PiShrink.git
cd PiShrink

Or if you’re more daring, wget the thing and place it in your path somewhere.

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

Shrink Ray the Day Away

sudo pishrink.sh /home/$USER/Documents/rpi-backup.img

The space savings depend on how much empty space the image has. For me, PiShrink typically takes my Pi Images from 60 GiB down to 3 or 4 GiB, which adds up quickly if you’re saving a lot of snapshots over time.

Applying the Image

These images can be applied the same way as any other.

Option 1: Terminal

In the terminal, you can apply our backup process in reverse:

sudo dd bs=4M \
    if=/home/$USER/Documents/rpi-backup.img \
    of=/dev/sdg \
    conv=fsync \
    status=progress

Option 2: Etcher

As I’ve mentioned elsewhere, I like to use Etcher to apply images:

This post has a more detailed explanation on using Etcher, if you need it.

Learn by doing.