How to Clone Your Raspberry Pi SD Card for Super Easy Reinstallations

How This Works

I’ve had this happen all too often, and I eventually figured out a good solution. Once I set up my Pi project exactly how I want it, I just use Win32 Disk Imager on Windows to clone an image of its SD card onto my PC. There I keep it, safely, until something goes wrong with my Pi. When that happens, I can just re-clone that image to the SD card, overwriting the broken or corrupt version, and I’m be back up and running in no time. (If you don’t use Windows, you can do something similar on Linux with the dd command.) It’s so simple, every Raspberry Pi user should do it.

This works best with those Pi projects that require initial setup and then just run in the background, doing their thing. If you ever make changes to the Pi project, you’ll need to re-clone the image, but with a lot of projects, this is perfect. For example, I use this technique for my two Raspberry Pis running Kodi—if either ever goes down, I can just re-clone my personal image, and the boxes are back up and running in no time, grabbing up-to-date library data from my home server and MySQL database as if nothing ever happened.

And as a bonus, you can more easily share your Raspberry Pi projects by just writing your cloned image out to a new SD card (or sharing the image itself).

Here’s how to do it.

Video

Copy SD card contents to an image file

In this section we start with copying the contents of the SD card to an image file on your PC. Power off your Raspberry PI, remove the SD card and insert the SD card into your PC.

Determine the SD card device name

Before we can copy the SD card contents, we need to first figure out its device name on your Linux PC. Otherwise we do not now how to access it. We can do so by running this command from the terminal:

lsblk -p

On my PC the command output looks like this:

In my case the device name of the SD card is /dev/

In my case the device name of the SD card is /dev/mmcblk0. How do I know this? For starters, the only other device name is /dev/sda/ and this one is my PC’s disk drive. Furthermore, I know that the SD card stores the Raspberry PI operating system. The Raspberry PI operating system always consists of two partitions:

  1. Boot partition (/dev/mmcblk0p1)
  2. Root file system partition (/dev/mmcblk0p2)

Therefore, the device name of my SD card must be the other one: /dev/mmcblk0. The exact name might be different on your PC. Make sure to write down the device name on your PC for later reference. Then every time I mention /dev/mmcblk0 in the remainder of this article, replace it with the device name of your SD card.

Before moving on, double-check that you got the correct device name for your SD card. When unsure, you could temporarily remove the SD card from your PC. Next, run the lsblk -p command again to determine which device name disappeared. This one was your SD card device name.

Dump the SD card data to an image file

At this point we can start with the actual clone operation of the Raspberry PI SD card to your Linux PC. The dd program allows us to make a data dump of the entire SD card contents into a file. The command syntax is as follows:

sudo dd bs=4M if=[SD CARD DEVICE NAME] of=[IMAGE FILE NAME] conv=fsync

Keep in mind that the image file size equals the size of the SD card itself. Before running the dd command, verify that you have sufficient free space on your PC’s hard disk.

I would like to name the image file raspi_lite_pragmalin.img and store this file in my user’s Download directory. You can of course adjust the name and location of the image file to whatever you desire. Just remember to change the SD card device name to what it is called on your PC. Here’s the command I ended up running:

sudo dd bs=4M if=/dev/mmcblk0 of=~/Downloads/raspi_lite_pragmalin.img conv=fsync

Note that this command can take several minutes to complete, depending on the size of your SD card:

We ran the command with sudo for super user privil

We ran the command with sudo for super user privileges. This way we know for sure that the dd program can access the SD card. As a result, the root user owns the created image file. We’ll change the image file owner to our own user with this command:

sudo chown $USER: ~/Downloads/raspi_lite_pragmalin.img

Further processing of the image file now no longer

Further processing of the image file now no longer requires super user privileges.

Advantages of Cloning Raspberry Pi SD Card

◉ Data protection:

If you’ve been using Raspberry Pi for a while, you probably know it’s a finicky little device. Especially when you use Raspberry Pi as a media center or retro gaming station, the SD card would be crashed and corrupted easily. Thus you may lost everything on it. To avoid this tragedy, you could clone the Raspberry Pi SD card to protect the data.

◉ SD card upgrade:

If the Raspberry Pi SD card has insufficient space or poor performance, you can upgrade it to larger one or newer one. In this case, cloning your Raspberry Pi SD card to larger card is a good way to avoid data loss.

2. Shrink, then copy

This method involves reducing the size of the main Linux partition on the SD Card and then performing the backup – all done on another PC, preferably one running Linux. A tool like GParted or similar is instrumental here. I employed this technique in the series of blog posts devoted to installing a Triple Boot System on the Pi. I suggest you take a look there.

In short the process is something like this: on a Linux based system attach your SD Card into a reader, shrink the Linux Partition (ext4) to something that just encompasses the data within it. Then perform a backup using dd of the boot (fat32) and linux (ext4) partitions. To restore: plop the new SD Card in, create the partitions, restore and resize the ext4 partition. Again take a look at the following blog post for more info: .

Advantages

  • Resulting file sizes are small
  • Can restore to any size SD Card

Disadvantages

  • Time consuming and more complex
  • Dangerous if you don’t know what you are doing

Steps

Visitor abracadabricx was kind enough to provide a script. Please see the comments section below for his work. Thanks again!

Guide #2: How do I backup my Raspberry Pi SD card to an image file?

DiskGenius supports to back up a NTFS/FAT32/FAT16/FAT12/exFAT/Ext4/Ext3/Ext2 partition to an image file, and the function also supports incremental backup which helps to create multiple restore points. Additionally, the program allows users to extract files from image files without restoring partition from it.

Step 1: Select the partition you want to backup and click Backup Partition button.

Tip: Generally, there are two partitions on the Raspberry Pi SD card, the boot partition with FAT32 file system and the rootfs partition with EXT4 file system. You can backup them individually. If you want to create an image for the entire SD card, then use the function Copy Sectors.

 		Step 2: Set a location to save the backup image

Step 2: Set a location to save the backup image.

Click Select Image File button and you can browse your computer to specify a location for the image file. Remember to set a name for the file.

 		Step 3: Click Start button and you will see fol

Step 3: Click Start button and you will see following message box. Click OK to start the backup process.

 		Step 4: Wait for the backup to finish.

Step 4: Wait for the backup to finish.

How to Shrink a Partition on Raspberry Pi

If you want to make a disk image of a microSD card, but don’t have an external USB drive of a greater capacity, you have a problem. Even though the eventual .img.gz file you create in the tutorial above should be much smaller than your source card, you still need enough space to accommodate the uncompressed .img file as part of the process. 

What’s particularly frustrating is that, by default, the dd file copy process makes an image out of ALL the space on your microSD card, even the unused space.For example, you might have a 64GB microSD card, but only be actually using 6GB of space. If you don’t shrink the rootfs partition, you will end up copying all 64GB over to your external drive, which will take a lot more time to complete and will require that you have at least 65GB of free space. 

So the solution is to shrink the rootfs partition of your microSD card down to a size that’s just a little bit bigger than the amount of used space. Then you can copy just your partitions over to the USB drive.

To do the shrinking, you’ll need a USB microSD card reader and a second microSD card with Raspberry Pi OS on it.

1. Put your source microSD card (the one you want to copy) in a reader and connect to your Raspberry Pi.

2. Boot your Raspberry Pi off a different microSD card.

3. Install gparted on your Raspberry Pi.

4.  Launch gparted from within the Raspberry Pi OS GUI. It’s in the System Tools section of the start menu. 

(Image credit: Tom’s Hardware)

5.  Select your external microSD card from the pull down menu in the upper right corner of the gparted window. 

(Image credit: Tom’s Hardware)

6. Unmount the rootfs partition if it is mounted (a key icon is next to it) by right clicking it and selecting Unmount from the menu. If the option is grayed out, it’s not mounted. 

(Image credit: Tom’s Hardware)

7.  Right click rootfs and select Resize / Move. 

(Image credit: Tom’s Hardware)

8.  Set the new size for the partition as the minimum size or slightly larger and click Resize.. Note that gparted may overreport the amount of used space (when we unmounted a partition with 4.3GB used, it changed to say 6GB were in use), but you have to go with at least its minimum. 

(Image credit: Tom’s Hardware)

9.  Click the green check mark in the gparted window and click Apply (when warned) to proceed. 

(Image credit: Tom’s Hardware)

10. Shutdown the Raspberry Pi.

11. Remove the source microSD card from the USB card reader and insert it into the Raspberry Pi to boot from.

12. Follow the instructions in the section above on creating a disk image. Make sure to use the count attribute in step 5. 

Video

You may also like: 25 awesome Raspberry Pi project ideas at home 15 best operating systems for Raspberry Pi (with pictures) My book: Master your Raspberry Pi in 30 days

Type of Backups

I like to separate the backup methods into three possible scenarios:

  1. Block per block copy
  2. Shrink, then copy
  3. File based copy (live option)

How to Shrink the rootfs Partition on RPi SD Card

When we give dd command for copying the SD card files for backup, it makes an image of all the storage of the SD card, even the space that is not utilized. For example, if you have a 128 GB card with a used space of just 8 GB, the command will create an image of the entire 128 GB storage. This is where it becomes important to shrink the rootfs partition on the RPi SD card.

For the partition shrinking process, you will require an SD card reader and another SD card with RPi installed on it.

Insert your Sd card reader with the root SD card into the Raspberry Pi. Boot up your Raspberry Pi with another SD card. Now, install the GParted on your Pi board using the command

sudo apt-get install gparted -y

Navigate to the system tools in your Raspberry Pi and launch Gparted. Choose the external SD card from the drop-down menu of the Gparted window. Next, unmount the external SD card by right-clicking and choosing the option unmount. Hit right-click on the rootfs and select the Resize/move option. Then, enter the size for the partition. You have to enter the minimum size, and it should be slightly larger than the space used. Click on the resize button.

Check on the green tick-mark given in the toolbar area to proceed. Turn off the Raspberry Pi

And remove the SD card.

Since the partition has been created, you need to create the disk image. As we shrunk a partition in the source SD card, we will require to use the count attribute to tell the command to copy only the utilized MBs in the space.

The command is:

sudo dd if =/ dev/ mmcblk0 of =[ mount point] / myimg.img bs =1M count =?

In the command above, you have to replace? with the count of MBs, you desire to copy.

You can follow the steps provided above.

Read More About Raspberry Pi SD Card Clone

This part covers more frequently asked questions related to how to clone Raspberry Pi SD card. Check details.

How do I clone an SD card?

Free download EaseUS Todo Backup software to clone an SD card with ease:

  1. 1. Connect your SD to your computer and check if it can be correctly recognized.
  2. 2. Start EaseUS Todo Backup. Click the left panel and choose Clone.
  3. 3. Select your SD card to clone and set the storage device.
  4. 4. Preview the disk layout and then click Proceed to clone the SD card.

How do I copy my Raspberry Pi SD card to the bigger card?

Step-by-step procedures to copy Raspberry Pi SD card to a bigger SD card:

  1. 1. Connect the source and target SD cards to your computer with card readers.
  2. 2. Download and run the powerful SD card backup tool – EaseUS Todo Backup.
  3. 3. Select the Clone feature and choose the Raspberry Pi SD card.
  4. 4. Select the bigger new SD card and start copying the files.

How do I clone a bootable SD card?

Use EaseUS data backup software to clone a bootable SD card:

  1. 1. Download and install EaseUS Todo Backup on your Windows OS computer.
  2. 2. Connect the bootable SD card to your computer and make sure it can be detected.
  3. 3. Launch EaseUS Todo Backup and choose the Clone option on the left pane.
  4. 4. Select the bootable SD card as the source disk to clone.
  5. 5. Select the destination device to start the backup.

How do I back up my Raspberry Pi SD card images?

You can use a data backup program to back up Raspberry Pi SD card to Windows hard drive or other storage devices. The backup software will copy the entire SD card as an image.

If the SD card and data corrupted, you can quickly restore the image back to the SD card, then, plug it into the Raspberry Pi and run again.

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

Adblock
detector
Go up