How to Backup your Raspberry Pi SD Card

In this guide, we will be showing you various ways on how to backup your Raspberry Pi and also how to restore it.

Backup your Raspberry Pi SD Card

Backing up is a crucial task that you should often be doing, especially if you make a lot of changes or are storing data on it.

One thing to note is that some backup methods are going to be way more thorough the others, for instance backing up your Raspberry Pi SD Card image is going to be more reliable than just backing up all the files to a USB device since the image is a replication of all partitions on the SD Card.

We will be exploring two different methods of backing up your Raspberry Pi in this guide, the two different methods that we will be showing you how to do is the following:

  • Backing up your Raspberry Pi to a computer using an SD Card Reader
  • Backup the Raspberry Pi to a USB device

Personally, we recommend backup to a computer as it is the most robust method. Mainly since the Raspberry Pi doesn’t have the best USB speeds or network speeds, so creating backups can take considerable time and put a fair bit of stress on the Raspberry Pi’s limited resources.

As a bonus, the backup we make using a tool like win32diskimager is a 1 to 1 copy of the SD Card meaning every byte should be the same.

Backing up the Raspberry Pi is an important step in making sure you don’t lose any data when you go to make big changes to your system. This backup process is important if you have stuff that you would prefer not to lose if you’re looking at doing a complicated Pi project, upgrade or something else to the operating system.

Table of Contents

Backing up your Raspberry Pi SD Card

To start off, we are first going to show you how to backup your Raspberry Pi SD Card as an image. To do this tutorial, you will need to have an SD Card reader handy.

To begin this tutorial, please first turn off your Raspberry Pi by running the following command on your Pi’s terminal:

sudo shutdown now

Once it has shut down, disconnect the power and remove your SD Card. Place the SD Card into your SD Card reader and proceed onwards with this section of the guide.

This section of the guide will be split into three parts, one for Windows, one for Mac OS X and one for Linux based systems.

Backing up your Raspberry Pi SD Card on Windows

1. To back up our Raspberry Pi’s SD Card on Windows we will be utilizing the imaging tool that is called win32diskimager.

Win32diskimager is an incredibly useful tool that can read and write images to USB Sticks or SD/CF Cards. It is a tool many will be familiar with since it is used often to write the initial Raspbian image to your Raspberry Pi.

We will start off this section of the guide by downloading and installing this tool by going to win32diskimager’s homepage.

2. With win32diskimager now installed, open it up. You should be greeted with a screen as we have shown below.

For now, we need to make sure the right drive has been selected by clicking the drop-down box under “Device:” (1.) and ensuring the correct drive letter of our SD Card is selected.

Afterward, click the folder icon (2.) that is direct to the left of the device drop-down box.

Selecting drive to backup

3. You will now be shown a file selection screen. However, we won’t be selecting any file. Instead, navigate to the folder you want to keep your backup file.

Once within this folder, type in the name you want to give your backup file in the text field (1.) at the bottom as shown in the screenshot below.

Make sure you have “.img” at the end of the name you choose to use. For example, we named our backup file “RaspberryPiMyLifeUp.img“.

Once you have typed in your file name, you want to utilize, click the Open button.

Backing up Raspberry Pi Selecting a disk image

4. Finally with our device set and our new filename set we can begin the backup process.

To do this, we need to clickthe “Read” button.

Please note that this can take some time as it is a complete backup of your SD Card, meaning every single byte is replicated.

Raspberry Pi Backup SD Card

Restoring your Raspberry Pi Backup on Windows

1. Now when it comes the time that you need to make use of your full SD Card backup we will need to make use of win32diskimager once again.

Before opening the software, make sure you have inserted your SD Card into an SD Card Reader that is plugged into your computer.

With the win32diskimager software now opened, let’s click the drop-down box that is located under the “Device” (1.) header and ensuring that the correct drive letter of our SD Card is selected.

After you have set the device, we can now, click the folder icon (2.) that is located to the left of the drop-down box.

Selecting drive to backup

2. Now that we have the file browser loaded up let’s find the backup file that we created earlier.

Once you have found it either, double-click the file, or single click it and click the “Open” button.

3. With our backup image now selected, we can now proceed with writing the backup file to the SD Card.

Please note this will wipe everything currently on the SD Card so make sure that you have the correct device selected.

When you are happy you have everything correct, click the “Write” button.

Raspberry Pi write backup image

4. Once the writing process has completed, you should now have an SD Card that is in the exact state of when you originally made the backup.

Thanks to the full image backup it means all partitions on the card are restored.

Backup up your Raspberry Pi SD Card on OS X

1. With your SD Card inserted into a card reader on your Mac, we can begin the process of making a full image backup of your Raspberry Pi.

Unlike Windows, we can easily use the terminal to do this.

To proceed with this tutorial, start off by opening the Terminal application.

2. With the Terminal application now open on your Mac device, we need to utilize the following command. This command will display all available disks on your device.

diskutil list

Within this list, look for your SD Card by looking for a disk that is about the size of your SD Card.

For instance, with a 16gb SD Card, you should be looking at a “_partition_scheme” of about 16gb. You will also notice that there is likely a partition called boot.

Once you have found your SD Card in this list, take notice of the mount location. For instance, my own SD Card was under “/dev/disk1“.

3. Now still within the terminal on your Mac, we need to utilize the following command.

This command will basically create a copy of your SD Cards image, and save it to your home directory as “PiSDBackup.dmg” (The file format being a disc image)

Make sure you swap out “/dev/disk1” with whatever you found when using the “diskutil list” command.

sudo dd if=/dev/disk1 of=~/PiSDBackup.dmg

This command can take some time to complete as it requires reading the entire SD Card to the disk.

The command also provides no feedback on how far along it is, so please be patient and wait for the prompt to enter another command to reappear before removing your SD Card from your Mac.

Restoring your Raspberry Pi Backup on OS X

1. Now that you need to restore your Raspberry Pi to its backup we need to use the “diskutil list” command to find our SD Card. Remember as before, take note of partition sizes to find your SD Card.

diskutil list

2. Now before we can write to the SD Card, we will need to unmount it. The reason for this is that OSX will attempt to write to it at the same time, unmounting the SD Card prevents this from happening.

Run the following command on your Mac device to unmount the card, again making sure that you replace “/dev/disk1” with the one you found using the “diskutil list” command.

diskutil unmountDisk /dev/disk1

3. Finally, we can now write the image back to the SD Card, and please be prepared for this to take some time as it involves rewriting the entire SD Card.

Remember to change out “/dev/disk1” with the mount location you grabbed using the diskutil list command.

sudo dd if=~/PiSDBackup.dmg of=/dev/disk1

Like reading the SD Card to a disk image file, the process of writing the image also takes a long time.

As an added note, the dd tool doesn’t show any writing progress so please be patient and wait till the enter command prompt reappears.

4. Once the writing process has completed we can now eject the SD Card from the Mac so we can continue using our Raspberry Pi Backup.

To eject the SD Card, we will need to utilize the command below.

sudo diskutil eject /dev/rdisk3

Your Raspberry Pi’s SD Card should now be in the same state as when you made the original backup.

Backing up your Raspberry Pi SD Card on Linux

1. Before starting to backup up your Raspberry Pi’s SD Card on Linux we will first run the df command with the reader not plugged in.

The reason for this makes it much easier to see which device is which.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot

2. Now insert your SD Card reader back into your Linux computer and again rerun the following command, but this time take note of the additional entries.

df -h

This command will return something like what you got above, but with an additional entry, in our case “/dev/sda1” is that additional entry, with your entry, remove the partition number.

For instance, “/dev/sda1” will become “/dev/sda“. We need to do this as we want to write to the entire drive and not just a singular partition.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot
/dev/sda1       3.7G   75M  3.6G   3% /media/boot

3. We can now utilize dd, the same tool we make use of on Mac OS X since it is also built on Unix.

We can utilize the following command to begin dumping the SD Cards image to our home directory.

Make sure you replace “/dev/sda” with the filesystem that you grabbed using the “df -h” command.

sudo dd if=/dev/sda of=~/PiSDBackup.img

The process of backing up your Raspberry Pi can take some serious time, so be patient and wait.

The dd tool provides no feedback, so you will have to wait until the input command returns to your terminal.

Once it reappears, you will have successfully backed up your Raspberry Pi.

Restoring your Raspberry Pi Backup on Linux

1. Now that you have made a backup of your Raspberry Pi you will want to at some stage make use of this. To do this, we will need to again go through the process of finding out the location of our filesystems.

Use the “df -h” command like we did in the first segment of this guide, though this time you might have more than one partition pop up for your SD Card such as “/dev/sda1” and “/dev/sda2“.

Take note of all new entries as you will need to unmount all of them.

2. Now that we have all our partition locations ready, we can unmount each of them by running the following command for each one.

Switch out “/dev/sda1” for the locations that you got in the previous step.

sudo unmount /dev/sda1

3. With all the partitions now unmounted let’s write our backup image to the SD Card. We can do that by running the following command. Remember swap out “/dev/sda” with your devices mount location.

sudo dd bs=4M if=~/PiSDBackup.img of=/dev/sda

Backing up your Raspberry Pi to a USB Drive

1. Before plugging in your USB device that you want to keep the backups on let’s first run the following command to find out the current filesystems that are available to us.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot

2. Now insert your USB Device into your Raspberry Pi and run the following command, take note of any new entries that pop up.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot
/dev/sda1       3.7G   75M  3.6G   3% /media/pi/MYBACKUPDRIVE

3. With our USB device showing on the list, we need to look at the “Mounted on” location. In our case, this is “/media/pi/MYBACKUPDRIVE“.

This directory will be the location that we will write our backup images. So, make a note of it for later in the tutorial.

Now with our backup location now handy we can download the backup script that we are going to use, this script was written by a user on the Raspberry Pi forums called Jinx.

Let’s clone the script to our Raspberry Pi by running the following two commands.

cd ~
git clone https://github.com/lzkelley/bkup_rpimage.git

4. With the script now saved to the Raspberry Pi, we can start to make use of it. We can do an initial backup by running the command below on our Raspberry Pi. Make sure you replace “/media/pi/MYBACKUPDRIVE” with your own mount location that you grabbed in the previous step.

sudo sh bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_backup.img

This script will create a dummy image then launch a rsync process to copy all the files from the system to the dummy image.

Please note that the initial backup can take up to an hour to complete.

5. Now that we have created our initial backup file and know that the script is working as intended we can move onto automating the backup. To do this, we will be making use of cron jobs.

One thing to decide on how is whether you want an incremental backup or multiple backups. An incremental backup just updates the original backup and doesn’t generate a new file.

Run the following command on your Raspberry Pi to begin editing the crontab.

sudo crontab -e

6. In the crontab editor, add one of the following lines to the bottom of the file.

This process will make a backup every day. If you want to edit the cron timings, you can use our Crontab tool work them out easily.

Incremental Backup

0 0 * * * sudo sh /home/pi/bkup_rpimage/bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_backup.img

Multiple Backup

0 0 * * * sudo sh /home/pi/bkup_rpimage/bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_$(date +%Y-%m-%d).img

Now save the file by pressing CTRL + X then pressing Y and then hitting ENTER.

7. You should now have an automated backup system up and running that will continually backup your Raspberry Pi to your USB device.

To restore these images follow our Restoring guides located in the SD Card section of this guide.

Conclusion

I hope that you now know how to backup your Raspberry Pi SD card on both your main system or backing up directly to a USB device.

You can now continue with working on some Raspberry Pi projects or just using your Pi as you would normally. If you run into any issues or have any feedback, then feel free to drop a comment below.

Leave a Reply

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

17 Comments

  1. Avatar for Michael
    Michael on

    Hello experts – I want to create images so I can recover when my attempt to make a WiFi hotspot or DNS goes wrong but, when I plug my SD card into my laptop (HP ENVY) using an adapter, I see four drives appear:

    G: (labelled ‘Removeable Disk’) & J: (labelled ‘SDHC’) which it tells me are not accessible and need formatting

    I: which it labels ‘boot’

    F: which it labels ‘RECOVERY’

    How can I create an image of the SD card from this mess, any ideas?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi,

      The win32imager software should backup the entirety of the drive uncluding all of the different partitions of the drive.

      Each of those “drives” are just a partition on the single drive, the one you cant access is where Raspbian itself sites, its formatted in the ext4 format which Windows does not support..

      Cheers,
      Emmet

  2. Avatar for Andrea
    Andrea on

    Thank you for this guidelines!
    I think I have find out an error in section Backing up your Raspberry Pi to a USB Drive point 6. In both crontab lines there is a dot before /home that shouldn’t be there:

    0 0 * * * sudo sh ./home/etcetc... should be:
    0 0 * * * sudo sh /home/etcetc...

    Regards, Andrea

    1. Avatar for Gus
      Gus on
      Editor

      Thank you for pointing that out, we have now updated the tutorial.

  3. Avatar for Sleeba Paul
    Sleeba Paul on

    You can get the status of `dd` by tapping ctrl+T in Mac

  4. Avatar for gbo
    gbo on

    On linux you can add the status=progress option to the dd command
    as in:
    dd bs=4m if=… of=… status=progress

  5. Avatar for Sean
    Sean on

    Great stuff!

    I use dd on one of my Pis to copy an entire SD card to a Mac, or Linux box, over an sshfs mounted filesystem over WiFi. Although I’m terribly restricted by the speed of the WiFi connection, it does mean I don’t have to take the Pi down in order to do a full SD card backup.

    This suits that particular Pi, as no data are stored on the Pi as all data, once generated by an ADS-B receiver, are transmitted directly to the consuming APIs. No local data storage needed nor used.

  6. Avatar for RLPendergast
    RLPendergast on

    Thanks Gus for a very valuable presentation.

  7. Avatar for Harald
    Harald on

    Thanks for this! Your tutorials are great, keep up the good work! I don’t see a device like /dev/sda . All I see are the 3 logical partitions.

  8. Avatar for Ash
    Ash on

    How to restore when using bkup_rpimage.sh ?

    1. Avatar for Mashrafi Rahman
      Mashrafi Rahman on

      As Gus showed for the different operating systems, I’m sure you can use the dd command to restore the rpi_backup.img file.

      For macOS:

      diskutil unmountDisk [disk]
      sudo dd if=[image] of=/dev/[disk]
      sudo diskutil eject [disk]

      You can find the disk path using diskutil list. You can see the progress by pressing control and T together.

      For Linux:

      sudo unmount [disk]
      sudo dd bs=4M if=[image] of=[disk] status=progress

      I hope this helps!

  9. Avatar for petakcore
    petakcore on

    How to backup noobs version? can’t use win32diskimager

    1. Avatar for Gus
      Gus on
      Editor

      The method of backing up a SD card is exactly the same if you used NOOBs to install the OS. Why can’t you use win32diskimager? There are other methods if you have an operating system that’s not Windows.

  10. Avatar for D.Duncan
    D.Duncan on

    Couldn’t of come at a better time, thanks, Gus! You’ve simplified an important process.

  11. Avatar for Bill Dornbush
    Bill Dornbush on

    Although Win32DiskImager does a fine job of copying an SD card, it makes a full sized copy so it takes as much space on your hard drive as the SD card size. There is another utility that I have used, HDDRawCopy that saves a compressed image. I have seen a 16GB image compressed to 3-5GB, I assume because of empty space on the card. I have been able to restore successfully from the compressed image.

    Also, Raspbian Jessie and later include a utility, SD Card Copier, that will copy the current SD card to another SD card. I insert the microSD card in card reader that plugs into a USB port to do it.

    1. Avatar for Piznti
      Piznti on

      thank you. i been looking for something like this for a long time. i have a lot of 32 gb images on my computer im going to replace now.

    2. Avatar for Petar
      Petar on

      Thank you very much, HDDRawCopy is an excellent tool for imaging.