Network Backup your Raspberry Pi
The Raspberry Pi is a very useful little computer. We use it as home automation hubs, servers for network services, emulation stations and more. Unfortunately, it uses the rather fragile micro-SD format as its storage device. If you use your Raspberry Pi for anything that writes to the disk a lot. Say something like Home Assistant you may find that early SD card death is a recurring thing. As a result, backups are more important than ever. For this tutorial, we will perform a network backup of a running Raspberry Pi and compress the image.
Pre-Requisites
The Hardware
- A running Raspberry Pi.
- Maybe like the one you setup if you followed our Headless Pi tutorial
- A server to send the network backup to.
- The server should be configured with a shared folder for the backups to be saved to. This varies from platform to platform. But could Windows, Mac, or some flavour of Linux.
The Software
On your Raspberry Pi, not much extra is needed beyond the stock software.
First, we need to ensure the cifs-utils package is installed so we can connect to an SMB share. It’s probably already installed. Still, it doesn’t hurt to run the command to confirm its presence.
sudo apt-get install cifs-utils -y
We can shrink the network backup images so they don’t consume a tonne of disk space on the server. Additionally, they will be able to be restored to SD cards which are not identical in size to the original.
To achieve this we will install PiShrink. A super convenient bash script purpose-built for Raspberry Pi disk images.
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh sudo mv pishrink.sh /usr/local/bin
You must also create the mount directory. For instance, I use a folder named backup.
sudo mkdir /media/backup
The Network Backup Script
I have created a script that will automatically do the following:
– Mount your defined share.
– Check if the backup destination exists and if not creates it.
– Perform the backup.
– Run PiShrink on the images
– Dismount the share
By default, the script creates a subfolder based on the hostname of the Raspberry Pi. Additionally, the image file is named with the current date. Importantly, if the script is run twice on the same day it will overwrite the original.
To install the script run the following.
wget https://raw.githubusercontent.com/sktaylortrash/Scripts/master/pibackup.sh -O- | tr -d '\r' >pibackup.sh chmod +x pibackup.sh sudo mv pibackup.sh /usr/local/bin
Configuring the script
The script assumes you are practicing good security and using username/password-protected folders. For configuration, it contains 4 user variables.
MountPoint – The location on the Raspberry Pi to mount the network share to. In the example, it is /media/backup
ServerName – This is the UNC path to the share. ie. //servername/backup
UserName – The user name for the share on the server
Password – The matching password.
sudo nano /usr/local/bin/pibackup.sh
Edit the variables to match your network configuration.
# User Variables # MountPoint="/media/backup/" ServerName="//servername/backup" UserName="username" Password="password"
Once you have made the appropriate changes.
Press Ctrl + x to exit.
When prompted hit Y then Enter to save and exit
Running the backup
To start the backup simply run.
sudo pibackup.sh
As the network backup begins you will see status messages regarding directory checks and the backup starting. Additionally, you will then see the status of the backup.
The speed of around 12 Mb/s seems to be fairly consistent across the brands of SD cards I tested. Consequently, larger cards of 32 GB or more can talk quite a while to run. Once complete PiShrink will run. Hopefully, once done you will see a significant reduction in space unless your SD card was quite full. In this example, the image shrunk from 30 GB to 3.6 GB
Notes and Problems I’ve Seen
So just a few items I’ve noticed in using this script over the past few months.
- Running the script over an SSH connection is generally fine with a couple of caveats.
- You must keep the session running for the entire duration or it will stop when you disconnect.
- If your SD card is 32 GB in size or larger. You will potentially see timeouts or halting of the backup depending on network traffic and hardware quality. These same timeouts did not occur when running the script from the local shell.
- As mentioned previously larger cards can take a long time. If they are quite full you and won’t benefit from PiShrink you may find it more efficient to just shut down the Raspberry Pi and do a manual image with something like Win32 Disk Imager.
- You should occasionally test your backups by writing the image to a secondary SD card. This can be done with balenaEtcher or the official Raspberry Pi Imager.
- Then attempt to boot your Pi with the backup.
- If all is well and you did the test right after the backup you could even just leave the restored image in your Pi
- If you are using databases like MySQL on your Raspberry Pi. Then a live network backup is a bad idea as it will constantly be writing to disk and you could end up with corrupted database files. For those instances, an offline backup is better.
Conclusion
Hopefully, you find this script useful. I have a half dozen Raspberry Pi’s running various small specialty tasks in my home. So knowing I have quick restore backups available is of significant benefit to me.