Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Automated backup of flash drives' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automated backup of flash drives
Authored by: szedula on Feb 27, '07 09:06:24AM
I made use of the comments in this hint and put them together to create a Folder Action Script. The script:
  1. uses "rsync" to make a backup of specific flash drives when they are mounted, you'll need to customize this list
  2. will keep a copy of the backup if configured to do so
  3. display dialogs (if configured to do so) prompting for permission to perform the backup and letting you know when it's done
  4. action must be assigned to "/Volumes" to operate properly
    1. from "Terminal" type in: cd /Volumes;open .
    2. now that the window is open in Finder, you can "Attach" the Folder Action
(*
   This action script will make a copy of a USB flash drive using "rsync" when it is mounted

   rsync options used:
        -p, --perms                 preserve permissions
        -r, --recursive             recurse into directories
        -t, --times                 preserve times
        -E, --extended-attributes   copy extended attributes, resource forks
   
*)
-- list of USB drive names to be backed up, names must not contain blanks
property myUSBdriveNames : {"Disk_1", "Disk_2"}
-- keep additional backup copy?, use "Yes" to keep copy
property SaveBackup : "Yes"
-- display dialogs?, use "Yes" to display dialog
property DisplayDialogs : "Yes"
-- amount of time before dialogs auto-answer
property dialog_timeout : 30
-- default backup question response, "Yes" or "No"
property DefaultButton : "Yes"

on adding folder items to this_folder after receiving added_items
   try
      repeat with thisItem in added_items
         set thisItem to thisItem as text
         if thisItem ends with ":" then
            set thisItem to (characters 1 thru ((count of thisItem) - 1) of thisItem) as text
            
            if myUSBdriveNames contains thisItem then
               
               if DisplayDialogs is equal to "Yes" then
                  with timeout of (dialog_timeout + 10) seconds
                     set alert_message to "Make back up of USB flash drive: " & thisItem
                     display dialog alert_message buttons {"Yes", "No"} default button DefaultButton giving up after dialog_timeout
                     set the theChoice to the button returned of the result as text
                  end timeout
               else
                  set the theChoice to "Yes"
               end if
               
               if theChoice is equal to "Yes" then
                  set DestinationFolder to "~/" & thisItem & "-Backup"
                  set BackupFolder to DestinationFolder & "-copy"
                  with timeout of 1800 seconds -- 1/2 hour should be plenty of time to copy drive
                     if theFolderExists(DestinationFolder) then
                        if SaveBackup is equal to "Yes" then
                           if theFolderExists(BackupFolder) then DeleteFolder(BackupFolder)
                           RenameFolder(DestinationFolder, BackupFolder)
                        end if
                     end if
                     
                     set theCmd to "rsync -rptE /Volumes/" & thisItem & "/ " & DestinationFolder
                     do shell script theCmd
                     
                     if DisplayDialogs is equal to "Yes" then
                        set alert_message to "Backed up USB flash drive : " & thisItem
                        display dialog alert_message buttons {"Ok"} default button 1 giving up after dialog_timeout
                     end if
                  end timeout
               end if
            end if
         end if
      end repeat
      
   on error theError
      display dialog "The following error occurred while backing up USB flash drive:" & 
         return & return & theError buttons {"Sorry"} default button 1
   end try
end adding folder items to

on theFolderExists(theFolder)
   try
      do shell script "ls -d " & theFolder
      return true -- "ls" command works .. folder exists
   on error
      return false -- "ls" command fails .. folder does not exist
   end try
end theFolderExists

on DeleteFolder(theFolder)
   -- construct name in trash using date/time so there are no duplicates
   set thedate to (current date)
   set theDateTime to (day of thedate) & (time of thedate)
   set theTrash to " ~/.Trash/" & GetName(theFolder) & "-" & theDateTime
   do shell script "mv -f " & theFolder & " " & theTrash
end DeleteFolder

on RenameFolder(OldPath, NewPath)
   do shell script "mv " & OldPath & " " & NewPath
end RenameFolder

-- returns the name portion of UNIX "thePath"
on GetName(thePath)
   if thePath ends with "/" then
      -- get the name of a folder
      set p to (characters 1 thru ((count of thePath) - 1) of thePath) as text
   else
      -- get the name of a file
      set p to thePath
   end if
   set x to the offset of "/" in (the reverse of every character of p) as string
   return (characters -(x - 1) thru -1 of p) as text
end GetName


[ Reply to This | # ]