estadisticas web Skip to content

How to clone the Raspberry Pi SD card on Windows, Linux and macOS

How to clone the Raspberry Pi SD card on Windows, Linux and macOS

The Raspberry Pi is the tin toy par excellence. Most of its users love to experiment and stop working installations in the process. However, despite how fun it is, everyone loves having an operational base configuration that they can return to after the experiment is over.

Another common scenario, let's say, you've just met a fantastic new project based on Raspberry Pi or a Linux distribution, and you're dying to try it. The problem is that you only have a micro SD card and you have already installed a working installation that you don't want to miss.

In both of the scenarios described above, it is useful to have an identical bit-by-bit backup identical to the functioning installation of Raspberry Pi. So today we will learn how clone o run the backup the Raspberry Pi micro SD card into an image file e restore the image after finishing the experiments. 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 total capacity of the SD card. For example, if you have a 16 GB SD card, the resulting image file will also be 16 GB, regardless of the space actually used by the installation. There is a method of reducing the image size, but it only works on Linux, and we'll explain it later in the tutorial.

Instructions for Windows

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

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

4. This will clone the SD card to the specified location. Copy take a while of time to complete, so do not turn off or put the PC to sleep during this period. When finished, the message "Reading successful" will be displayed.

You are now free to insert the card into your Raspberry Pi and break things up or install a new distribution. Once you have finished playing and want to restore the backup image, follow the steps in the next section.

Restore the Raspberry Pi SD card

Insert the micro SD card into the PC and open Win32DiskImager. Select the image file previously created and the unit appropriate in the section Device . Now, click on the button write . The image will be saved to the SD card exactly as it was at the time of copying.

Again, this process will take some time depending on the size of the SD card. Once the restore is complete, you can remove the card from the PC and reinsert it into the Raspberry Pi.

Linux instructions

Backup of the Raspberry Pi SD card

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

2. Try to find out the first name of the device of your SD card. I have a 16 GB SD card, therefore easily identifiable as a / dev / sdb device which has a size of 14, 9 GB. This is because the actual storage on a device always slightly less than the advertised one. Write down this device name .

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

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

Here, the parameter if (input file) specifies the file to clone. In my case, / dev / sdb, which is the device name of my SD card. Replace it with the name of your device. The parameter (output file) specifies the name of the file to write to. I chose raspbian_backup.img in my home directory.

Note : be careful and double check the parameters before running the dd command, as entering the wrong parameters can potentially destroy the unit data.

You won't see any output from the command until cloning is complete, and it may take some time, depending on the size of your SD card. Once completed, you will see an output similar to the following.

Now you can remove the SD card and use it in your Pi. Once you are ready to restore the backup image, follow the instructions below:

Restore the Raspberry Pi SD card

1. Insert the SD card into the PC. Before restoring the image, it is important to make sure that the SD card partitions are dismounted . To check this, open the Terminal and run the command sudo mount | grep sdb sudo mount | grep sdb. Here, replace sdb with the device name of the SD card.

If you see an empty exit, you don't have to do anything. If you see some partitions mounted, dismounts those listed. 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 di = / dev / sdb 

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

Again, check and check the parameters here, since entering the wrong command will result in permanent loss of data.

Once the writing is complete, a confirmation from dd will be displayed. then you can remove the card from the PC and reinsert it into the Raspberry Pi.

macOS instructions

Backup of the Raspberry Pi SD card

1. Insert the SD card into your Mac using a USB or built-in card reader. Now open a window of Terminal and enter the diskutil list command. Try to identify the device ID of the SD card. For example, mine looks like / dev / disk3.

2. Unmounting the SD card:

 diskutil unmountDisk /dev/disk3 

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

3. Use the dd command to write the image to your hard drive. 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, / dev / disk3, which is the device name of my SD card. Replace it with the name of your device. The parameter (output file) specifies the name of the file to write to. I chose raspbian_backup.img in my home directory.

Note : Be careful and double check the parameters before running the dd command, as entering the wrong parameters can potentially destroy the unit data.

You won't see any output from the command until cloning is complete, and it may take some time, depending on the size of your SD card. then you can remove the SD card and use it in the Pi. Once you are ready to restore the backup image, follow the instructions below:

Restore the Raspberry Pi SD card

1. Insert the SD card into your Mac. Open a Terminal 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 di = / dev / disk3 

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

Again, check and check the parameters here, since entering the wrong command will result in permanent loss of data.

Once the writing is complete, a confirmation from dd will be displayed. You can then 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 beginning of the article, these methods create an image file that equals the total capacity of the SD card. For example, cloning an SD card with a 32 GB capacity will create a 32 GB image file, even if only 5 GB are actually in use on the card. This is fine if you only have one or two of these images, but more of this (especially if you use an SSD) will make you run out of space.

To overcome this limitation, we will use PiShrink, a script that automatically reduces a Pi image which will automatically be resized to the maximum size of the SD card on startup. It also allows us to copy the image back to the SD card much faster.

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

1. Download it PiShrink script is Make it executable. In a terminal, enter the following two commands:

 wget //raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod + x ./pishrink.sh 

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

 sudo ./pishrink.sh ./raspbian_backup.img 

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

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

SD card Clone Raspberry Pi for easy recovery

This is all about cloning, i.e. backing up and restoring existing Raspberry Pi installations. You will no longer have to miss a fully functional installation of the Raspbian system on the Raspberry Pi to try a new distro or a new project. Just back up the installation, tinker and restore the image when you're done. It doesn't get simpler than that.

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