Backing up the pi

Backing up your Raspberry Pi

You got a nice project running when suddenly krrsttrsrs, your SD card fails. Disaster has struck. Now you need to begin all over again, and s**t you can’t remember exactly how you’ve done things.

Now how could we have prevented this from happening?
SD cards fail due to constant writing to it, so if its possible you could make it read only.
But that is out of scope for this tutorial and will be covered in a future one.
What we are going to do will not prevent the SD card from going corrupt, but it’ll help you getting your project back running in no time.


What do you need for this to work?
Well next to your Raspberry Pi project you want to backup you’ll need the following tools:

  • Spare SD card + SD card reader (Micro if you’re not using the RPI1) (live backup)
  • SD Card reader (offline backup)
  • USB drive (live backup V2)

Backing Up

There are 2 major was of backing up your pi and it all depends on what tools you have around.
Most of you will have an (micro) SD card reader as you need this to burn the SD card. This makes it possible to use your PC as the Backup station.

But what is that other option? You can do a live backup from your Pi.
Lets see how that is done too.

We’ll be using a headless Pi, that means a pi without a screen. and running the lite version of the OS. so there is no X server available to us.

Pro tip: You can combine both methods so you don’t need to shutdown your beloved pi!

Live backup method

SSH into your pi, if you are using something like MobaXterm you actually can use graphical programs from the pi, fin out how that works here.

For a clone of you SD to another one you will need one with the same or higher capacity of the original one. Note that not every 16Gb SD card is the same (unless its from the same brand/manufacturing process) so get one that’s identical of bigger.

For this to work we need an extra package installed.
In the desktop versions of the Raspberry Pi OS this package comes pre-installed and is available in the GUI.

sudo apt install piclone gvfs -y

Do note that in the GUI it’s called SD Card Copier. Also note that gvfs is a required package for piclone but is not in the dependency list. This means we have to install it manual.
When installed we will start this program from the terminal (as we are running the Pi headless). Before we can do this we need a program that accepts there inputs like MobaXterm.
Open the SSH using the -Y or -X option. This options tells the SSH to open with an extra graphical layer. When we have opened the SSH we can execute the program remotely.

piclone &

The & makes a program run in the background, keeping the terminal free to use for other purposes. You can see in the image that it is possible to open the program on your desktop but that is running on the Pi.

Altought this popups the required window, this won’t work as we need to run it under root.
So lets try that:

sudo piclone &

Well that didn’t work out did it?
The problem is that the root user doesn’t have permission to use the Xorg window.
We need to run a few command in order to get it running.

xauth list | grep unix`echo $DISPLAY | cut -c10-12` > /tmp/xauth
sudo su
cat /tmp/xauth

In most cases there will be only one entry, but since i changed the hostname after use the old one is still there, copy the correct line and put it on the end of the following command
‘xauth add’, for example:

xauth add testpi/unix:10  MIT-MAGIC-COOKIE-1  4c7e5d87fd582363645ac6256ea15397

Note that its a single line.
Once done reboot the Pi and try agian

sudo piclone &

Now we are there yes?
Well no as you probably can see we can’t select any drives. that isn’t convenient at all. So lets solve that problem.

sudo dbus-launch piclone

Here we go, we finely can copy the SD card.

Piclone SSH startup

Select the drive you are copying from and the drive you want to get the copy to.

Press start and wait for the magic to happen.

One problem with this one, you need to do every single step each time over again.
That’s why we scripted it for you:

sudo chmod +x ./
sh ./

Shrinking your Image

When you made a copy of your SD card it’ll be the same size as the original one.
But that means you are (for example) wasting 16GB of space on random ones and zero’s.
Just a waste of space if you ask me.

So how do we shrink the image?
By default the dd file copy process makes an image from ALL the space on the drive.

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, and save a lot of space in the progress.

We’ll use a second SD card and the clone process described above. Once completed we use that new SD card as the basis for the shrinking.

Before we can continue we’ll need to install Gparted.

sudo apt install gparted -y

We’ll start gparted from the SSH terminal like we did with piclone before.
Afterwards we’ll download the bash script and run it.
As before this bash script activates root’s permissions to use Xforwarding on SSH and it starts gparted.

sudo chmod +x

Once gparted has started we’ll select the drive we want to shrink the partition of.
In this case its /DEV/SDA.
Now we press the ‘Resize/Move’ button.

This will open a new windows where we can change the size and location of the partition.
We’ll crop the partition size to around 4GB in this occasion, press the ‘Resize/Move button and ‘Apply’ the changes.
Then we’ll wait for the changes to take effect, lets take a break.

So we made it, the size of our image can now be reduced from 32GB to around 4GB.
When we copy this to another drive we’ll save 28GB of space. Ain’t that nice?

Lets insert your backup drive and make an image of your pi.
In my case the new drive identifies as /DEV/SDB.
You can check how your drive is called using fdisk:

sudo fdisk -l | grep /dev/s

Lets mount it and create our image using the ‘dd’ command.
For ease of use we’ll mount it on ‘/mnt’

sudo mount /dev/sdb1 /mnt
sudo dd if=/dev/sda of=/mnt/pibackup.img bs=1M count=4000 status=progress

Lets talk about the dd command while its running. What are all the options we use?
The 2 first options used define the input and output file (if and of). Next is the bs option, this is the Block Size and the count option or the amount of blocks we’d like to copy over.
Last but not least is the status option, we chose for progress giving us an update on where the copy is.

4000 counts means that in this case we’ll make an image from the first 4GB of data.

dd command

So lets take a free SD card and burn this new image on it and check out if it worked.
To do this we swap the of and if options and change the /dev/sda to the new location, for example:

 sudo dd if=/mnt/usb/pibackup.img of=/dev/sda bs=1M status=progress

Image backup Offline

As said before you could do a dd in any linux machine.
We’ll only cover Windows programs here as we already have discussed the linux way.

The Raspberry Pi Foundation didn’t include a way to backup an SD card unfortunately so we’ll use a 3th party program.
The program we are using is called Win32 Disk Imager. Follow the link and download.

Install the files, insert your SD card and start the program.
Once started you browse for the location where you want to backup to and give it a proper name (don’t forget the extension, it won’t be added automaticity). Select the correct drive where to read from and finely press the read button.

Win32 Disk Imager

Wait and have fun.


We have seen 3 ways of backing up your pi.
There is not one best way of doing it, find what suits you best.
There may be other ways to accomplish it, if you have a better way then don’t hesitate to share.

1 comments On Backing up your Raspberry Pi

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.