Automatically backing up a Raspberry Pi’s files

Automatically backing up a Raspberry Pi’s files

If, like me, you have a Raspberry Pi which for one reason or another you want to automatically back up, then this may be of use to you.

The “tl;dr, I know stuff about scripts and Linux” version

Stick this, modified to suit, in cron every five minutes:

sshpass -p "password" rsync -avz -e ssh /home/pi/path/to/folder/ newuser@192.168.1.10:Documents/Pi-Backups/

For everyone else, read on:

In my case, I’m using RetroPie and take screenshots of the games I play. It’s a pain having to transfer these over to another computer or the internet, so I thought I’d look for another way of doing it.

There are methods of getting the likes of Google Drive or Dropbox working on a Raspberry Pi, but these are 1) overkill, 2) use their own existing folders rather than those I want to back up, and 3) are more resource hungry than I’d like – especially considering my Pi will be running games and I don’t want any syncing to slow that down mid-game. There’s also Bittorrent Sync, but that requires installing something else on another computer, which I also wanted to avoid if possible.

I thought about using rsync, which I’d dabbled with for other purposes before, but initially discounted it as I’d have to somehow mount a remote filesystem, do the sync and dismount and it all seemed fiddly and complicated. When I looked into it some more, however, it actually seemed very simple indeed, is not resource hungry at all, and runs happily via cron.

On my home network I have a Mac and a Synology NAS which are both almost always on, so either were a candidate for where to sync files actually to. I decided that syncing every five minutes would be sufficient (if there’s nothing to sync, rsync spawns for literally milliseconds then quits), and as I’d found rsync can do its thing over ssh, my Mac was a better candidate (I don’t have ssh turned on on the NAS).

So here’s what I needed to do:

  1. Set up the necessary folder on the Mac to “receive” the files. I decided to create a new user on the Mac just for this purpose, for security (see why later).
  2. Set up the bash script on the Pi to run the backup. This is where the rsync command will be put.
  3. Configure cron on the Pi to run the script every five minutes.

The first of these was quite simple – just add a new user in System Settings, log in as them, and log out again.

The rsync command, when we test it before we stick it in a script, is basically this:

sudo rsync -avz -e ssh /home/pi/path/to/folder/ newuser@192.168.1.10:Documents/Pi-Backups/

Where “/home/pi/path/to/folder/” is, well, the path to the folder you want to back up (in my case, “/home/pi/.config/retroarch/”), “newuser” is the username you connect to your computer with, “192.168.1.10” is your computer’s IP address, and “Documents/Pi-Backups/” is the path on that computer where the backups are going to be saved. Change all these as necessary.

When you run this command for the first time, you may be asked if you want to store an ssh key. Say yes. Note: we’re running this as root so the key is saved for root for cron later. It’ll also ask you to log in with newuser’s password. Here’s a problem: how do we pass that password each time? There are two ways I looked at. The first is: Don’t. Use ssh keys instead. The second, which I chose, is with sshpass.

Important: to be most secure, you want to be taking the secure key generation route. However, since I’m doing this only on my Raspberry Pi, only when it’s turned on, only on my own personal network, and only with a username I created just for this purpose that has no real access to anything else anywhere else, I felt it OK to forgo that on this occasion.

By default, certainly on RetroPie, sshpass isn’t installed. So you’ll need a quick:

sudo apt-get install sshpass

first to get that set up. With sshpass you can pass the ssh password (now there’s a sentence) like this:

sudo sshpass -p "password" rsync -avz -e ssh /home/pi/path/to/folder/ newuser@192.168.1.10:Documents/Pi-Backups/

Note how the password is in plain text, and will be stored in the backup script. Your security, etc. blah blah. If that works for you, then you can create the bash script that cron will run. I called it “sync.sh” and put it in /home/pi, but it’s not important as long as you remember. If you don’t know know to create scripts, do this on the pi:

nano /home/pi/sync.sh

which will open the nano text editor. Type in this script:

!#/bin/bash
sshpass -p "password" rsync -avz -e ssh /home/pi/path/to/folder/ newuser@192.168.1.10:Documents/Pi-Backups/

Save it with CTRL-O (Return) and quit with CTRL-X (Return). Now you need to make the script executable with:

sudo chmod +x /home/pi/sync.sh

Finally, it’s time to cron this up so it runs every five minutes. Edit cron jobs with:

sudo crontab -e

This will open nano again with a default crontab. Move the cursor to the bottom and type:

*/5 * * * * /home/pi/sync.sh

then CTRL-O/CTRL-X out again. You’ll get a message about installing new crontab, and you’re all set! For me, this means that within five minutes of me taking a screenshot in RetroPie, the file appears on my Mac.

Leave a Reply

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