Tinkering 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:
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:
echo "Backing up RetroCam files"
echo "Zipping files..."
tar -vczf retrocam-backup-$DATE.gz retrocam
echo "Uploading to Dropbox"
./dropbox_uploader.sh upload /home/pi/retrocam-backup-$DATE.gz /backups/
echo "Removing temp file"
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:
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 .