The Raspberry Pi, a beloved tool for tinkering, invites experimentation. Yet, amid the joy of exploration, having a reliable baseline configuration to return to post-experimentation is cherished.
Imagine this scenario: You stumble upon an enticing Raspberry Pi project or Linux distribution. Eager to dive in, you face a dilemma—you possess only one micro SD card with a valuable working installation you wish to preserve.
Having an exact backup of your Raspberry Pi installation is beneficial in both scenarios. Today, we’ll explore how to clone or backup the Raspberry Pi micro SD card to an image file, and then restore it once our experimentation is complete. We’ll cover the process for Windows, Linux, and macOS.
Note: This method creates an image file matching the SD card’s total capacity. For instance, a 16 GB SD card will produce a 16 GB image file, regardless of the actual space used by your installation. Shrinking the image size is possible, but it’s exclusive to Linux, which we’ll discuss later in the tutorial.
Windows Instructions
Backup Raspberry Pi SD Card
1. Insert the micro SD card you want to clone into your PC using a USB or built-in card reader.
2. Download, install, and run Win32DiskImager. You will encounter a screen similar to this:
3. In the Device section at the top right corner, select your SD card drive (e.g., D:). Then, click the folder icon to its left and pick a location and filename for the image file. I named mine raspbian_backup_19_oct.img. Ensure the filename has a .img extension. Afterward, click Read.
4. Cloning the SD card to the specified location will take some time. Please refrain from turning off or putting your PC to sleep during this process. Once completed, you’ll receive a “Read Successful” message.
Now, you can reinsert the card into your Raspberry Pi and proceed with experimentation or installing a new distro. When you’re finished and wish to restore the backed-up image, follow the steps outlined in the next section.
Restore Raspberry Pi SD Card
Insert the micro SD card into your PC and launch Win32DiskImager. Choose the previously created image file and the appropriate drive in the Device section. Then, click the Write button to save the image to the SD card exactly as it was when copied.
Restoring the SD card will take time, depending on its size. Once complete, remove the card from your PC and reinsert it into the Raspberry Pi.
Linux Instructions
Backing Up Raspberry Pi SD Card
1. Insert the SD card into your PC using a USB or built-in card reader. Open a Terminal window and enter the command sudo fdisk -l. This will list all filesystems on your system.
2. Identify the device name of your SD card. For example, my 16GB SD card is recognized as the device /dev/sdb, with a size of 14.9GB due to the actual storage being slightly lower than advertised. Make note of this device name.
3. Use the dd command to write the image to your hard disk. For example:
sudo dd if=/dev/sdb of=~/raspbian_backup.img
Here, the if parameter specifies the file to clone, which is /dev/sdb, my SD card’s device name. Replace it with yours. The of parameter specifies the file name to write to. I chose raspbian_backup.img in my home directory.
Note: Be careful and double-check the parameters before executing the dd command, as entering incorrect parameters can potentially destroy your drive data.
During the cloning process, there will be no output until it’s completed, which may take some time depending on the SD card size. Once finished, you’ll receive an output like this:
Now, you can safely remove the SD card for use with your Pi. When you’re ready to restore the backed-up image, follow these steps:
Restoring Raspberry Pi SD Card
1. Insert the SD card into your PC. Before restoring the image, ensure that the SD card’s partitions are unmounted. To confirm, open Terminal and run the command sudo mount | grep sdb, replacing sdb with your SD card’s device name.
If the output is blank, no action is needed. If there are mounted partitions listed, unmount them. For instance:
sudo umount /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4
2. Use the dd command to write the image file to the SD card:
sudo dd if=~/raspbian_backup.img of=/dev/sdb
This mirrors the previous cloning command, but in reverse. Now, the input file is the backup image, and the output file is the SD card device.
Verify and double-check the parameters carefully to avoid irreversible data loss.
Upon completion, dd will confirm. Remove the card from your PC and reinsert it into the Raspberry Pi.
macOS Instructions
Backup Raspberry Pi SD Card
1. Insert the SD card into your Mac using a USB or built-in card reader. Open a Terminal window and enter the command diskutil list
to identify the device ID of your SD card (e.g., mine is /dev/disk3).
2. Unmount your SD card:
diskutil unmountDisk /dev/disk3
Replace disk3
with the name of your SD card from step 1.
3. Use the dd command to write the image to your hard disk. For instance:
sudo dd if=/dev/disk3 of=~/raspbian_backup.img
Here, the if parameter specifies the file to clone. In my case, it is /dev/disk3, my SD card’s device name. Replace it with yours. The of parameter specifies the file name to write to. I chose raspbian_backup.img in my home directory.
Note: Be careful and double-check the parameters before executing the dd command, as entering the wrong parameters can potentially destroy your drive’s data.
You will not see any output from the command until after the cloning is complete, and that might take a while, depending on your SD card’s size. You can then remove the SD card and use it in your Pi. Once you are ready to restore the backed-up image, follow the instructions below:
Restore Raspberry Pi SD Card
1. Insert the SD card into your Mac. Open a Terminal window, and unmount it using this command:
diskutil unmountDisk /dev/disk3
Replace “disk3” with your SD card’s name from step 1.
2. Use the dd command to write the image file to the SD card:
sudo dd if=~/raspbian_backup.img of=/dev/disk3
This reverses the cloning command. Now, the input file is the backup image, and the output file is the SD card device.
Ensure you verify and double-check the parameters to avoid permanent data loss.
Once the write is complete, you will receive confirmation from dd. Remove the card from your Mac, and reinsert it into the Raspberry Pi.
How to Shrink the Cloned Raspberry Pi Image (Linux-only)
As mentioned at the start of the article, these methods create an image file equal to the total SD card capacity. For instance, cloning a 32GB card will produce a 32GB image file, even if only 5GB is in use. This is acceptable for one or two images, but more (especially with an SSD) will exhaust space.
To overcome this limitation, we’ll employ PiShrink, a script that automatically reduces a Pi image, which then resizes to match the SD card’s maximum size upon boot. This also accelerates the image copying process back onto the SD card.
Regrettably, this tool is solely accessible on Linux. If Linux isn’t installed, you can set up the latest Ubuntu or Linux Mint version in a virtual machine and execute the script there. Here’s how:
1. Get the PiShrink script and grant it executable permissions. In a Terminal, input these two commands:
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x ./pishrink.sh
2. Execute the script, followed by the image name you wish to shrink.
3. The image will shrink. Once done, write the shrunk image file to the SD card as mentioned above methods. For example, on Linux:
sudo dd if=~/raspbian_backup.img of=/dev/sdb
Clone Raspberry Pi SD Card for Easy Restore
That’s all on cloning, backing up, and restoring Raspberry Pi installations. Never lose a perfectly working Raspbian OS installation on Raspberry Pi to try a new distro or project. Just back up your installation, tinker around, and restore the image when done. It doesn’t get simpler than that.
How did you like this method of backing up your Raspberry Pi installations? Have a better way? Questions? Comments? Let us know below.
Pritam Chopra is a seasoned IT professional and a passionate blogger hailing from the dynamic realm of technology. With an insatiable curiosity for all things tech-related, Pritam has dedicated himself to exploring and unraveling the intricacies of the digital world.