home  |  pictures  |  who is schwalbe  |  my testimony
contact me: email//aim: j525x//txt.msg

Daily Encrypted Off-site Backup using Dropbox and Scripts

I wanted to share a solution I came up with awhile back that uses Dropbox, a few bash scripts, and some ingenuity to make a free, daily, encrypted, incremental off-site backup to keep track of important files that are changed daily.

The idea: For those not yet acquainted, Dropbox is a free service which allows you to sync 2.25 gigs of information (which can be increased by referrals or money) in a specific folder with their offsite server. You are able to access this info from any computer and from many Mobile devices (including iPhone).

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.

Here’s the code 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).  Downloadable file after the code:

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

#####################################
datestring=`date +%d`
thisyear=`date +%y`
thismonth=`date +%m`

#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

Here’s the file you can download: backup.sh

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 (Google crontab).

To use it or see if it’s working, navigate to the Dropbox folder that you set in the script, and double click on the .dmg disk image file, entering your password when asked.  Copy the file(s) that you need off the newly mounted disk, and then eject it.  Simple pie.

Update #1: Please note that in order to use folders with spaces in them, such as “Business Contacts” you would enter in
“Business\ Contacts”.
(Note the backslash before the space.) Thanks to @MagerValp in tuaw’s comments for pointing that out.

Questions in the comments.

Disclaimer #1: I’m not responsible if this causes problems. Sorry :(
Disclaimer #2: If you click on the link to Dropbox and signup, you and I both get an extra 250mb of storage. Not bad.

Uncategorized | Justin Schwalbe on Monday November 22 2010 @ 7:52 am | Comments (17) Tags: , ,

17 Comments »

  1. Comment by Bob Sander-Cederlof — November 23, 2010 @ 9:01 am

    Two extra lines, defining dir and today, variables not ever used.

    “dumb and redundant but sorta needed” needs explanation.

    I noticed on my system the folders and files in tmp/Backups/ have the group for each file changed to “wheel”. Is this necessary or desirable?

    Some backups will linger longer than 31 days. For example, Jan 29 30 and 31 will not be replaced until Mar 29 30 31.

  2. Comment by Justin Schwalbe — November 23, 2010 @ 9:10 am

    Thanks for the comment, Bob.

    In regards to the dumb/redundant comment, the script is essentially tarring a file and then immediately un-tarring it, but I found that this worked better than copying an entire directory (gave some more flexibility).

    I don’t really know about the “wheel” group thing.. No idea of its implications either. It should get deleted at the end of the script running, unless that group setting is preventing this from happening.

    I’m okay with a few backups lasting longer. Every other month you get an extra month of grace period, I guess. :)

  3. Comment by Justin Schwalbe — November 23, 2010 @ 9:12 am

    Oh yeah, and I updated the code, removing the extra lines you noticed. Thanks! They were leftovers from porting the code from working on my system to working more generically.

  4. Pingback by Use Dropbox to backup a few folders every day | Design City — November 23, 2010 @ 10:39 am

    […] a nice little tutorial on Justin Schwalbe’s blog for using a simple script to backup a few folders to a Dropbox backup folder every day. Why would you do this? Well, let’s say you aren’t always able to connect to […]

  5. Comment by Matt — November 23, 2010 @ 12:52 pm

    Can this script only backup individual directories or could you set the backupfolders var to /Volumes/flashdrive if you wanted to backup the entire contents of flashdrive?

  6. Comment by Justin Schwalbe — November 23, 2010 @ 12:57 pm

    Matt, the script shouldn’t know the difference between the two.. so yes, it should work as a daily flash drive backup solution. (Haven’t tested it though.) You may want to add in some code where it makes sure the flash drive is at least present, though.


    # check if directory exists
    if [ -d $backupfolders ]; then
    the rest of the code goes here..
    fi

  7. Pingback by Use Dropbox to backup a few folders every day | veet — November 23, 2010 @ 7:09 pm

    […] a nice little tutorial on Justin Schwalbe’s blog for using a simple script to backup a few folders to a Dropbox backup folder every day. Why would you do this? Well, let’s say you aren’t always able to connect to […]

  8. Pingback by Use Dropbox to backup a few folders every day | Another news blog — November 23, 2010 @ 8:09 pm

    […] a nice little tutorial on Justin Schwalbe’s blog for using a simple script to backup a few folders to a Dropbox backup folder every day. Why would you do this? Well, let’s say you aren’t always able to connect to […]

  9. Pingback by Use Dropbox to backup a few folders every day | aan — November 25, 2010 @ 1:10 am

    […] a nice little tutorial on Justin Schwalbe’s blog for using a simple script to backup a few folders to a Dropbox backup folder every day. Why would you do this? Well, let’s say you aren’t always able to connect to […]

  10. Pingback by maccentricity.info» Blog Archive » Use Dropbox to backup a few folders every day — November 25, 2010 @ 10:13 am

    […] a nice little tutorial on Justin Schwalbe’s blog for using a simple script to backup a few folders to a Dropbox backup folder every day. Why would you do this? Well, let’s say you aren’t always able to connect to […]

  11. Comment by bugmancx — November 25, 2010 @ 1:59 pm

    You can also create symlinks inside your Dropbox folder to anywhere on your filesystem. You could create symlinks of all of the directories above which would push your changes into Dropbox into real time without having to actually store the files in that separate Dropbox directory. (Linux or OSX only obviously.)

  12. Comment by James — December 3, 2010 @ 10:59 am

    Dude,
    1) You are genius.
    2) That is all.

    James

  13. Comment by joanca — December 8, 2010 @ 3:32 pm

    I get an error:
    “””
    hdiutil: WARNING: -passphrase has been deprecated in favor of -stdinpass
    hdiutil: create: image name parameter missing
    Usage: hdiutil create [options]
    hdiutil create -help
    “””
    Am I doing something wrong? I can’t figure it out…
    Thanks and sorry if it’s a dumb question, I’m not really into this stuff…

  14. Comment by Justin Schwalbe — December 8, 2010 @ 4:20 pm

    @joanca good question.
    the warning part is just a warning and can be ignored. The rest of it, however, means that there is probably an error somewhere with the defined Folders. Try using just one folder first, without any spaces, then add on as you can.

  15. Comment by lille — December 15, 2010 @ 3:14 am

    Hi thanks for making this script, its just what I have been looking for.
    But I’m having a bit of a problem with it.
    Since I’m not so good with terminal yet, I found this application that adds a GUI to the crontab deal, its called Cronnix and is easy to use.
    The problem is that when time comes for it to run the script all it does is open it in text edit:/
    Let me add that I did try with terminal first running the chmod +x backup.sh command but it kept saying no file or directory was found.

    Please hope you can help, as I would love to get this thing working.

  16. Comment by Chris — December 18, 2010 @ 2:50 am

    You shouldn´t hardcode to /tmp…while it works the cleaner solution is to use $TMPDIR to get the location for temporary files.

  17. Comment by Ricardo — May 9, 2012 @ 10:07 pm

    Hi, thanks for sharing this.

    I’m having some issues with your script, some of them I figured out, put there’s still one that I can’t. When running the hdiutil command, I’m getting an error:

    hdiutil: create: image name parameter missing
    Usage: hdiutil create [options]
    hdiutil create -help

    Any ideas?

    Thanks.

RSS feed. TrackBack URI

Leave a comment