Allen J. Hall

Materials Science & Engineering, Productivity, and Life

Automatic USB Key Backups Using Rsync

For Christmas, my awesome brother (Donnie) got me a decently sized LaCie Iamakey [Amazon] along with a few other awesome items. He remembered it from a year or two ago on an old list! Total surprise, and a great one. I’ve been eye-ing one for a while since I’m in need of multiple backups of my research work and thesis writing.

I do do manual backups, and in particular have a drive mirrored using rsync in osX so that it’s bootable (old computers and poor grad-students tend to make for older hard-drives). However, I wanted something a bit faster and easier to use. I wanted an automatic (or semiautomatic) backup of critical writings or data.

After digging here and there and playing around over the past of the past few weeks, I’ve figured it out. Since I didn’t see a very good writeup about how to accomplish this, I thought I’d share what I’ve done.

The first key is how to launch something when a USBkey is mounted in OsX 10.5.x+. To accomplish this, I setup a launch daemon process by editing the proper plist for launchd. However, shortly after the manual edit, I found Lignon, which is a great little GUI for the launchd processes, and I suggest you use it instead. The older (and free) 2.1.1 worked just fine for me [sourceforge] and a newer version is available on the Mac App store. [Feel free to edit the plist if you know what you're doing, the GUI does just that.]

If you are making your own plist, save it to something easily identifiable in your user account Library under LaunchAgents (~/Library/LaunchAgents):

In Lingon (at least the old version) this will look something like this:

If you look carefully, you’ll notice that this launch agent will launch at any drive mounting. When it does, it will attempt to execute a script (in this case named “AutoWorkBackup.sh”). A little trick I’ve used to only launch on the proper drive mount, is to save the script on the actual drive. So, the launch agent will launch the script, if it successfully mounts the usbkey. It’s a little fail-safe in case of a similarly labeled drive is mounted. However, it is worth noting at this point, that the script will successfully launch on any other drive also mounting (with the backup key currently mounted). If I have time (probably won’t) I may look into possible solutions for this. The only problem I’ve found running the script in this manner is that it appears the launch agent is paused and relaunched after 10seconds if the usbkey mount is slow. Launchd appears a bit fast on the draw for the drive mount process.

Finally, we need the script to do the actual dirty work. I use a mix of bash script and applescript to get the job done. I originally only used a bash script and auto-launching rsyncing. It worked quite well, but left me afraid to mount or unmount the key. So, I decided a user dialog would be better. However, I’m tempted to go back to the automatic method as it really was quite fast and I only mounted my key if I knew a backup was expected. In the interest of speed, I’ll assume you can chop out code in the bash script to make your own automatic rsyncing like my first successful script.

Here’s the code I use that launches an applescript dialog asking if a backup is wanted:

#!/bin/bash
/sw/bin/growlnotify -I /Users/allenhall/ShellScripts/LaCieKey.icns -t "Backup USB Key" -m "Backup Key Detected."
x=`/usr/bin/osascript <<EOT
tell application "Finder"
activate
display dialog "Would you like to start a backup?" buttons {"Backup", "No"} default button "Backup" giving up after 15
set myReply to button returned of result as text
end tell
EOT`
# the previous wraps a dialog inside a Finder tell clause to allow for user interaction - the result is returned as text
# test if we got the result properly
# echo $x
# note- trick to prevent error thrown- wrap $x in "" for the conditional
if [ "$x" = 'Backup' ]
then
# test for if statement
# echo 'I will back up!'
# Notify us that we are starting backups
/sw/bin/growlnotify -s -I /Users/allenhall/ShellScripts/LaCieKey.icns -t "Backup USB Key" -m "RSYNC Backup Has Begun."
# actual backup script
/sw/bin/rsync -r -X -A -a -S -v -x -p --exclude-from=/Users/allenhall/backup_excludes.txt $* /Users/allenhall/TEXWork /Volumes/AHallThesisBackup/
# notify us that we completed our backups
/sw/bin/growlnotify -s -I /Users/allenhall/ShellScripts/LaCieKey.icns -t "Backup USB Key" -m "RSYNC Backup Is Complete."
fi

The above script uses a trick to “tell application Finder” to display a dialog. This enables user interaction via an applescript code in a shell script. Useful little trick. Note the above shell script also uses growlnotify the command line part of Growl for notifying the user of tasks started and completed. I’m sorry the code is a bit jumbled above- looks like I’ll have to look for a code plugin for WordPress! Here is the code as ascii text: AutoWorkBackup. Please note that you can also add extra folders in the “actual backup script” portion – just watch the rules of rsync when you do. Of course all the above has to be changed to match your computer and your important folders, etc. [I assume you aren't dorking with launch agents and rsync without knowing a bit about it- or at least won't hold me responsible. ;) ]

I hope you find this useful! Drop me a line if you end up using it, or if you’ve made some cute and useful changes! Here’s wishing you a healthy, productive, and prosperous new 2011!

Post a Comment

Your email is never shared. Required fields are marked *

*
*

A Quick Introduction...

I'm a graduate student (PhD Candidate) at the University of Illinois at Urbana-Champaign.

I've studied and researched in two fields of Materials Science and Engineering (Polymers and Semiconductors). My interests are as diverse as my musical tastes and I usually have my hand in some crazy project during my free time.

I'm available for consulting and have access to a world-renown materials research user-facility supported by the D.O.E. If you would like to know more, please contact me.

Popular Tags

Amazon Associate Link Apple Support AppStore Bug CIGS CIS CLI Conferences Cross Platform Data Mining data visualization dual-driver headphones failure Friend Geek Tool Great Scientists HAM Radio Hardware Tips How To Humanitarian IEM IM In-Ear Monitors iPod Touch LaTeX Linux Mac OsX Materials Science and Engineering Matlab Obituary Open Source problem Productivity reciprocal space return Silent Key Software Software Review Support This Blog Thesis Writing Tip UIUC VOIP Windows xrd

Support This Blog

You can support this blog by shopping on Amazon through my Affiliate Store.