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 to clone or backup the Raspberry Pi micro SD card to an image file, and restore the 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

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. Download and install Win32DiskImager, and run it. You will see a screen like this:

imager

3. Under the Device section in the top right corner, select the drive of your SD card. It is D: in my case. Now click on the folder icon to its left, and choose a location and filename where the image file will be saved. I chose the file name raspbian_backup_19_oct.img. You can choose whichever filename you like, but it is preferable to have an .img extension. Once done, click on the Read button.

4. This will make a clone of the SD card in the location you specified. The copying will take a while to 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 file you created previously, and the appropriate drive in the Device section. Now, click on the Write button. 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

Backup Raspberry Pi SD Card

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

2. Try to find out the device name of your SD card. I have a 16GB SD card, so it is easily identified as the device /dev/sdb which 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 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 (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. The of parameter (output file) 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 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 are unmounted. To verify this, open the Terminal, and execute the command sudo mount | grep sdb. Here, replace sdb with 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, unmount the listed ones. For example:

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 is like the command we used to make a clone, but reversed. This time, the input file if is the backup image, while the output file of is the SD card device.

Again, verify, and double-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 a Terminal window, and enter the command diskutil list. Try to identify the device ID of your SD card. For example, mine shows up as /dev/disk3.

2. Unmount your SD card:

diskutil unmountDisk /dev/disk3

Here, replace disk3 with the name of your SD card that you identified in step 1.

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

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

Here, the if parameter (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. The of parameter (output file) 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 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 a Terminal window, and unmount it using the following command:

diskutil unmountDisk /dev/disk3

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

2. Use the dd command to write the image file to the SD card:

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

This is like the command we used to make a clone, but reversed. This time, the input file if is the backup image, while the output file of is the SD card device.

Again, verify, and double-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 use PiShrink, a script that automatically shrinks a Pi image that 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 the PiShrink script, and make it executable. In a Terminal, enter the following two commands:

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh

chmod +x ./pishrink.sh

2. Run the script, followed by the name of the image that you want to shrink.

sudo ./pishrink.sh ./raspbian_backup.img

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:

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

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.

Comments 33
  • Smelly says:

    Thanks for making this article. It turned out to be a lot simpler than I thought.

    Manpages suggest a progress “indicator” appears when we supply “status=progress” as an argument to the dd command.

  • danni says:

    there is a missleading line in your instructions on linux devices;
    dd if=/dev/sdx of=/raspi.img should be like
    dd if=/path/to/image of=/path/to/partion.

    I run the line and copied the empty sd card into rasli.img file and my home directory run out of space :))

  • joseerj says:

    Thank you! Win32 Disk Imager now comes with an option “read only allocated partitions” that makes the shinking process directly on windows!

  • Thomas Sturges says:

    Will this copy just the used part of the SD card (in my case about 3GB) or will it copy the entire SD card (i.e. the full 64GB available). It seems to me that somehow it is copying everything, even with unused/unwritten data.

  • digimeter says:

    GUILHERME RODRIGUES & Jason’s question still not answered as I would also like to know.

    They mention if the SDcard if read in Windows can display as multiple drive letters, I am viewing one now that has 3 drives listed in ‘This PC’ Devices & Drives, they are F: H: & I:, every tutorial I have read so far assumes one drive letter x: which would then be quite obvious which to use, so do we have corrupt SDcards, if so they work just fine ??
    So can some kind guru with RPi’s state clearly What drive do we select from the W32dskimage app drop down listbox to clone your Pi SD. ???? Totally confused as it seems only GUILHERME RODRIGUES & Jason and me have this issue and yet all is operating fine in a Pi – Help Please, totally confused.

    • Tim says:

      I have not been able to make it work either, since there is several drive letters. I was able to create an image, but not restore (using the windows method).

      Any ideas on this would be helpful!

  • Mr. D says:

    Thanks for the detailed post here. Appreciate the fact it covers the 3 major OS platforms. The info on Pishrink will be valuable as well. 🙂

  • Jim sting says:

    This sounds great ,can’t wait to try it ,thanks

  • scurvydog says:

    Helpful article, thanks, Sometimes adding ‘status=progress’ to end of dd command will show progress of copy/write.
    cheers

  • Michael Buckingham says:

    I used the dd command to clone the 16Gb pi3 micro card on to a 32Gb card as a .img file The new card only has the 16Gb image partition, and I can’t seem to resize to the 32Gb. Do you know a partitioning program that can resize to the full extent. Gparted wii not do this, nor another third party program.
    I am looking to use the pi3 for a server cloud in the house.
    Thank you,

    • Mr. D says:

      Hi Michael;
      I will be facing a similar issue as I have a Linux install image that is close to capacity for a 32Gb card and would like to migrate that image to a 64Gb card …
      Have you looked into Balena Etcher which is normally a tool used to write a setup Image to a new SD card. I had corrupted a SD card that had multiple partitions and Balena Etcher re-partitioned the corrupted SD card with a working boot image. Not sure if Balena Etcher would create the additional free space on the Card that could be used by the OS???

  • Roch says:

    Adding “status=progress” to dd for monitoring purpose:
    sudo dd if=/dev/sdb of=~/raspbian_backup.img bs=1M status=progress

  • punktyras says:

    Adding bs parameter would increase reading speed considerably
    sudo dd if=/dev/sdb of=~/raspbian_backup.img bs=1M

  • Max says:

    great! really, thanks!!

  • faruk says:

    thanks a lot

  • Mauri Carmo says:

    Thanks. You helped a lot.

  • Fred says:

    Will this work if you have a 16GB card and are trying to clone it to a 32GB card because you ran out of space?

  • ed says:

    Win32 sounds great…. but what if your PI SD card shows up as two drives under windows? “boot” and another one?

  • Aries says:

    PiShrink script can run on macOS via Docker

  • JaiBo says:

    This is my issue also. Can’t backup my kids Kano OS as it’s seen on Windows as two SD card partitions…

  • Nagarajan Kandasamy says:

    Regarding Backup Raspberry Pi SD Card part, we can have a graphical representation of the progress through the pv package. Install pv using
    -> apt-get install pv
    Modify the dd command as follows
    -> sudo dd if=/dev/sdb | pv | dd of=~/raspbian_backup.img
    Check man page of pv for briefing.

  • Charles M says:

    I’m getting a read only error when trying to restore my SD card:
    sudo dd if=~/raspbian_backup_1.img of=/dev/sdc
    dd: failed to open ‘/dev/sdc’: Read-only file system

  • Awneesh says:

    I am not able to restore the image . I have successfully made the backup image in ubuntu and was able to write it again on the sd card , but when i putted it in Raspberry Pi 3 , it says “unable to find root fs block”

  • James says:

    Thanks! On OSX with a 64GB SD card it took 86 minutes.

  • Chuck MacCluer says:

    Hey, it’s even simpler. The RPi3 has a built-in SD copier. Put the blank CD in a USB holder and pull up the app. Check your blank and mash copy.

    • ed says:

      How do you put a blank CD in a USB holder?
      What app you ‘pull up’
      check your blank… for what???

      • Scott Rossell says:

        I believe Mr. MacCluer is referring to the SD Copier app on the Raspberry Pi discussed here: https://www.raspberrypi.org/blog/another-update-raspbian/ (Scroll down a bit or search for “SD card copier”. This page discusses several different subjects). I have no idea how well it works.

  • GUILHERME RODRIGUES says:

    Hello! When I insert my micro SD on my computer, there are 2 partitions on the SD, the only readable one is boot with 64mb. If I backup only this one it probably won’t backup my data

    • Jason says:

      I had the same question. Raspbian creates multiple partitions that obviously show up as separate drives in windows. How do you clone the whole card?

      • Dolphik says:

        It’s a full clone of the card, not partition. If you are running on windows you just can’t access files in second partition but that’s ok

      • ONECore says:

        No, it will even show up as multiple drives in Ubuntu. In windows it looks like 5 drives, in Ubuntu it looks like 3. If I back up just one, and push that to a new drive, it does not work. Does anyone know how to back up multiple drives as one image. Also, it might be wishful, but damn, it would be great to use something like PiShrink to be able to move between different sized drives too.

      • Mauri says:

        You must backup the full device. So don’t specify a number after the device letter. Use /dev/sde instead of /dev/sde1, for example. On Win32 Disk Imager you don’t need to worry about those details.

  • Ric says:

    Great script for shrinking the SD card image. Thanks!

Leave a Reply