Dirt-cheap client-encrypted online backups with Raspberry Pi
To be useful to me, backups must be:
- Stored in a reliable and offsite location
- Readable only by me (client-side encryption)
- Cheap
- Automatic
We can achieve all of these goals with a combination of tools:
- Duplicity
- Google Nearline
- GPG
- Raspberry Pi
I have been using this setup for about a year, and it costs me about $2 a month for about 100 GB of backups.
The Raspberry Pi is very useful because it uses very little power and can backup my Network Attached Storage (NAS) automatically over the network.
Aside: backing up a computer
You should strongly consider using Backblaze1. It’s cheap, has no storage limit, and lets you use your own encryption key. It’s going to be the simplest and most reliable bet if you only need to backup the computers you use frequently, instead of your NAS.
Backing up your NAS, or multiple computers
There are five basic steps we need to get backups running:
- Set up the storage service: Google Nearline
- Set up the connection to the files to be backed up: sshfs
- Set up encryption software: GPG
- Configure the backup software: Duplicity
- Run the backups on a schedule: cron
Set up Google Nearline
Go to the Google Developer’s Console, and sign up as necessary. You may need to enter billing information. Create a new project on that page, perhaps called duplicity-backups
.
Click on the duplicity-backups
project and click the hamburger menu (three lines) button in the upper left corner and select “Storage” under “Storage.” Press “Create bucket” and choose a name (perhaps “photos”) and select “Nearline” for “Storage class.”
The last piece needed is access credentials for this storage bucket. Press Settings on the left, then click Interoperability. Create a new key, then copy down the Access Key and Secret shown. We will use these later.
Mounting via SSH
You will need to access the files you wish to backup (likely located on your NAS) over the network. I will assume that they are reachable via SSH. If they are not, you will need to mount them on the filesystem in a similar way (perhaps with NFS).
Run sudo apt-get install sshfs
to make mounting drives over SSH possible.
It will be easier to connect to your NAS via SSH if you use passwordless authentication with a public/private key pair. Run ssh-copy-id nasuser@mynashost
to copy it, substituting in your actual NAS information.
Run mkdir ~/nas
to create a place to mount your NAS directories.
Add lines like this to /etc/fstab
so your Raspberry Pi can treat the remote host as a drive:
And test it with sudo mount /home/pi/nas/Photos
, verifying that the files appear in that directory as expected.
I recommend that your create a second partition directory on your NAS (separate from that which you wish to back up) to store Duplicity’s local cache and log files. Otherwise, you are likely to quickly fill your Raspberry Pi’s local storage. This directory is called “write” in my examples. Run touch /home/pi/nas/write/.useThisWriteDir
after it is mounted to use with the script below.
Set up GPG and your encryption keys
GPG is supported by Duplicity for encryption, and provides a high level of security.
Run sudo apt-get install gnupg
. Then run gpg --gen-key
and follow the prompts, choosing the defaults. Be sure to choose a long (ideally random) passphrase and to write it down (preferably in a password manager). You won’t be able to read your backups without the generated keys, so be sure to back that up as well.
We will need the fingerprint of the newly-generated key to tell Duplicity to use it. Run gpg --fingerprint
to see it. gpg --fingerprint | grep pub | grep -P "(?<=/)\\w{8} "
should highlight the 8-character fingerprint you require.
Set up Duplicity and the backup script on your Raspberry Pi
These instructions assume you use Raspbian. They should be adaptable for use on other Linux (or Linux-like) systems.
Install duplicity by running sudo apt-get install duplicity
.
A simple backup script is needed to store credentials and run the backup. I store it along with the files I wish to backup (Photos), but you could do something more secure instead.
Run the backup script on a small test directory to make sure it’s all set up properly.
Scheduling backups with cron
The last step is to run this backup automatically. Cron can do this for us, and is built-in.
We will use a simple perl script to keep things from running multiple times. Download it to /home/pi
and make it executable with chmod u+x /home/pi/solo.pl
.
Run crontab -e
and add these lines:
This will start a new backup as soon as the last completes. This works well for my use case, adjust as necessary.
Be sure to test your backups to ensure you can restore in a disaster! duplicity verify
may help you here.
Future improvements
This could be improved with emails about failed backups, or when backups haven’t run for some time. The overall process could also be simpler. Ideas? Let me know in the comments and I can update the article with them!
-
I have no affiliation with Backblaze. ↩