I wanted to back my current state as an image but Im running into a problem: it seems that my microSD contains two partition: pi_root and pi_boot. How do I create an exact image of my microSD card with two parititons using dd?
2 Answers
You can use dd
.
First you have to find the device name of the memory card, as this may depend on the card reader used. Run sudo lsblk
The output will be something like this:
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
mmcblk0 179:0 0 29.3G 0 disk
├─mmcblk0p1 179:1 0 56M 0 part /boot
└─mmcblk0p2 179:2 0 29.2G 0 part /
In this case, mmcblk0
is the only device, as it is done on a Pi. mmcblk0
is typically the name used with memory card readers, but USB card readers may also show up as sdb
and so on.
To make a complete backup of the whole card, issue the command sudo dd if=/dev/mmcblk0 of=filename.img
. If the device of the card is not mmcblk0, replace as per output of lsblk
output.
This will read all of the memory card into the file filename.img
. This should generally not be done from the Pi - as reading a live filesystem may lead to inconsistent results. Power off your Pi, and put the card into a different computer. Do not mount it.
To write the backup back to a card, reverse the input and outputs, i.e. sudo if=filename.img of=/dev/mmcblk0
.
Please be very careful, and read dd commands three times before you execute it. If you mix up input and output for instance, you risk destroying all your data!
If you want to compress the image for storage, run for instance bzip2 filename.img
. This will reduce the file size significantly, especially if you have a lot of free space. To decompress the image, for restoring, run bunzip2 filename.img.gz
.

- 22,691
-
Args, didn't see you posted already an answer while i was writing on my one :) – Videonauth Nov 11 '17 at 15:39
-
Thanks a lot. I only used sudo fdisk -l and it only showed the partitions, not hte whole disk so I was confused. – Peter P Nov 11 '17 at 16:33
If you insist on using dd
you can do it like described below. Just a word of warning beforehand, there are simpler to use tools like clonezilla which are less error prone and probably far more performant as using dd
.
First make sure you know the drive letters for your SD card (I assume here it is /dev/sdb1
and /dev/sdb2
containing your partitions). You can simply pull a full image of this drive by not addressing the single partitions instead using the drive itself (i.e. /dev/sdb
). To fetch the full contents of that drive in an image do:
sudo dd if=/dev/sdb of=~/myimage.img
to reverse it:
sudo dd if=~/myimage.img of=/dev/sdb
You may ask how you can get the proper drive letters to use, this you can by using lsblk
which will produce an output similar to this:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 119,2G 0 disk
├─sda2 8:2 0 16G 0 part [SWAP]
└─sda3 8:3 0 103,2G 0 part /
sdb 8:16 0 1,8T 0 disk
├─sdb1 8:17 0 1G 0 part /media/<username>/pi_boot
└─sdb2 8:18 0 15G 0 part /media/<username>/pi_root

- 33,355
- 17
- 105
- 120
dd
but I'd encourage a look at Clonezilla (http://clonezilla.org) – Mark Nov 11 '17 at 15:17dd
, See this description, https://askubuntu.com/questions/958242/fastest-way-to-copy-hdd/958248#958248; Notice that it works with any kind of mass storage device: memory card, USB pendrive, SSD, HDD. I suggest that you create an image of the whole drive with the bootloader and all the partitions (a compressed image, which is a directory with a number of files). -- When you restore from the image, you need a target drive of at least the same size as the source drive (not one byte smaller). The same is true fordd
. – sudodus Nov 11 '17 at 17:01