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#
Determining the Mount Point of Your SD Card#
Insert the original SD card into your Linux desktop computer and check the name of the device:
sudo fdisk -lThe 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 LinuxThis 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/sddIf 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#
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
- "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 an Image File#
TBD.