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 file, because the SD card on a Pi is split up into multiple partitions, and the copy must duplicate not just the files and file structure but the type and layout of those partitions. This 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 on the Windows Subsystem for Linux (WSL), an Ubuntu-in-situ application. 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.

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. You'll need to determine which one is the SD card, as you don't want to be messing around with the drives on your desktop.

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.

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

sudo umount /dev/sdd
Then use the "dd" command to make the copy, storing it in my home ("~") 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=~/rpi-backup-20210328.img bs=4M
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
"bs"
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

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

How To Restore an SD Card From an Image File#

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

TBD.