How To Backup An Sd Card
Back to current versionRestore this version
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, 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.

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
Once you've unmounted the partitions, you can then use the "dd" command to make the copy. I'll be 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 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
"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 progress bar during 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.