Using dd to back up only contents an image on an SD card

I have a Raspberry Pi (RPi) with a 128 GB SD card. This SD card contains the full OS and settings for the RPi and I would like to back it up. The total size of all files on the SD card are around 350 MB.

I put the SD card into my laptop which runs a Linux distro and used dd to make a copy of the contents of the SD card:

sudo dd if=/dev/sdb of=RPi4Image.img

This command was taking a long time and I checked the file size being generated on my laptop and it was in the 44 GB size and not finished being created. I assume I would end up with a 128 GB file size, to only backup 350 MB of content, which is not useful to me.

I stopped the process.

It seems to me that this dd command makes an image of the whole SD card, all 128GB of it, even when I only need to backup the 350 MB of contents / files on it.

  • Am I correct in assuming that the dd command backs up the whole device and so makes a file size that is equal to the whole SD card size?
  • If so, are there any ways to backup, and be able to restore to an SD card, if needed, only the 350 MB of contents of the SD card?
Asked By: ironfish

||

Am I correct in assuming that the dd command backs up the whole device and so makes a file size that is equal to the whole SD card size?

Yes, dd does a block-by-block copy

If so, are there any ways to backup, and be able to restore to an SD card, if needed, only the 350 MB of contents of the SD card?

You can use Partclone to achieve that

Answered By: Nicolas Formichella

dd without arguments copies from source to destination until reaching the end of one or the other, but it is not the only way of using the tool, or even the usual.
If, for example you know that the filesystem on the card was limited to the first 2GB of space you could specify to only dump that much. You do this using the bs blocksize option and the count option.

dd if=/dev/sdb1 of=new.img bs=1M count=2048

If necessary you could maybe use resize2fs to resize a linux filesystem to make it occupy only the beginning of the drive.

If however, the filesystem was not limited to part of the disk, and isn’t a filesystem you can resize from Linux like ext2/3/4 then your only option is to take an image of the entire disk/partition.

Fortunately, if your system was put on a fresh card and you weren’t using it read-write for a long time this system will usually have a LOT or zeros and will compress down to almost exactly the used space. So gzip or zip the image file as soon as you have it.

You can even do this on the fly so that you never need the whole file on the disk:

dd if=/dev/sdb1 | gzip | dd of=new.img.gz

This is because if if or of aren’t specified they default to stdin or stdout. It won’t save on copy time but will save on disk space needed to store the image.

Answered By: davolfman
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.