Saturday, November 21, 2009

Automatically back up thumb drives on your Mac

I have a couple of different thumb drives that I use as portable working devices. The data on them is important, so I wanted to back them up. Today I worked out how to copy the contents of the USB drive to a folder on my hard drive every time the USB drive is inserted into the computer.

The two technologies I used to accomplish this are Folder Actions and AppleScript. The first step is to use ScriptEditor to save the script below to ~/Library/Scripts/Folder Action Scripts/SyncThumbDrive.scpt.


property DEST_DIR : "Documents:ThumbDrives:" (* inside $HOME *)

on handle_new_volume(volume)

log "Detected new volume " & volume

tell application "Finder"
-- figure out source for copying data
set sourcePath to the quoted form of the POSIX path of volume
log "Source path:" & sourcePath

-- work out the destination
set sourceVol to the name of volume
set myHome to path to home folder as string
set destFolderName to (myHome & DEST_DIR & volume)

if exists folder destFolderName then
set destPath to POSIX path of destFolderName
log "Destination path:" & destPath
do shell script "rsync -a " & sourcePath & " " & destPath
beep
end if
end tell

end handle_new_volume

on adding folder items to this_folder after receiving these_items
repeat with aItem in these_items
my handle_new_volume(aItem)
end repeat
end adding folder items to


Next, enable folder actions using the "Folder Actions Setup" application. Add the script above as an action on the "/Volumes" folder. Once you have done that, any time a file or directory is added to "/Volumes" the script will be invoked. Since a new entry is added for each volume mounted automatically, this amounts to triggering the script every time a volume is mounted.

Folder Actions Setup.png


The script looks for a destination directory ~/Documents/ThumbDrives/$VOLUME" where $VOLUME is the name of the drive inserted. You need to create the directory before inserting the drive because the script uses the presence of the directory as confirmation that it should copy the updates to the files on the thumb drive over to the hard drive.

After you create the destination directory, insert the thumb drive. When the backup is complete, the computer will play your configured alert sound.

No files are ever deleted, so if you have removed old files from the hard drive they will re-appear until you remove them from the thumb drive.

2 comments:

Crufty Curmudgeon said...

Hi Doug,

One modification that you might want to do is to determine the size of the thumb drive. If it's over a certain size, the the backup should probably not happen. For example, 250Gb+ external USB drives are relatively inexpensive these days, so unless you've got a *lot* of hard drive space, you might get into trouble.

Regards,

Crust Curmudgeon

Doug Hellmann said...

I thought I'd keep it simple and let the user control which drives are backed up by whatever criteria they choose. That is accomplished by requiring the backup directory to exist before the backup starts.