This page describes using the "dd" command on Linux to create an exact copy of an SD card, storing it in an "img" file, then restoring from an "img" file.

The Raspberry Pi uses an SD card to contain its operating system. Once you've gone to the trouble of getting your Pi to the point where it's perfect, wouldn't it be great to do a backup copy? And be able to restore from that backup? Because there's always the chance that something will go wrong, it's generally good to make backups anyway.

Doing a backup of an SD card isn't just a matter of copying a bunch of files, because the SD card on a Pi is split up into multiple partitions, and the copy must duplicate not just the file structure and files, ownership and permissions, but the type and layout of the partitions. Copying an entire drive is called "cloning".

If you're using a Linux or Mac OS computer this page applies. If you're using Windows you can do this using the Windows Subsystem for Linux (WSL), an Ubuntu-as-an-application that can be installed — with some effort — on Windows 10. If you want to use Windows, there are many web pages describing how to use Windows applications to copy cards. We don't use Windows around here.

How to Clone an SD Card#

This describes how to clone an SD card to an "img" file.

Finding Your SD Card#

Insert the original SD card into your Linux desktop computer and check the name of the device:

sudo fdisk -l
The result will list all of the drives connected to your computer, which appear in the /dev directory as if they were files (they're not). You'll need to determine which one of these is your SD card, as you don't want to be messing around with the drives on your desktop. Big mistake.

In my case, I'd inserted a 16GB SD card, so I was looking for a drive around that size (I have no other 16 GB drives so this was sure to be precise).

At the bottom of the fdisk list I found:

Disk /dev/sdd: 14.9 GiB, 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
Disklabel type: dos
Disk identifier: 0x06aa76a7

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sdd1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sdd2       532480 31116287 30583808 14.6G 83 Linux
This shows a drive of size 14.9GB mounted at the location /dev/sdd. Note that the usable space on any drive is always less than the advertised size. This drive is split into two partitions, a FAT32 partition of 256MB at /dev/sdd1 and a Linux partition of 14.6GB at /dev/sdd2. These correspond to the /boot and / (main) partitions of our Raspberry Pi OS.

So I've found my SD card at /dev/sdd.

Unmount the SD Card#

I need to unmount (disconnect) the drive before making the copy:

sudo umount /dev/sdd
If you get a response like "umount: /dev/sdd: not mounted." you'll need to unmount the partitions individually:
sudo umount /dev/sdd1
sudo umount /dev/sdd2

Making the Backup Copy#

The "dd" command is part of the Linux OS, you don't need to install any software.

Once you've unmounted the partitions, you can then use the "dd" command to make the copy. I'll be storing it on my Desktop ("/home/altheim/Desktop") directory as a file named "rpi-backup-20210328.img". I generally add a timestamp to my backups so I can figure out when I made them.

sudo dd if=/dev/sdd of=/home/altheim/Desktop/rpi-backup-20210328.img bs=4M status=progress
Note that you must alter the "if" (input file) argument to match the drive of the SD card you found using fdisk, otherwise you may damage an existing drive. If won't necessarily be the same as what I found, and an SD card won't even always mount at the same location. Check it every time.
Let's analyse the arguments of that command line:
"if"
the "input file" and refers to the drive we want to copy
"of"
the "output file", the "img" file we're going to create. Use a full path to where you want to store the file (I wanted it to show up on my Desktop)
"bs"
(optional) the size of the blocks of data copied. The default is only 512 bytes, so bumping that up to 4MB will speed up the copying process
"status"
(optional) shows a helpful progress display during the copying process (e.g., "2357198848 bytes (2.4 GB, 2.2 GiB) copied, 131 s, 18.0 MB/s")

You can type "man dd" on the command line to see the user manual for "dd".

And then be patient and don't interrupt things or plug in or disconnect other USB drives while copying. Just let it do its thing. This can take quite awhile for the copying to complete. The bottleneck is the SD card read speed, which for me was around 18MB/second. On a 16GB drive you can do the math. Go get a cup of coffee. Read a book. Take the cat for a walk.

As a point of reference, on my desktop computer (an "Intel® Core™ i5-3570K CPU @ 3.40GHz × 4"), backing up my 16GB SD card using the above command line took 877.819 seconds (almost 15 minutes) to complete. Bumping up the block size from 4MB to 32MB the copy took 887.095 seconds, i.e., it made no noticeable difference.

How To Restore an SD Card From a Backup#

This describes how to restore an SD card from an "img" file.

Find Your SD Card#

Using the instructions above with fdisk, locate your SD card.

Format Your New SD Card#

From a Linux desktop you can use the Disks utility to reformat the entire drive (not simply its partitions), first unmounting and then deleting all partitions, then using "Format Volume" with an Ext4 format. Alternately, you can use gparted (the Gnome Partition Editor) (installed via: sudo apt install gparted). It's a bit beyond the scope here to describe how to reformat the drive using these GUI tools but we'll cover the command line approach because it's actually easier.