Save the day: How to back up Raspberry Pi projects to dropbox

Raspberry PiTinkering with projects on a Raspberry Pi is fun, but I was often finding I lost code. Being a newbie, when prototyping a new project I managed to make the Pi unable to boot a few times by either making mistakes or yanking the power one too many times and messing up the SD card (don’t do this, use ‘sudo shutdown -h now’ or add a shutdown switch).

Now I set up my Pi to backup what I’m working on to Dropbox as the first step of a project.

Here’s how you can do the same:

First, you need to download and set-up the Dropbox uploader from Andrea Fabrizi. There is a good tutorial for this over at RasPi TV here. Its got a few steps but it’s pretty easy. I set it up to just have access to a single folder, named after the camera project I was I creating; ‘retrocam’.

One tip: running ./dropbox_uploader.sh to set up your API keys will only do so for your current user (most likely ‘pi’). I found it was worth also running sudo ./dropbox_uploader.sh .

This gives Dropbox access to your root user as well, so any processes launches by the system can use it. Useful if you want to automate the backup. You will have to input the API keys again and authorize on the website again.

Then, setup a folder to store the files you create for your project. I did:

mkdir retrocam

Make sure to store all of your project files in here as much as you can.

Then create a script to compress the contents of this folder into a single file (named by the date), then upload that file to dropbox.

To create the script cd to the project folder you’ve created and create and edit a new file:

sudo nano backup.sh

Mine looked like this:
#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M%s")

echo "Backing up RetroCam files"

echo "Zipping files..."
cd /home/pi
tar -vczf retrocam-backup-$DATE.gz retrocam

echo "Uploading to Dropbox"
cd /home/pi/retrocam
./dropbox_uploader.sh upload /home/pi/retrocam-backup-$DATE.gz /backups/

echo "Removing temp file"
cd /home/pi
rm retrocam-backup-$DATE.gz

echo "Backup complete"

The script defines a variable of the date first to use in the file name. If you don’t do this then each backup will just overwrite the last one (no confirmations, linux assumes you know what you are doing…).

Then it zips the files into a single tar, and uploads them to the Dropbox folder.

Finally, it removes the tar file, so the backups stack up on Dropbox and not taking up space on your SD card.

To make it work, you need to make it executable:

sudo chmod +x backup.sh

Then, when you want to backup a version of your project files, just cd into the project folder on the Pi and type:

./backup.sh

As the script is also in that folder, it even backs up itself.

I’ve just had this handy and run the script before I’ve made any major changes or think I might be about to break something.

If you *really* don’t trust yourself, you could schedule it to run automatically every x minutes using crontab. A simple guide for this is here. Don’t forget to use the full path to the script in a crontab, in my case: /home/pi/retroback/backup.sh .

I hope this helps others save their code!
Photo Credit: G. T. Wang via Compfight cc

2 thoughts on “Save the day: How to back up Raspberry Pi projects to dropbox”

  1. Have you considered using git?
    If so, it would be interesting to know why you prefered dropbox.
    If not, well, there you go 🙂

    1. Thanks for the comment! The honest answer is… I hadn’t thought of it. I’ve used Dropbox for years for file syncing so when I spotted this was possible I went ahead and did it.

      One thing I like about this is that it can sync things like photos taken by the Pi (as I’m running a camera project) to my main laptop, I don’t know if Git could do that too.

      Anyway, a couple of people have mentioned this so I will take a look at Git and learn what it’s all about. Thanks for the suggestion.

Leave a Reply

Your email address will not be published. Required fields are marked *