In the modern cloud-computing enabled tech landscape there are many services available to backup your computer to the cloud. However, many of these are expensive, overbearing, and can be questionable on how secure your data will be. Most people are not really storing anything that is super secure but it is still nice to be in control of where, and how, your data is being stored.
Raspberry Pi computers are very small, lightweight, and cheap. They are simple to setup on a local network and with the addition of an external hard drive can serve as secure backup system. In this article we are going to go through how to setup a Raspberry Pi on a local network and have local machines back up to it. This example will be based on a Linux desktop but a similar process works for MacOS and Windows.
- Raspberry Pi (There are many versions available but the 3 Model B is probably your best bet): ~$39
- A 1 TB SSD: ~$85, or a 1 TB HDD: ~$45
- Hard Drive Disk Enclosure (Connects to USB for the Pi connection): ~$13
For the sake of brevity we are going to assume that you already know or learned how to setup the Raspberry Pi. Make sure to plug it into ethernet instead of Wifi to get the fastest connection. After setting it up as normal install the SSD into the enclosure and plug it into one of the USB ports on the Pi.
Formatting the new drive
Next either: connect your Pi to a monitor and keyboard, or SSH to it from another machine on your network. Then confirm the location of the drive you just plugged in:
sudo ls -l /dev/sd*
You should see that a device is located on /dev/sdX (where X is either a or b depending on your setup).
parted command on the drive to confirm its total size is what you expect:
sudo parted /dev/sdX unit MB print
This should output something close to 1 TB. Depending on the drive some show up as smaller than that exactly. Copy the total size in MB that is displayed as that will be needed later.
Now we need to create a new partition on the drive which will contain the entire drive.
Note: This next step will completely wipe the drive so make sure there is nothing on it you want to keep.
sudo parted /dev/sdX mklabel gpt sudo parted /dev/sdX mkpart primary 1 <the size copied from above> sudo mkfs.ext4 -L backupdrive /dev/sdX1
Auto-mounting the backup drive
In order to access the drive after restarts or power-failures, its important to auto-mount the new drive.
sudo mkdir /backupdrive sudo nano /etc/fstab // inside of nano add the following to the bottom of the file LABEL=backupdrive /backupdrive ext4 user,rw 0 0 CTL+X to exit sudo mount -a
Create the backup directory
For the first client system backup create a directory in the newly mounted
sudo mkdir -p /backupdrive/home/<user>
In order to manage the backups we are going to use a simple utility called
rdiff-backup. This utility is based off of
rsyncand is available on most modern operating systems.
In Ubuntu run
sudo apt install rdiff-backup for other distros substitute the package manager.
Client Side Setup
Switch back over to the client machine that you want to backup to the Pi. From now on this will be referred to as the client. Also install
rdiff-backup on the client using the package manager that best fits.
SSH without password
In order to automate the backup process we will need to be able to securely SSH to the Pi without having to enter a password every time. To do this we will pass an SSH pub key to the Pi so that it can properly authenticate the request without a password supplied.
On the client machine run:
ssh-keygen -t rsa
And take note of where is says the public key has been saved.
Now copy the public key that was created above to the Pi with a simple
scp <path to .pub> pi@<pi_ip_address>:/var/tmp/id_rsa.pub.temp
Then switch back over to the Pi and run the following to copy the file into the right location.
cat /var/tmp/id_rsa.pub.temp >> ~/.ssh/authorized_keys
And now you should be able to SSH to the Pi without entering a password.
In order to verify everything is working before automating the process lets run the initial backup. On the client machine determine what directory you would like to be backed up automatically. Then run:
rdiff-backup --terminal-verbosity 8 --print-statistics <path to backup dir> pi@<pi ip address>::/backupdrive/home/<user>
This process may take some time to run on the initial upload but hopefully at this point everything will be backed up successfully! Backups after this will take less time as it will only copy what has been modified.
Now that everything is setup to backup our client machine we do not want to have to remember to do so. Any manual trigger for a backup system will eventually be forgotten an therefore is not really useful. On Linux or MacOS we can setup a simple Cron job to run every day and execute the command we used above for the initial upload.
crontab -e to edit the cron registry. Then enter the following cron string to run the backup every night at 10PM.
0 22 * * * rdiff-backup <path to backup dir> pi@<pi ip address>::/backupdrive/home/<user>
I chose 10PM because that is a time where I am most likely on my computer nearly every night. This system is initiated from the client side to the machine must be on for it to trigger. For more information on Cron strings check out this website.
And with any luck that process should run every day and automatically backup your system!
In this article, we saw how to take a Raspberry Pi computer and turn it into a secure local network backup device.