Backing up and duplicating SD cards with dd and LZ4

June 28, 2020

I frequently work with devices that run off micro SD cards, NVIDIA Jetson family being one of them. Duplicating SD cards for another prototype with a slightly different configuration running on the devices is a common task. As well as, finding that 5 months old prototype that was manualy tweaked to run better on target hardware, because all of a sudden another customer really wants to see it in action.

Most of the SD cards I run off are 32 gig ore more. Backing them up by simply using dd is all well and good, but the raw images will be as big as your card, taking up a ton of space. To compress all that empty space, I found LZ4 to be both extremely fast and with great compression ratio.

As always with dd, please triple check your device names, it can easily ruin your day. We are writing to the whole device, not a partition here, hence using /dev/sdX, not /dev/sdX1 etc.

To backup an image:

sudo dd if=/dev/sdf bs=16M status=progress | lz4 > backup.lz4

With my cheap-ass USB3.0 card reader, a micro SD HC card, writing to a mechanical HDD I get about 73.4 MB/s, and a final file size of 13.2 GB.

To extract the image we can use the -d flag to decompress: lz4 -d backup.lz4 image.img, or we can ask LZ4 to decrompress to stdout adding the -c flag and then piping it into dd, specifying our block device, writing it to a new SD card.

lz4 -dc backup.lz4 | sudo dd of=/dev/sdf bs=16M status=progress

Depending on your backup size this can take some time, but my card reader averaged at 53.2 MB/s.

This is by no means a good way to make production devices, where a Docker image would be more appropriate to deploy applications to the target hardware.


Profile picture

Written by Alex March