Automating backup of Flash drives Creative Commons License

posted in the wee hours by Constantinos. Filed under Code, OS X

This post was originally published in 2007
The tips and techniques explained may be outdated.

I have a lot of my critical work on a few flash drives which I carry around with me constantly, mainly because I work on a number of machines and would like to have a central repository of my work. After losing one of the drives recently though (bound to happen sooner or later), I looked further into automating the backup process (I had lost about 2 days of work). That’s when I stumbled accross this hint on macosxhints.com, but the script didn’t exactly fulfil what I had imagined my automated process to do.

Ideally, the automated backup process would have these features:

  1. Not require installation of any third party app
  2. Have the ability to somewhat customize the backup script for each thumb drive
  3. Have the backup script silently run whenever the thumb drive is mounted
  4. Require little to no set up for each new thumb drive that I want to backup
  5. Support spaces in Volume names
  6. Not try to backup the boot drive under any circumstances (don’t ask…)

Updated 2007-03-10: I updated the backup script to include automated handling of archives, and keeps an arbitrary number (specified in the preferences of the script) of incremental archives using rsync's --link-dest parameter, as recommended by John in the comments. This creates what functionally amounts to FULL backups, but with a fraction of the space needed for an actual tar backup. Also it can be configured to keep at most one backup per day, or with minor editing at most one backup for any period of time.

Updated 2007-03-18: I've created a second script which can be used as a drag-n-drop solution. Simply save the script as an application bundle, save it somewhere handy, and drag it to your dock. Now you can drag your thumb drive on the script which will perform the backup, and then eject your drive!

Updated 2008-03-10: Folder action modified to work with Leopard. Well, more like hacked at this point, but it seems to work.


Now to satisfy the first parameter, using Do Something When was out of the question, even though it seems like a good piece of software. That’s when I turned to Fodler Actions, and found this hint again on macosxhints.com. I then proceeded to read the Apple-published Folder Action for automating backups (which, again, didn’t do quite what I wanted), so I came up with this:

A folder action that scans the root folder of the mounted volume, and looks for (one of) a specified file name.

property backup_script : "rsync.app" (* add/change this string to match the name of your script *)
 
on adding folder items to this_folder after receiving these_volumes
    tell application "Finder"
        try
            set the volume_list to every file of this_folder
 
            (* go through all entries in /Volumes/ *)
            repeat with i from 1 to number of items in volume_list
                set this_item to the original item of item i of volume_list
                if the kind of this_item is "Volume" then
                    set this_disk to (this_item as alias)
 
                    (* is this item the newly mounted disk? *)
                    if this_disk is in these_volumes then
 
                        (* iterate through all files in the root of disk *)
                        set searchCmd to "ls -d " & quoted form of POSIX path of this_disk & backup_script
                        (* check to see if a backup script is available *)
                        set searchResult to ""
                        try
                            set searchResult to do shell script searchCmd
                        end try
                        (* run the backup script *)
                        if (searchResult starts with "/Volumes") then
                            set backupFile to POSIX file searchResult as alias
                            open backupFile
                        end if
 
                    end if
                end if
            end repeat
        on error error_message number error_number
            if the error_number is not -128 then
                display dialog error_message buttons {"OK"} default button 1
            end if
        end try
    end tell
end adding folder items to

Download this script

If you already know how to apply folder actions, just save the script above and apply it as a folder action to the /Volumes/ folder. If you don’t know (or don’t remember) how to do that, re-iterating the instructions from this Apple page, follow these steps to do it:

  1. Copy the above code in Script Editor
  2. Save it as a script in /Library/Scripts/Folder Action Scripts/, and give it a descriptive name
  3. In Finder, click Go -> Go to Folder (or just press cmd-shift-G), and type /Volumes/.
  4. Launch the Folder Actions Setup utility (probably located in /Applications/Apple Script/ folder)
  5. Click the Add Folder Action (round plus) button at the bottom left
  6. Drag the small folder icon from the Finder window title bar into the Choose Folder sheet dialog, and click ok.
  7. From the dialog that comes up, chose the script you just saved.
  8. You’re done.

Alternatively (or additionally), you can save this script as an application bundle, save it somewhere handy, and drag it to your dock. When you drag a thumb drive on this application the backup process will start, and as soon as it's done the drive will eject.

property backup_script : "rsync" (* add/change this to match the name of your script *)
 
on open dropped_item
    set this_vol_alias to (dropped_item as alias)
    try
        tell application "Finder"
            if the kind of this_vol_alias is "Volume" then
                set the file_list to every file of this_vol_alias
                repeat with j from 1 to number of items in file_list
                    (* check to see if a backup script is available *)
                    set this_file to name of item j of file_list
                    if this_file is equal to (backup_script & ".app") then
                        open item j of file_list (* run the backup script *)
                    end if
                end repeat
            else
                display alert "This script can only be executed on volumes"
                return
            end if
        end tell
 
        delay 2 (* give some time for the backup script to actually launch *)
        tell application "Finder"
            try
                set bakapp to creator type of process backup_script
                repeat while bakapp is not equal to ""
                    delay 1 (* the backup is still running, we can check again in a second *)
                    set bakapp to creator type of process backup_script
                end repeat
            on error
                if (ejectable of this_vol_alias is true) then
                    (* display alert POSIX path of dropped_item *)
                    eject this_vol_alias
                end if
            end try
        end tell
 
    on error error_message number error_number
        if the error_number is not -128 then
            display dialog error_message buttons {"OK"} default button 1
        end if
    end try
end open

Download this script

Now, for the modified backup script. The properties at the top can be modified on a per copy basis to customize the function of the script.

property archive_backup : true
property number_of_archives : 3 (* 0 for unlimited *)
property with_administrator_privileges : false
property one_archive_per_day : true
 
property display_notification : false
property backup_target : "Backups/" (* MUST be a folder location with trailing slash! Always relative to home folder *)
 
property rsync_params : "-aEz --delete-excluded"
(* END OF PREFERENCES *)
 
set WhereImRunningFrom to path to me
tell application "Finder"
    (* can't run from the script editor *)
    set AppCreator to creator type of WhereImRunningFrom
    if AppCreator is "ToyS" then
        activate of me
        beep
        display alert "This script cannot run directly from ScriptEditor"
        return
    end if
 
    (* NEVER run from the hard drive! *)
    set bootVolume to name of disk of home (* safety feature! *)
    set NameOfDisk to name of disk of WhereImRunningFrom
    if NameOfDisk is bootVolume then
        beep
        display alert "Should not run this script from the boot volume!"
        return
    end if
 
    (* setup backup dir *)
    set homeDir to path to home folder from user domain
    set backup_folder to POSIX path of homeDir & backup_target
    try
        (* quick and dirty check to see if folder exists - must be a good way to do this in applescript? *)
        do shell script "cd " & backup_folder
    on error
        try
            do shell script "mkdir " & backup_folder
        on error
            display alert "There was an error creating the backup folder " & backup_folder
            return
        end try
    end try
 
    (* find source and target *)
    set backupBase to NameOfDisk & "-Backup"
    set targetDir to backup_folder & quoted form of backupBase
    set sourceDir to quoted form of ("/Volumes/" & NameOfDisk & "/")
 
    (* set up archive filename *)
    if archive_backup then
        set thedate to current date
        set theday to day of thedate
        set themonth to month of thedate
        set theyear to year of thedate
        set thetime to time of thedate
 
        set thedate to "-" & theyear & "-" & shortMonth(themonth) of me & "-" & theday as string
        if not one_archive_per_day then set thedate to thedate & "-" & thetime as string
 
        set backupFolder to quoted form of (backup_folder & backupBase & thedate  & "/")
    end if
end tell
 
(* do the backup *)
(* try *)
if with_administrator_privileges then
    set excluded to " "
else
    set excluded to " --exclude='.Trash*' --exclude='.Spotlight*'  "
end if
 
if archive_backup then
    set posixDir to POSIX file backup_folder
    set fileList to list folder posixDir
    set archiveList to {}
    tell application "Finder"
        (* Search for existing backups *)
        repeat with i from 1 to number of items in fileList
            set thisFile to item i of fileList
            set filePath to path to resource thisFile in bundle posixDir
            if thisFile starts with backupBase then
                set archiveList to archiveList & POSIX path of filePath
            end if
        end repeat
    end tell
 
    (* List existing backups in order of last modified date *)
    set latestArchive to ""
    set linkDest to ""
    set listString to ""
    repeat with x from 1 to number of items in archiveList
        set listString to listString & " " & quoted form of item x of archiveList
    end repeat
    set newString to do shell script "ls -dt " & listString (* sort files by modified date *)
    set theList to (paragraphs of newString)
 
    (* Clear out outdated backups *)
    if (number of items in theList is greater than number_of_archives) and (number_of_archives is greater than 0) then
        repeat with y from (number_of_archives + 1) to (number of items in theList)
            do shell script "rm -Rf " & quoted form of item y of theList
        end repeat
    end if
    if number of items in theList is greater than 0 then
        set latestArchive to quoted form of item 1 of theList
        if latestArchive is equal to backupFolder then
            (* Preserve same target folder *)
            if number of items in theList is greater than 1 then
                set latestArchive to quoted form of item 2 of theList
            else
                set latestArchive to ""
            end if
        end if
    end if
 
    if latestArchive is not equal to "" then
        set linkDest to " --link-dest=" & latestArchive
    end if
    set theScript to "rsync " & rsync_params & linkDest & excluded & sourceDir & " " & backupFolder & "; touch " & backupFolder
else
    set theScript to "rsync " & rsync_params & excluded & sourceDir & " " & targetDir & "; touch " & targetDir
end if
 
if with_administrator_privileges then
    do shell script theScript with administrator privileges
else
    do shell script theScript
end if
 
activate of me
beep
if display_notification then display alert NameOfDisk & " drive backed up"
(*
on error
    display alert "There was an error backing up " & NameOfDisk
end try
*)
 
 
to shortMonth(themonth)
    set MonthList to {January, February, March, April, May, June, July, August, September, October, November, December}
    set mm to 1
    repeat until item mm of MonthList = themonth
        copy mm + 1 to mm
    end repeat
    if mm < 10 then copy "0" & mm to mm
    return mm
end shortMonth

Download this script

Copy the above code in Script Editor, and save it as an Application bundle giving it the name "rsync". If you wish to give it a different name, or maintain a number of copies each with a different name, make sure to modify the first line of the Folder Action script appropriately.

Finally, I can just drop a copy of the rsync application bundle in any thumb drive I want to keep a backup of, and apply the Folder Action to any computer I want to maintain backups on. Since the backup script is using rsync for the backup, I can have multiple backups on any number of computers and they will all mirror the contents of the thumb drive whenever the thumb drive is mounted on them. Additionally, a tar-gzipped copy of the thumb drive can optionally be created with a date stamp for archiving. Deletion of old archives must be managed manually.

If you are having permission issues with some of the files on one of your thumb drives, you can enable the with_administrator_privileges property in the backup script. This will cause the script to ask you for a password every time the thumb drive is mounted before performing the backup, but will perform the backup as an administrator eliminating any permission issues.

I feel that this script can still be improved, but for now it works just fine for my needs. If you have any suggestions for improving this process, feel free to add them in the comments.

78 Responses to “Automating backup of Flash drives”

Pages: [2] 1 » Show All

  1. 78. Comment by skororu
    on 2013-11-26 @ 4.30 am · Quote #206702 ·

    Update for OSX Mavericks

    I made a quick hack to the folder action script that attaches to the /Volumes directory so it works for OSX Mavericks. The script that sits on the memory stick itself works fine without any modification.

    http://pastebin.com/d77rwsrP

  2. 77. Comment by Nichole
    on 2011-06-20 @ 7.57 am · Quote #33108 ·

    Where do I go to access my backedup files?

    The 6th property in the actual backup script reads


    property backup_target : "Backups/" (* MUST be a folder location with trailing slash! Always relative to home folder *)

    If you did not change it, then your backups would be in ~/Backups/, where ~/ is your Home directory. So Open up the finder, click on your home dir, and look for a folder called Backups.

    Thank you for your reply… I went to finder, searched for backups and nothing comes up. I followed the instructions as per above, and my flashdrive seems to get to business as soon as its mounted. Also, when I save something the screen briefly flashes (as if it’s backing up – just my imagination?)

    I’m having a heck of a time with this… Nothing seems to be saved anywhere on my computer. I def. did not change the script. Any help would be greatly appreciated. I recently lost everything on a different flash drive and don’t want to go through that again.

  3. 76. Comment by Constantinos
    on 2011-06-13 @ 7.41 pm · Quote #32771 ·

    Where do I go to access my backedup files?

    The 6th property in the actual backup script reads


    property backup_target : "Backups/" (* MUST be a folder location with trailing slash! Always relative to home folder *)

    If you did not change it, then your backups would be in ~/Backups/, where ~/ is your Home directory. So Open up the finder, click on your home dir, and look for a folder called Backups.

  4. 75. Comment by Nichole
    on 2011-06-12 @ 11.58 pm · Quote #32730 ·

    Where do I go to access my backedup files?

  5. 74. Comment by Guapo
    on 2011-01-21 @ 3.23 am · Quote #24562 ·