Encrypted multi-version backup to Dropbox script

I wrote a little backup script that I’ve been using and thought there might be some who could find it useful.

What it does:

  • Encrypt, compress and back up your Scrivener file to Dropbox (can be a regular folder).
  • Runs every hour and only does backup if you Scrivener file has been updated.
  • Can allow you to get a graph of the progress of your work over time.

Why use it?

  • I can access my Dropbox account from multiple places, but I don’t want anyone to be able to access and read my Scrivener backups.

Your encrypted backup folder will look like this:
TheWork.2011-04-16–13-00.tar.gz.x
TheWork.2011-04-16–17-00.tar.gz.x
TheWork.2011-04-16–18-00.tar.gz.x
TheWork.2011-04-16–21-00.tar.gz.x

And with a little formatting you can get the size of each of the files and see your writing progress graph over time. I takes a little work, but it’s easy to make a graph in Excel.

This will be a little technical, but I’ll do my best to make it easy.
We’ll assume that:

  • Your Scrivener file is in ~/Documents/TheWork/TheWork.scriv
  • Your backup script will live in ~/Documents/backup
  • Your backups folder is ~/Dropbox/Backup/TheWork/ (make it if it doesn’t exist)
  • The encryption passphrase is “12345” (thoughts of luggage come to mind)
  1. Open Terminal in Applications -> Utilities

  2. Let’s create the passphrase file. Type in the following:

echo "12345" > ~/.x
chmod 600 ~/.x
  1. Now let’s create the backup script. In Terminal type:
pico ~/Documents/TheWork/backup

… and press Enter

  1. Copy and paste in the following
#!/bin/bash

cd ~/Documents/TheWork
tar cf TheWork.tar TheWork.scriv
gzip -f TheWork.tar

# Using size for comparison, diff and cmp don't seem to work
FILESIZE1=$(stat -f%z "TheWork.tar.gz")
FILESIZE2=$(stat -f%z "TheWork.tar.gz.last_backup")

if [ $FILESIZE1 == $FILESIZE2 ] ; then
  echo "same - no backup" >/dev/null
else
  echo "different" >/dev/null
  openssl aes-128-cbc -e -kfile ~/.x -in TheWork.tar.gz -out TheWork.tar.gz.x
  cp TheWork.tar.gz.x ~/Dropbox/Backup/TheWork/TheWork.$(date +%Y-%m-%d--%H-%M).tar.gz.x
  cp TheWork.tar.gz TheWork.tar.gz.last_backup
fi
  1. Press Control-X to exit pico. It will ask you if you want to save the file, press Y and Enter.
  2. Now let’s schedule the script to run once an hour. In Terminal, type:
crontab -e
  1. It will open in Vim. Press “i” to enter Insert mode then copy and paste this:
 0 * * * *       ~/Documents/TheWork/backup
  1. Now press Escape to exit the Insert mode, then type “:wq” and press Enter.

And that’s it! Easy, right? :slight_smile:

Oh, almost forgot, to actually decrypt your file in Terminal type the following (it will ask you for your passphrase):

openssl aes-128-cbc -d -in TheWork.2011-04-16--13-00.tar.gz.x -out TheWork.decrypted.tar.gz
tar xvfz TheWork.decrypted.tar.gz

Enjoy.
Andrey.