Since owning a couple of Raspberry Pi’s I have always wondered if the SD card failed, would I have to either install all the apps again that I had initially put on there or even worse re-create any scripts I had on there.

If your like me, and don’t particularly care about redoing these things, I have a really easy solution that you can use to help not only automate the backup process but literally not even have to worry about it after you have it setup.

So on all build of Raspbian you have an inbuilt toolset. It just so happens that there is also an inbuilt tool called DD. Its primary purpose is to convert and copy files, but in our case an entire SD Card. See where I am going.

However for this to work in my environment I had a couple of requirements.

  • Needs to be automated - Cron job compatible
  • Creates an img file that can be directly re-written again to a new SD card in case of a failed sd card
  • This img file can be written across the network and stored on a NFS drive
  • Must be backed up to the cloud
  • Must be able to trigger from a primary backup server

To demonstrate the following concepts I have in place the following devices:

  • LinuxBackupServer(Ubuntu Server 18.04) - Key purpose is to store the SSH key internal certificate and to trigger the ssh backup commands remotely to each Raspberry Pi. This is a VM in my lab which is backed up, however this can also represent another device that can support SSH and the ability to run remote commands via the protocol.
  • Raspberry Pi - So I have 3 in my environment, generally these are imaged up with raspbian, but should work with most version of the os you can place on the Raspberry Pi. As an example I have Octoprint, UPS Nuts server and RetroPi running on my three.
  • NFS Share - In my case I have a Synology NAS that I have setup. I have created a shared folder called backup. This shared folder is enabled to allow NFS requests based on an IP basis.

So with those requirement in mind this is the solution I came up with to tackle this problem.

1. Create SSH Keys:

First we need to create ssh keys so that we can trigger remote ssh commands without requiring a password. This is required to enable the script to run without any user input.




On LinuxBackupServer type in the following command:


Press enter for each step it asks for.

What this has done is created two files;

  • id_ras

in the following directory: /home/kanderson/.ssh/*

Now that those files have been created, we need to copy the to the Raspberry Pi we want to backup.

Run the following command:

scp .ssh/ user@RASPBERRYPI:

NOTE: At the end of the script a colon has been used to indicate to the command that the file needs to be placed into the users home directory.

Log onto the Raspberry Pi. This can be done by the LinuxBackupServer by using SSH or if you have a keyboard and monitor connected use that.

On the Pi create a folder called .ssh

mkdir .ssh

We now need to copy the file to the .ssh file. We will need to move it, but we will also need to rename it as well.

Run the following command:

mv .ssh/authorized_keys

Now lets test it out, and make sure its all functional.

Log into your Raspberry Pi from LinuxBackupServer and see if you get prompted for a password.

You may be asked to type yes for a new connection. If this is the first time your connecting in this method to the raspberry pi, by typing yes writes an identity into the .ssh file for the raspeberry pi. Later connections you will not see this message.


2. Map NFS Drive

The next step is to map a NFS drive so that we are able to store the backup IMG file on the Synology Nas. I will be making an assumption here that you have successfully enabled NFS on the Synology NAS.

NOTE: In this example it you do not need to use a NFS drive. I choose this because of my NAS which gets backed up. But you could use a different storage solution like a USB stick, external hard drive etc.

To mount a USB device to your Raspberry Pi for this backup see here. The USB size must be above the size of the SD card you have used.  If you have used a 16GB MicroSD card you would need to use a 32GB or bigger.



Firstly we need to install nfs-common. To do this, run the following command:

sudo apt install nfs-common

We now need to create a storage folder for the NFS drive once mapped. The purpose of this drive is to create a space that the mapped drive can live in. In this example I called the folder “backup”.

sudo mkdir /media/backup

Change the permission values on the folder.

sudo chmod -R 755 /media/backup

We now need to tell the raspberry pi host where to find the NFS share. This is done by modifiying the fstab file located in etc.

sudo nano /etc/fstab

This will open up the fstab file using nano.

Once you have made your changes press Ctrl + X, then Y for yes and then enter to save.

Now to test to see if we have successfully mapped the drive, we can test it out by entering a SH terminal.

Type in the following commands:

sudo sh

mount -avt nfs


In yellow, I have highlighted the commands entered. In green, I have added the sections to note. Here we can see that we successfully mapped the NFS drive.

At this stage we are now ready to test writing the IMG file to the NFS Share.

3. Test Backup with DD

If you don’t care too much about automating this process and your happy to do this manually when you need, then this is it. You can run this command as many times as you want to backup the Raspberry Pi.



We first need to determine what the name of the storage is. Going back to the raspberry pi or ssh’ing in run the following command:

sudo fdisk -l

Generally you can use the size of the SD card you have in your Raspberry Pi along with the data at the very bottom of what was returned. In my case the mmcblk0 device is my SD Card.

However you can also have sda as well.





Once you have identified your SD card you can now run the following command from LinuxBackupServer:

ssh pi@RASPBERRYPI sudo dd if=/dev/mmcblk0 of=/media/backup/MyPi.img ibs=4096 conv=noerror status=progress


Note: As mentioned above, if you did not use a NFS share to backup to, you can also use a USB drive. See here to show steps on mounting a

USB drive to your Raspberry Pi. Just remember that the size of the USB must be bigger then the size of the MicroSD card you have used in the Raspberry Pi.

And that’s it! Check where you saved the IMG file and you should now have an image called MyPi.img sitting in the /media/backup/ directory.

You can now simply use Etcher to re-burn the exact image back onto a new MicroSD card and everything will be exactly the same as you had before.

4. Automation

Coming soon.  Basically will be a cronjob that runs but trying out a couple of other things like Ansible and Jankins etc. So will see if there any better to trigger these types of jobs.