automatic backup of USB flash drives with a folder action
I use USB flash drives to store all the little tools, scripts and config files i need to switch between Mac, Linux and Windows. A Macbook pro is my main notebook and because of that i use it to make regular automated sync’s of my USB flash drives to the local disk. I use the automator and a bash script that starts rsync to do a incremental backup of the files on the USB drive.
Start the Mac OS Automator to create a new folder action. A folder action we be started if something changes inside a folder. If you plug in a USB flash drive, a folder with the Name of the drive will be created under /Volumes and the drive will be mounted under this directory. This triggers the folder action we will create now
Choose to create a new folder action in the new document dialog.
Open the folder dialog to select the /Volumes folder as the trigger for the folder action.
Because /Volumes is not visible in the finder and in the file dialogs you must use
Shift + Command + G
to open the go to folder dialog.
Now the Volumes folder shows up in the file dialog.
Input the bash script that will run rsync if the name of the USB flash drive matches on of the names in the USBNAMES array.
If you use Mountain Lion you can add a automator action for the Notification Center after the bash script that will run rsync if the name of the USB flash drive matches on of the names in the USBNAMES array. The automator action can be downloaded at [automatedworkflows.com](http://automatedworkflows.com.
USBNAMES=( BBO-4GB BBO-8GB )
RSYNC=/usr/bin/rsync
RSYNCOPT=-avh
BACKUPFOLDER=~/USBBACKUP
MOUNTFOLDER=/Volumes
MOUNTS=( $MOUNTFOLDER/* )
## create backup folder if missing
if [ ! -d $BACKUPFOLDER ]
then
mkdir -p $BACKUPFOLDER
fi
for folder in $MOUNTS
do
for name in "${USBNAMES[@]}"
do
if [ $folder == "$MOUNTFOLDER/$name" ]
then
$RSYNC $RSYNCOPT $folder $BACKUPFOLDER --log-file=$BACKUPFOLDER/$name.log
fi
done
done
Save the folder action and give it a meaningful name.
If you connect the USB flash drive the workflow icon will show up in the status bar and it will disappear if the script has been finished.
After the workflow has finished you should see the new USBBACKUP folder in your home directory, the backup folder for the flash drive and the rsync logfile for that drive.
It is also possible to use Growl notifications to report the status of the workflow, but i used the Mountain Lion notifications after i have upgraded my macbook.