Here’s How You Can Clone Raspberry Pi SD Card on Windows, Linux and macOS

The Raspberry Pi is the ultimate tinkerer’s toy. Most of its users love to experiment and break working installations in the process. However, despite how much fun it is, everyone loves to have a working baseline configuration that that they can return to after the experiment is over.

Another common scenario is let’s say, you just got to know about a cool new Raspberry Pi-based project or Linux distro, and you are dying to try it out. Problem is, you only have one micro SD card, and already have a working installation on it that you do not want to lose.

In both the above scenarios, it is helpful to have an exact, bit-by-bit identical backup of your working Raspberry Pi installation. So today, we will learn how toclone or backupthe Raspberry Pi micro SD card to an image file, andrestorethe image after we are done experimenting. We will explain how to do it on Windows, Linux, and finally macOS.

Note: This method will create an image file exactly the size of the SD card’s total capacity. For example, if you have a 16 GB SD card, the resulting image file will also be 16 GB, no matter how much space your installation is actually using.There is a method to shrink the image size, but it works only on Linux, and we will be explaining it later in the tutorial.

Windows Instructions

Windows Instructions

Backup Raspberry Pi SD Card

1.Insert the micro SD card that you want to clone in your PC using a USB or built-in card reader.

2.Downloadand install Win32DiskImager, and run it. You will see a screen like this:

3.Under theDevicesection in the top right corner,select the drive of your SD card. It isD:in my case. Now click on the folder icon to its left, andchoose a location and filenamewhere the image file will be saved. I chose the file nameraspbian_backup_19_oct.img. You can choose whichever filename you like, but it is preferable to have an.imgextension. Once done, click on theReadbutton.

4.This will make a clone of the SD card in the location you specified. The copyingwill take a whileto complete, so do not turn off or put your PC to sleep during this period. Once it is done, you will get a “Read Successful” message.

You are now free to insert the card back in your Raspberry Pi and break things or install a new distro. Once you are done playing and want to restore the backed up image, follow the steps in the next section.

Restore Raspberry Pi SD Card

Insert the micro SD card in your PC, and open Win32DiskImager.Select the image fileyou created previously, and the appropriatedrivein theDevicesection. Now, click on theWritebutton. The image will be saved to the SD card, exactly how it was, at the time you copied it.

Again, this process will take a while depending on the size of the SD card. Once the restore is complete, you can remove the card from your PC, and insert it back in the Raspberry Pi.

Linux Instructions

Linux Instructions

Backup Raspberry Pi SD Card

1.Insert the SD card in your PC using a USB or built-in card reader. Now open aTerminalwindow, and enter the commandsudo fdisk -l. This will list all the filesystems present on your system.

2.Try to find out thedevice nameof your SD card. I have a 16GB SD card, so it is easily identified as the device/dev/sdbwhich has a size of 14.9GB. This is because the actual storage on a device is always slightly lower than advertised.Note down this device name.

3.Use theddcommand to write the image to your hard disk. For example:

sudo dd if=/dev/sdb of=~/raspbian_backup.img

Here, theifparameter (input file) specifies the file to clone. In my case, it is/dev/sdb, which is my SD card’s device name. Replace it with the device name of yours. Theofparameter (output file) specifies the file name to write to. I choseraspbian_backup.imgin my home directory.

Note:Be careful, and double check the parameters before executing theddcommand, as entering the wrong parameters here can potentially destroy the data on your drives.

You will not see any output from the command until after the cloning is complete, and that might take a while, depending on the size of your SD card. Once it is complete, you will see an output like the following.

You can now 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 in your PC. Before we restore the image, it is important to make sure that the SD card’s partitions areunmounted. To verify this, open theTerminal, and execute the commandsudo mount | grep sdb. Here, replacesdbwith your SD card’s device name.

If you see a blank output, you do not need to do anything. If you do see some mounted partitions,unmountthe listed ones. For example:

2.Use theddcommand to write the image file to the SD card:

This is like the command we used to make a clone, butreversed. This time, theinput fileifis the backup image, while theoutput fileofis the SD card device.

Again, verify, anddouble-verify the parameters here, as entering the wrong command here will cause permanent data loss.

Once the write is complete, you will see a confirmation from dd. You can then remove the card from your PC, and insert it back in the Raspberry Pi.

macOS Instructions

Backup Raspberry Pi SD Card

1.Insert the SD card in your Mac using a USB or built-in card reader. Now open aTerminalwindow, and enter the commanddiskutil list. Try to identify the device ID of your SD card. For example, mine shows up as/dev/disk3.

2.Unmount your SD card:

Here, replacedisk3with the name of your SD card that you identified in step 1.

3.Use theddcommand to write the image to your hard disk. For example:

sudo dd if=/dev/disk3 of=~/raspbian_backup.img

Here, theifparameter (input file) specifies the file to clone. In my case, it is/dev/disk3, which is my SD card’s device name. Replace it with the device name of yours. Theofparameter (output file) specifies the file name to write to. I choseraspbian_backup.imgin my home directory.

Note: Be careful, and double check the parameters before executing theddcommand, as entering the wrong parameters here can potentially destroy the data on your drives.

You will not see any output from the command until after the cloning is complete, and that might take a while, depending on the size of your SD card. 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 in your Mac. Open aTerminalwindow, and unmount it using the following command:

Here, replacedisk3with the name of your SD that you identified in step 1 of the previous section.

2.Use theddcommand to write the image file to the SD card:

This is like the command we used to make a clone, butreversed. This time, theinput fileifis the backup image, while theoutput fileofis the SD card device.

Again, verify, anddouble-verify the parameters here, as entering the wrong command here will cause permanent data loss.

Once the write is complete, you will see a confirmation from dd. You can then remove the card from your Mac, and insert it back in the Raspberry Pi.

How to Shrink the Cloned Raspberry Pi Image (Linux-only)

As mentioned at the starting of the article, these methods create an image file that is equal to the total capacity of the SD card. For example, cloning an SD card with a capacity of 32GB will create an image file of 32 GB, even if only 5 GB is actually in use on the card. This is fine if you only have one or two such images, but any more than that (especially if you use an SSD) will cause you to run out of space.

To work around this limitation, we will usePiShrink, a script thatautomatically shrinks a Pi imagethat will then automatically resize to the max size of the SD card on boot. This  also makes copying the image back onto the SD card much faster.

Unfortunately, this tool is only available on Linux. If you do not have Linux installed, you can install the latest version of Ubuntu or Linux Mint in a virtual machine, and run this script there. Here is how to use it:

1.Download thePiShrink script, and make it executable. In aTerminal, enter the following two commands:

  1. Run the script, followed by thename of the imagethat you want to shrink.

3.The image will then be shrunk. Once done, you can write the shrunk image file to the SD card as mentioned in the methods listed above. For example, on Linux:

SEE ALSO:How To Run Commands on Raspberry Pi by Email

Clone Raspberry Pi SD Card for Easy Restore

So that is all on cloning aka backing-up and restoring existing Raspberry Pi installations. Never again will you have to lose a perfectly working installation of Raspbian OS on Raspberry Pi to try out a new distro or a cool project. Just back up your installation, tinker around, and restore the image when you are done. Doesn’t get any simpler than that.

How did you like this method of backing up your Raspberry Pi installations? Got a better way? Questions? Comments? Let us know in the comments section below.

Shivam Raj

Command lines, DIY, video games, linguistics, and a little bit of everything else.

Add new comment

Name

Email ID

Δ

01

02

03

04