Offsite daily encrypted backup via Dropbox

Nov 23, '10 07:30:00AM

Contributed by: Anonymous

Using some shell scripting + crontab + Dropbox, I've created a method for doing a daily encrypted backup of folders, lasting for 31 days.

Why bother? It's important to back up data because eventually all drives will die. Incremental backups are important because it allows you to track changes from day to day. The solution I have here isn't perfect, but it allows for a few folders to be saved for 30 days before they are overwritten. The benefit of this is that if a huge error is caught 5 days after it was made, then you can revert to the file that was saved 6 days ago.

Following is the script that does the job. I named this file backup.sh and had it run every day at 3am, when nobody is likely to be editing or messing with files. The result of the code is a disk image (.dmg) that will be password protected, and will have the name of backup[1-31].dmg (based on the current date). You'll need to edit the first few variable to match your setup.

# Dropbox as incremental backup.
# Justin Schwalbe
# http://finishtherace.net/wp/?p=622

password="trickypassword"
dropboxdir="~/Dropbox/backups/" #change this as needed, make sure it exists
backupfolders="/path/to/folder /path/to/second/folder /and/so/on"

##############################
# No need to edit below this #
##############################

datestring=`date +%d`
thisyear=`date +%y`
thismonth=`date +%m`
today=`date +%m.%d.%y | sed -e s/^0//`

dir="Daily"

#seems dumb and redundant, but it is sorta needed 
mkdir /tmp/Backups; cd /tmp/Backups
tar -czf backup$datestring.tgz $backupfolders
tar -zxf backup$datestring.tgz
rm backup$datestring.tgz

# hdiutil won't overwrite files, so if it exists, delete it.
if [ -f $dropboxdir/backup$datestring.dmg ]
then
	rm $dropboxdir/backup$datestring.dmg
fi

# create the .dmg file
hdiutil create -srcfolder '/tmp/Backups' -encryption -passphrase $password -fs HFS+ -volname Backup$thismonth.$datestring.$thisyear 
$dropboxdir/backup$datestring

# clean up our mess so tomorrow we can start fresh
rm -rf /tmp/Backups
Just put that file somewhere, make the necessary edits at the top of the file: set a password, your dropbox backup folder location, and the folders you'd like to backup. Then make sure the bash script can run (chmod +x backup.sh), and then set crontab to make it run once a day.

See entire post and instructions at my blog, and download the script here.

[crarko adds: I haven't tested this one.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101122081425940