Backup and Restore SD Card – Raspberry Pi

Backup and Restore SD Card for Raspberry Pi

I was in a bind and needed a microSD card to help unbrick an embedded development board. Without a free card available my next choice was to salvage a microSD card in use on a Raspberry Pi, backup the image, do what I needed, then restore the image. I’ve done the process before for saving Raspberry Pi drives. This means searching for the ‘dd’ commands that I can’t ever recall.

If you’re on Windows, just grab http://sourceforge.net/projects/win32diskimager/files/latest/download and use it. Simple process.

Backup on Linux

I’ll run through the process for backup and restore on Linux. This applies to SD cards and microSD cards. First, you’ll need to figure out what device you SD card is using. Pay special attention because you don’t want to use the wrong device and destroy something like your harddrive. Start without the card inserted and run:

$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 243M 2.9G 8% /run
/dev/nvme0n1p2 198G 162G 26G 87% /
tmpfs 16G 37M 16G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/nvme0n1p1 511M 3.5M 508M 1% /boot/efi
/dev/sda1 459G 248G 188G 57% /opt/containers

Insert the card and run the command again. Look for the newly added device(s).

$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 243M 2.9G 8% /run
/dev/nvme0n1p2 198G 162G 26G 87% /
tmpfs 16G 37M 16G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/nvme0n1p1 511M 3.5M 508M 1% /boot/efi
/dev/sda1 459G 248G 188G 57% /opt/containers
/dev/sdd2 7.3G 3.2G 3.9G 45% /media/spisak/retropie
/dev/sdd1 57M 22M 36M 37% /media/spisak/boot

In my case, the card was at the bottom as /dev/sdd2 and /dev/sdd1. The volume names “retropie” and “boot” make sense. The device is “/dev/sdd”. The number appended is the partition. We want to back up the ENTIRE device so we can restore it later.

Unmount the two devices:

$ sudo umount /dev/sdd2
$ sudo umount /dev/sdd1

Next, we’ll use ‘dd’ to copy the contents of the device in to a file.

$ sudo dd if=/dev/sdd of=./retropi.img
1374271+0 records in
1374271+0 records out
703626752 bytes (704 MB, 671 MiB) copied, 35.8459 s, 19.6 MB/s

The ‘dd’ command will take some time. You can open another terminal and ‘ls -la’ the directory to see the file size increasing. That offers a little peace of mind the backup is actually working.

Restore on Linux

After unbricking my devel board, I can now restore my retropie image. Identify the device used by the card by following the process from before. It was run ‘df -h’ without the card and then ‘df -h’ again with the card to identify the device. Pay attention! The wrong device could cause data loss. You may need to check ‘dmesg’ to find the device if it wasn’t automounted. In my case the SD card was formatted with FAT32 and not automounted.

To restore the image we start by unmounting any automounted partitions for that device.

$ sudo umount /dev/sdd1

Next, we’ll write the backup image to the SD card:

$ sudo dd bs=4M if=./retropi.img of=/dev/sdd
1904+0 records in
1904+0 records out
7985954816 bytes (8.0 GB, 7.4 GiB) copied, 1080.55 s, 7.4 MB/s

This may take awhile depending on the size of the image. I’m writing back an 8GB image. It was slow. Before you remove the card, sync the file system.

$ sync

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.