Friday, January 24, 2025

How do I back up SD Cards

    This is the primary way I back up SD cards for SBCs.  I use this when I have a working system to protect myself from SD card failure or to do mid-project backups in case I mess something up I don’t have to start over from the beginning.  I’ll be using an Orange Pi 5 Plus with Ubuntu 24.10 installed but any Debian-based system should be the same.

  •  Linux Computer
  • SD card reader
  • Large fast disk drive, at least 2.5 times the size of your SD card of free space

 

Make an Image

Insert an SD card into the reader, this can be a built-in or external, but a good fast external card reader is preferred.

Use lsblk to find our source drive.

lsblk


 

 

For me it is “mmcblk1”, this system has EMMC storage that is on mmcblk0

Here is the same SD card plugged into a generic USB card reader. It helps if the SD card is a different size than any other disk if you are not sure what disk your SD card is.

 

 

As the image copy can take a long time I like to have a progress indicator.  You can do it either way, the result is the same.

Plain DD

The input file(if) should be your SD card “disk” from the lsblk output with “/dev/” in front of it.  This is the part of the Linux file system that represents the raw SD card.

The output file(of) is where you want to write the image to.  You will need plenty of space, this image will be the size of the SD card, and we will need space to create a second copy.

sudo dd if=/dev/mmcblk1 of=sdc_backup.img


With a progress bar

The “if” and “of” are the same as when using the plain DD method.

For pv the size(-s) use the nearest whole GB

sudo apt install pv
sudo dd if=/dev/mmcblk1 | pv -s 28G | sudo dd of=sdc_backup.img


 

Now that we have an image of the SD card let’s shrink it down using a script called PiShrink.  Please check out Drewsif’s GitHub page for more documentation and alternative methods for installing.

https://github.com/Drewsif/PiShrink

To install

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


 

Let’s go back to the location of our SD card image and run PiShrink.

sudo pishrink.sh sdc_backup.img sdc_backup_shrunk.img


A screenshot of a computer

Description automatically generated

 

As you can see our backup image went from the original 29G(rounded up from 28.5) to less than 2!  This will be much easier to store and faster to restore onto another SD card not to mention we can now restore to a smaller SD card than the original one.

 

Testing

So now we have our new little image, let’s put in a different SD card and try it.  Just for fun, I’m going to use a much smaller SD card.

lsblk


 

sudo dd if=sdc_backup_shrunk.img of=/dev/mmcblk1

Or

sudo dd if=sdc_backup_shrunk.img | pv -s 2G |sudo dd of=/dev/mmcblk1


After imaging to the SD card and booting the image it auto-expanded!

A computer screen with white text

Description automatically generated

 

If the auto-resize fails, you can do it manually with resize2fs

This time we will want the partition(part) from lsblk

 

 

To resize

sudo resize2fs /dev/mmcblk0p1


UniFi Network force DNS over TLS network wide

The post on doing this in OPNSense has been pretty popular so let's do this with UniFi!  You will need a UniFi gateway and the Network a...