How to Back Up Your Raspberry Pi as a Disk Image

How to Back Up Your Raspberry Pi as a Disk Image
(Image credit: Tom's Hardware)

 

When you work hard on a Raspberry Pi project, you’ll want to make a complete disk backup of the entire OS and software, not just your code. Even the best Raspberry Pi microSD cards can fail or get lost and you may also want to re-use the project on another card or share it with others. For example, at Tom’s Hardware, we have a Raspberry Pi web server we use for battery tests and we have multiple Pis that all have the same exact image.

There are a few ways to backup a Raspberry Pi. You can use Raspberry Pi OS’s SD Card Copier app, which is under the Accessories section of the Start menu, to clone your microSD card directly to another microSD card. But unless you need a second card right away, it’s a better idea to create a disk image: a file you can store on a PC or in the cloud, distribute to others and write to a new microSD card at any time, using Etcher or Raspberry Pi Disk Imager.

Some folks recommend taking your microSD card, sticking it in a Windows PC and copying it sector-for-sector with Win32 Disk Imager, but that creates two problems. First, the card you are writing to has to be exactly the same size as the one you backed up or larger. Because there are subtle differences in the number of sectors on different makes and models of card, a 32GB San Disk card may have a few more sectors than a 32GB Samsung card and, if the destination card is smaller, the copy process won’t work properly and the card won’t boot. Second, your backup file will be huge: the full size of your card, even if you only were using 3 out of 32GB.

Fortunately, there’s a way to create a compressed disk image that’s even smaller than the amount of used space on the source microSD card you’re backing up. To create the disk image, you’ll need an external USB drive to connect to your Raspberry Pi and write it to. If the USB drive is a higher capacity than the source microSD card (ex: a 32GB USB drive to image a 16GB card), you can back up the whole card before shrinking it. However, if you don’t have a USB drive that’s big enough, check out the section at the bottom of this article on shrinking your rootfs partition. 

 How to Make a Raspberry Pi Disk Image 

1.  Format a USB Flash or the best hard drive as either NTFS (if you are using Windows on your PC and plan to read this drive on a PC) or EXT4 (for Linux). Make sure the Flash drive is larger than the capacity of the used space. Make sure to give the drive a volume name that you remember (ex: “pibkup” in our case). You can also format the drive directly on the Raspberry Pi if you like.  

Format Disk

(Image credit: Tom's Hardware)

2.  Connect the USB drive to your Raspberry Pi.

3. Install pishrink.sh on your Raspberry Pi and copy it to the /usr/local/bin folder by typing: 

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
sudo chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

 4. Check the mount point path of your USB drive by entering 

lsblk

use lsblk to find mount point

(Image credit: Tom's Hardware)

 

You’ll see a list of drives connected to the Raspberry Pi and the mount point name of each. Your USB drive will probably be mounted at /media/pi/[VOLUME NAME]. In our case, it was /media/pi/pibkup. If your drive isn’t mounted, try rebooting with the USB drive connected or you can mount it manually by typing sudo mkdir /dev/mysub to create a directory and sudo mount /dev/sda1 /dev/myusb to mount it. However, you can’t and shouldn’t do  that if it’s already mounted.

5. Copy all your data to an img file by using the dd command. 

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

However, if you shrank a partition on the source microSD card, you’ll need to use the count attribute to tell it to copy only as many MBs as are in use. For example, in our case, we had had a 16GB card, but after shrinking the rootfs down to 6.5GB, the card only had about 6.8GB in use (when you count the /boot partition). So, to be on the safe side (better to copy too much data than too little), we rounded up and set dd to copy 7GB of data by using count=7000. The amount of data is equal to count * block size (bs) so 7000 * 1M means 7GB. 

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

use dd to copy your whole microSD card

(Image credit: Tom's Hardware)

Only do this if you have previously shrunk the partition. If you use count and copy less than the full partition you could have an incomplete image that's missing data or won't boot. 

6. Navigate to the USB drive's root directory.

cd /media/pi/pickup

7.  Use pishrink with the -z parameter, which zips your image up with gzip. 

sudo pishrink.sh -z myimg.img

use pishrink with -z option to shrink your Raspberry Pi image

(Image credit: Tom's Hardware)

This process will also take several minutes but, when it is done, you will end up with a reasonably sized image file called myimg.img.gz. You can copy this file to your PC, upload it to the cloud or send it to a friend.  

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.

sudo apt-get install gparted -y

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

Launch gparted

(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. 

Pick a Drive

(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. 

Unmount the Partition

(Image credit: Tom's Hardware)

7.  Right click rootfs and select Resize / Move. 

Resize / Move Partition in gparted

(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. 

Enter new size for partition in gparted

(Image credit: Tom's Hardware)

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

Click gparted checkmark to process resizing

(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. 

 Writing Your Raspberry Pi Disk Image to a Card 

 Once you’re done, you’ll have a file with the extension .img.gz and you can write or “burn” it to a microSD card the same way you would any .img file you download from the web. The easiest way to burn a custom image is to: 

1.  Launch Raspberry Pi Imager on your PC. You can download Raspberry Pi Imager if you don’t have it already. 

Raspberry Pi Imager

(Image credit: Tom's Hardware)

2.  Select Use custom from the Choose OS menu. 

Raspberry Pi Imager Use Custom

(Image credit: Tom's Hardware)

3.  Select your .img.gz file

4.  Select the microSD card you wish to burn it to. 

Raspberry Pi Imager

(Image credit: Tom's Hardware)

5.  Click Write

Raspberry Pi Imager

(Image credit: Tom's Hardware)
Avram Piltch
Avram Piltch is Tom's Hardware's editor-in-chief. When he's not playing with the latest gadgets at work or putting on VR helmets at trade shows, you'll find him rooting his phone, taking apart his PC or coding plugins. With his technical knowledge and passion for testing, Avram developed many real-world benchmarks, including our laptop battery test.
  • aixylinux
    There's a one-step way to create the compressed backup image without requiring an intermediate file.

    cd /media/pi/pibkp
    dd if=/dev/mmcblk0 | gzip -c - > mysys.img.gz

    I did have a problem with the NTFS filesystem on the USB drive being read-only. Fixed it with:

    sudo apt-get remove ntfsprogs
    sudo apt-get install ntfs-3g
    reboot
    Reply
  • ZagrobaAtTom'sHardware
    "If your drive isn’t mounted, try rebooting with the USB drive connected or you can mount it manually by typing sudo mkdir /dev/mysub to create a directory and sudo mount /dev/sda1 /dev/myusb to mount it." One of the /dev/myusb or /dev/mysub needs correction.
    Reply
  • mamasan2000
    I use an adapter for this: https://www.amazon.com/usb-micro-sd-adapter/s?k=usb+to+micro+sd+adapterGet one of these from any shop. Shutdown your RPI, take out the microSD-card. Plug it into adapter and plug adapter to PC.
    I make backup image in Linux because why wouldn't I have Linux installed.

    To find out which device it is on your system, run lsblk, blkid or similar in terminal. You might need 'sudo' before the command. 'sudo blkid', 'sudo lsblk', 'sudo fdisk -l'.
    Another, graphical way, is using Gnome-disk-utility. It's also easy to mount/automount your NTFS partitions if you want. In case you want to move the backup to your Windows partitions. Always save a backup in more than 1 place. You never know when you have to wipe OSes or partitions or the drive just fails.

    It's 1 command: sudo dd bs=4M if=/dev/sdf of=/home/<replace with your username>/rpi.img

    if = inputfile. In this case a /dev which is a Device. Sdf is the specific device, in this case my USB-adapter. Don't add any number after sdf. You want the whole device imaged.
    of = outputfile. Where you want the image saved. Up to you where you put it.

    Here is a guide how to automount your NTFS-partitions: https://www.freecodecamp.org/news/automount-a-storage-partition-on-startup-in-linux/Or any partition/device really. It's the exact same procedure. And it is very easy.

    If you are on Manjaro like me (or any Arch-derivative I imagine):
    'sudo pacman -Sy'
    To update repository

    'sudo pacman -S gnome-disk-utility '
    To install Disks-utility. If you open the utility via Start-menu, it will be called 'Disks'. Works on any Desktop Environment. I am running KDE Plasma. So don't think you have to have Gnome installed or running.

    You can also do this graphically, as in the guide. To me, it just takes too long. Faster via terminal.

    So now you have the image of your RPI. Copy it to a couple places so you'll always have it.


    To image it back to an SD-card, use something like Balena Etcher. You can also use DD but of course reversed. Input file is the image. Outputfile is your device. DD has no failsafes so I would suggest Balena Etcher, so you don't overwrite your OS or anything.
    https://www.balena.io/etcher/It's those 3 steps, as pictured on the website. Select image, select drive, flash. It is that simple. If you are using an adapter like me, it should be a Mass Storage Device with the size of your SD-card, when you go and select drive.
    Reply