How to Back Up Your Raspberry Pi as a Disk Image (2023)

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

How to Back Up Your Raspberry Pi as a Disk Image (1)

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.shsudo chmod +x pishrink.shsudo mv pishrink.sh /usr/local/bin

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

lsblk

How to Back Up Your Raspberry Pi as a Disk Image (2)

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.

(Video) Raspberry Pi OS. Backup, Shrink and Restore to SD, USB or SSD. Raspberry Pi 400, Pi 4.

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

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

How to Back Up Your Raspberry Pi as a Disk Image (4)

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.

(Video) Backup Your Raspberry Pi Image - EASY!

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.

How to Back Up Your Raspberry Pi as a Disk Image (5)

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

How to Back Up Your Raspberry Pi as a Disk Image (6)

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.

How to Back Up Your Raspberry Pi as a Disk Image (7)

7. Right click rootfs and select Resize / Move.

How to Back Up Your Raspberry Pi as a Disk Image (8)

(Video) Backup and Shrink your Raspberry Pi Image

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.

How to Back Up Your Raspberry Pi as a Disk Image (9)

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

How to Back Up Your Raspberry Pi as a Disk Image (10)

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.

How to Back Up Your Raspberry Pi as a Disk Image (11)

(Video) How to copy / clone / back up a raspberry pi microSD card image for retropie and raspbian

2. Select Use custom from the Choose OS menu.

How to Back Up Your Raspberry Pi as a Disk Image (12)

3. Select your .img.gz file.

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

How to Back Up Your Raspberry Pi as a Disk Image (13)

5. Click Write.

How to Back Up Your Raspberry Pi as a Disk Image (14)

Be In the Know

Get instant access to breaking news, in-depth reviews and helpful tips.

(Video) How to Backup & Restore your Raspberry Pi 4 (2020) | Learn Technology in 5 Minutes

FAQs

How do I backup my Raspberry Pi as a disk image? ›

  1. Put the SD card in your computer. Safely shut down your Raspberry Pi and put the SD card into your computer using a MicroSD card adapter. ...
  2. Download a Windows imaging program. ...
  3. Launch Win32 Disk Imager. ...
  4. Choose a location. ...
  5. Initiate the backup. ...
  6. Verify the backup process is successful. ...
  7. How to restore your backup file contents.
Nov 19, 2020

How do I completely backup my Raspberry Pi? ›

HOW TO: Backup Raspberry Pi SD Card
  1. Connect your USB SD card reader to your Pi. You can format your destination SD card if you want but it is not necessary. ...
  2. Locate your Source and Destination SD cards. Your newly inserted USB SD card device will be located under /dev. ...
  3. Run the backup with the dd command.

How do I backup my Raspberry Pi to my computer? ›

Using Win32 Disk Imager
  1. Insert the SD card into your PC.
  2. Download Win32 Disk Imager. ...
  3. Install and launch the Win32 Disk Imager tool with administrator privileges.
  4. Select the location to save your backup files. ...
  5. Click on the Read option to start the backup process.
Sep 22, 2022

How do I create a disk image backup? ›

How to create system image backup on Windows 10
  1. To create a full backup on Windows 10, open the “Backup and Restore” settings.
  2. Click the “Create a system image” option, and select the USB drive to save the backup.
  3. Continue with the directions by selecting the system and secondary drives and clicking on “Start backup.”
Jan 5, 2023

How do I backup a disk image? ›

Type "control panel" in the Windows Start Menu and select the "Control Panel" app. Select "Backup and Restore (Windows 7)" (the function works on Windows 10 and 11 as well) Select "Create a system image" from the left pane. Choose where to store the backup - on an external drive, a DVD, or a network location.

How do I backup my entire system? ›

Back up your PC with File History

Select Start​ > Settings > Update & Security > Backup > Add a drive , and then choose an external drive or network location for your backups.

What are the 3 ways to backup? ›

3 Methods for Computer Backup
  • Use an External Hard Drive. One of the simplest ways that you can backup your computer is by purchasing an external hard drive. ...
  • Backup Your Files Online. ...
  • Use a Cloud Storage Service. ...
  • Back It Up.
May 10, 2017

How do I backup my data forever? ›

So what's the best plan?
  1. Make regular backups. Back up your devices on a regular schedule. ...
  2. Make archives. ...
  3. Make copies. ...
  4. Store your archives in a cool, dry place. ...
  5. Request regular backups of your social media activity. ...
  6. Convert documents and media out of proprietary formats. ...
  7. Consider encrypting your archive.
Mar 15, 2014

What is the easiest way to backup my computer? ›

An external USB hard drive is the fastest and most cost-effective way to back up your files at home.

What backup program for Raspberry Pi? ›

An easier approach is to use a free program called Déjà Dup. This automates rsync and gives it a user-friendly interface. It's an easy program to use, and you can back up your Raspberry Pi using Amazon S3, SSH, FTP, or by copying the files directly to a flash drive.

How do I create a disk image of an SD card? ›

How do I create a disk image from SDHC or Compact Flash cards?
  1. Insert the SDHC or CF card into a USB card reader and connect the reader to the computer. ...
  2. Open Applications > Utilities > Disk Utility.
  3. In the left window pane, select the memory card.
  4. Select New Image from the top tool bar.

How do I turn my Raspberry Pi into a NAS whole home file sharing? ›

Tutorial: how to set up Raspberry Pi as NAS?
  1. Step 1: download and install OpenMediaVault. ...
  2. Step 2: start the Raspberry Pi NAS and change the keyboard layout. ...
  3. Step 3: change password and display IP address. ...
  4. Step 4: logging onto the web interface. ...
  5. Step 5: securing the web interface.
Jan 28, 2021

How do I Copy a Raspberry Pi SD card image? ›

From your Raspberry Pi's desktop, go to Accessories and open the SD Card Copier utility. Select the main MicroSD card in the “Copy from Device” dropdown menu. Select your new MicroSD card in the “Copy to Device” dropdown menu.

How do I transfer data from Raspberry Pi to computer in real time? ›

What Is the Best Way to Transfer Files from Raspberry Pi to PC?
  1. Send data using email.
  2. Sync via cloud storage.
  3. Transfer data from your Raspberry Pi with USB.
  4. Swap data from your Raspberry Pi over SSH.
  5. Use your PC's FTP client to transfer data to Raspberry Pi.
Sep 30, 2020

Videos

1. How to Create a Backup Image for a Raspberry Pi
(Marcos Alonso)
2. Easiest Raspberry Pi Backups
(KM4ACK)
3. Raspberry Pi SD Card Image Backup
(Mark Dillner)
4. Full PC Image Backup with Raspberry pi / UrBackup
(Novaspirit Tech)
5. How to Create a Back-up Image of Raspberry PI OS
(IOT Connectivity Simplified)
6. Backup of Raspberry Pi SD-Card to an image on Mac
(Pixelfriedhof Tutorials)

References

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated: 10/04/2023

Views: 6028

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.