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 automator to create the folder actionStart 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

create a new folder action in the automator guiChoose to create a new folder action in the new document dialog.

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

the folder dialogshift+cmd+g to open go to folderBecause /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.

screen of volumes folder selectedNow the Volumes folder shows up in the file dialog.

select the run script action from the libraryNext you can select the “run shell script” action from the library on the left side.

input the bash rsync scriptInput the bash script that will run rsync if the name of the USB flash drive matches on of the names in the USBNAMES array.

input the bash rsync script including Mountain Lion notificationsIf 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.

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 actiongive the action a meaningful nameSave the folder action and give it a meaningful name.

the workflow shows up in the statusbarIf 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.

the content of the backup folder after the folder action was finishedAfter 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.

8 comments » Write a comment

  1. hi birk, noop alarm, how looks the script visa versa. i worked at my desk and from time to time 10 min i like to sync a frolder from desktop to my usb stick. the stick is not moved from usb every time. an suggestions. mike

  2. Hi Birk,

    I tried a lot of things and it has not worked. it just doesn’t trigger the action when I connect the USBs, it does create the folder USBBACKUPS though. I was wondering if there were any specific syntax rules for the names of the drives or anything I should take into consideration.

    Thanks your help,
    Juan

  3. okay thanks, i changed the script and saved it. but it only created the usbbackup folder. no actual sync was done. and the funny thing is that i translated the home folder to a new particion and i dont have those .plist files. any ideas?

    • Hi Juan,

      please open a terminal and save the script in a text file (e.g. scriptname.sh). Then plug in your USB stick and run the script with “bash -x scriptname.sh”. Paste the output in a comment…

      kind regards,
      Birk

  4. hi, listen i update to 10.8.4 from 10.7.5 and it stopped working i’ve tried erasing it and settin it up again an nothing. any suggestions? thanks by the way it is super

      • ok. thanks. i picked up a book on shell scripting and the folder creation works when it is inserted directly on the conditional and the mkdir but the rsync syntax don’t know it yet. but the logic is pretty strait forward it should work!!
        i change the action to a simple make directory – one line- and it works.
        but thanks again. fast reply lets how we fix this, but u already got me started on automation.
        hehehe

        • Hi Juan,

          the folder action was broken for me too after i have upgraded to 10.8.4. The solution was to delete the folder actions plist files as described here https://discussions.apple.com/thread/4237919 . After deleting these files i have removed and readded the folder action. This has solved the problem.

          I have also made a bugfix (changed for name in $USBNAMES to for name in “${USBNAMES[@]}”) for the script itself. The script now also works for more than one drive name.

          kind regards,
          Birk

Leave a Reply

Required fields are marked *.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>