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.
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.
The script looks for a destination directory
$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.