Using DD to dump only the partition data from a USB/SD Card on the Raspberry Pi

So I was playing recently with my Pi and wanted to backup the SD card – the issue with the commands given on the raspbian website is that if you DD an image to a larger SD card (say 16GB) and then make a backup of it, you will get a 16GB image, so for raspbian this is 2.9G of useful stuff with the rest being random useless data.

Well there is a way to avoid this and have a very nice correctly sized image. The gist of this is that we determine the block where the partition ends and dump everything until that point. This works just as well for any storage device.

Now the raspbian image is exactly 3276800000 bytes; when you DD this to a SD card and run fdisk against it you get this:


linux-suse:/home/rm # fdisk -l

Disk /dev/mmcblk0: 15.9 GB, 15931539456 bytes, 31116288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00090806

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/mmcblk0p2 122880 6399999 3138560 83 Linux

A simple calculation here of 6399999 x 512 = 3276799488, which is only 1 block (512 bytes) less than the size of the image (3276800000).

So in order to make sure I get a correct image with all the data we use the count and bs (block size) parameters of DD. The total number of blocks DD will write is count x bs so in order to write 3276800000 bytes I chose a count of 4096 and a bs of 800000. So the full DD command would be:

linux-suse:/home/rm # dd if=/dev/mmcblk0 of=raspbian_2014_09_13.img bs=800000 count=4096
4096+0 records in
4096+0 records out
3276800000 bytes (3.3 GB) copied, 112.452 s, 29.1 MB/s

My reason for choosing a block size of 800000? Well because I have plenty of RAM and reading lots of data in large chunks is much faster (29MB/s is pretty good!).

Leave a Reply

Your email address will not be published. Required fields are marked *

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