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.

1.-start-automator-300x262

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

2.-create-folder-action-300x191

Choose to create a new folder action in the new document dialog.

3.-choose-folder-for-action-300x81

Open the folder dialog to select the /Volumes folder as the trigger for the folder action.

4.-folder-dialog-300x220 5.-goto-folder-dialog-300x220

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.

7.-volumes-folder-selected-300x220

Now the Volumes folder shows up in the file dialog.

8.-select-run-script-action-214x300.png

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.

9-2.-input-rsync-bash-script-plus-notification-300x172

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

10.-save-folder-action-300x153 11.-name-the-action-300x69

Save the folder action and give it a meaningful name.

12.-workflow-in-status-bar-300x79

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.

13.-content-of-backup-folder-274x300

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.