I wanted a way to automatically backup my 1Password data to my flash drive whenever I plugged it into my MacBook Pro. I already have the data backed up through Dropbox and Backblaze but since 1Password holds all my passwords (including those to Dropbox and Backblaze), it’s important that I have as many different ways of accessing the data as possible.

Keyboard Maestro1 can trigger macros based on certain actions—on login, on system wake, by typing a key combination, etc. The two triggers available for this purpose fire either 1) when a volume mounts or 2) after attaching a USB device. The backup requires mounting a particular volume2, so I chose the first trigger.

After I plug in my thumb drive, the targeted volume mounts and a Keyboard Maestro macro triggers the Zsh script (below). This script uses an rsync alias I’ve taken from Prezto (a Zsh configuration) called rsync-synchronize to update the data on the thumb drive.

#!/usr/bin/env zsh -l
#
# Defines Rsync aliases.
# (rsync-synbchronize is an alias taken from the rsync module in Prezto)
#
# Authors:
# Sorin Ionescu <[email protected]>
#
# Return if requirements are not found.
if (( ! $+commands[rsync] )); then
return 1
fi
#
# Aliases
#
_rsync_cmd='rsync --verbose --progress --human-readable --compress --archive --hard-links --one-file-system'
if grep -q 'xattrs' <(rsync --help 2>&1); then
_rsync_cmd="${_rsync_cmd} --acls --xattrs"
fi
# Mac OS X and HFS+ Enhancements
# http://help.bombich.com/kb/overview/credits#opensource
if [[ "$OSTYPE" == darwin* ]] && grep -q 'file-flags' <(rsync --help 2>&1); then
_rsync_cmd="${_rsync_cmd} --crtimes --fileflags --protect-decmpfs --force-change"
fi
alias rsync-synchronize="${_rsync_cmd} --update --delete"
# rsync-synchronize "source" "destination"
rsync-synchronize "$HOME/Dropbox/Apps/1Password/1Password.agilekeychain" "/Volumes/Thumb Drive/1Password/1Password.agilekeychain"
unset _rsync_cmd

This Keyboard Maestro macro with Zsh script automates the synchronization of my 1Password data to my thumb drive every time I plug it in, rendering yet another backup of it’s valuable data.

Finally, if this is all too hacky for you, checkout Carbon Copy Cloner’s folder-to-folder backup’s for a nice GUI to the backup process.

  1. This referral link will save you 20%!

  2. This normally happens when attaching the thumb drive, but if someone renamed the volume or the volume fails to mount, the macro won’t run.