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

Automated backup of flash drives Storage Devices
I recently ran into a problem where I couldn't find my 512MB USB flash drive. Since I didn't regularly make backups of it, I thought I was toast. Valuable work information, portableFirefox, projects, etc., all gone. Luckily I did find it, and it prompted me to devise an easy way to back it up constantly. No way am I digging through the trash bin at 2AM ever again.

First, I installed Do Something When; it's a great little utility that has been featured before, and does exactly what its name implies. Next I set up a rule to run an AppleScript (saved as an application) whenever I mount my USB drive. All the Applescript says is:
do shell script 
  "rsync -rt /Volumes/USBdrivename/ ~/USBdrivename-Backup"
display dialog "USB drive backed up"
The word USBdrivename should be replaced with the name of your USB drive (and remember to quote it if it includes spaces). Copy and paste into Script Editor, modify to suit your needs, and save it as an application. I am not an AppleScript expert, and I'm sure this can be done better, but it was simple and works for me.

I had thought about changing the dialog to ask which way to sync (USB » Mac or Mac » USB), but this works so I left it. Now, every time I plug in my USB drive, the contents are backed up -- "just in case." It also benefits me in those times when I leave the drive at home.
    •    
  • Currently 2.80 / 5
  You rated: 2 / 5 (5 votes cast)
 
[35,166 views]  

Automated backup of flash drives | 16 comments | Create New Account
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: lar3ry on Feb 26, '07 09:16:55AM

DSW is a great little utility, and great for doing semi-automated backups (the only thing "semi-" about it is that I need to make the drive accessible to OS X). This works for flash drives as well as the larger USB drives (and even iPods can be used!).



[ Reply to This | # ]
Automated backup of flash drives
Authored by: friedmaj on Feb 26, '07 09:17:06AM

If your drive does become corrupted and you insert it into your Mac, isn't there a chance that your backup will be immediately overwritten?



[ Reply to This | # ]
Automated backup of flash drives
Authored by: deleted_user3 on Feb 26, '07 11:21:59AM
I would do something like the following which would prevent overwrites

tell application "Finder"
	activate
	set thedate to (current date)
	set theday to day of thedate
	set themonth to month of thedate
	set theyear to year of thedate
	set thedate to theday & " " & themonth & " " & theyear as string
end tell
do shell script 
	"rsync -rt /Volumes/USBdrivename/ ~/USBdrivename-Backup" & thedate
display dialog "USB drive backed up"


[ Reply to This | # ]
Automated backup of flash drives
Authored by: mr. applescript on Feb 26, '07 12:34:17PM
FYI, you can use the free Automator action here to have a workflow automatically execute whenever the drive is mounted:

<http://automator.us/examples-08.html>;

[ Reply to This | # ]
Automated backup of flash drives
Authored by: scotty321 on Feb 26, '07 01:57:28PM

Or, much easier: You could use the amazing utility ChronoSync, which can automatically start a backup as soon as you mount any drive.



[ Reply to This | # ]
Automated backup of flash drives
Authored by: AndyMan401 on Feb 26, '07 04:02:45PM

so far nothing has worked for me--i mean none of the scripts or anything



[ Reply to This | # ]
Automated backup of flash drives
Authored by: viadd on Feb 26, '07 08:37:59PM

Don't you need the -E flag on rsync to get a complete backup of Mac files?
% man rsync
...
-E --extended-attributes copy extended attributes, resource forks
...



[ Reply to This | # ]
Automated backup of flash drives
Authored by: Ezra Balaraj on Feb 27, '07 05:10:52AM

I tried this hint. It works very well. Thanks.

---
EB



[ Reply to This | # ]
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 | # ]
Automated (really) backup of flash drives
Authored by: cnotarianni on Feb 27, '07 09:20:35AM
The original script is not really "automated"... yet.
My following modied script saved as application bundle into flash drive will be!
Now you can use it with multiple flash drives different only by their own name!
The script:
set WhereImRunningFrom to path to me
tell application "Finder"
set AppCreator to creator type of WhereImRunningFrom
set NameOfDisk to name of disk of WhereImRunningFrom

set thedate to (current date)
set theday to day of thedate
set themonth to month of thedate
set theyear to year of thedate

set thedate to theday & "-" & themonth & "-" & theyear as string

---- OPTIONAL whith short month date
set thedate to theday & "-" & shortMonth(themonth) of me & "-" & theyear as string
end tell

if AppCreator is not "ToyS" then
do shell script
"rsync -rt /Volumes/" & NameOfDisk & "/ ~/" & NameOfDisk & "-Backup-" & thedate
activate of me
beep
display dialog NameOfDisk & " drive backed up"
else
activate of me
beep
display dialog "This script don't run directly into ScriptEditor"
end if

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


[ Reply to This | # ]
Automated backup of flash drives
Authored by: teece on Feb 27, '07 12:50:47PM

This is a great hint, thanks.

One caution: rsync on Mac OS X leaves a lot to be desired. Even with the -E option, it does not really make a perfect copy of data in OS X. It will get all regular data, as far as I know, but not all metadata.

It's also pretty buggy (it would regularly crash when I used to use it daily). I used to use it for my main drive automated backup (bash scripts I had carried over from Linux). I quit using it because it was so flakey; I had to buy SuperDuper.

I suspect for the kinds of things stored on a flash drive, rsync will work fine. But be wary. It's buggy for a backup tool (on Mac OS X. It's rock solid on most other Unixes).



[ Reply to This | # ]
Automated backup of flash drives
Authored by: swilcox on Feb 27, '07 03:37:49PM

I use ChronoSync for this, and for automatically backing up certain folders on my HD to my flash drive when the flash stick is mounted.



[ Reply to This | # ]
Automated backup of flash drives
Authored by: Spiken on Feb 28, '07 03:10:00PM

Isn't this kind of the opposite of what you want? You use the flash drive AS a backup for your documents in case your hard drive would crash, or your laptop would be stolen - right? I keep my flash drive with my wallet and keys - close to heart :)



[ Reply to This | # ]
Automated backup of flash drives
Authored by: tpayton on Mar 16, '07 01:38:34PM

This got me thinking about the stack of 400 CDs that I need to copy over to a FW drive. humm...

It would be a script that would see if there was a mounted CD and then just dump it to a destination and then eject the CD.

It could be somewhat universal and for the flash application could have a restriction on file names so that if you mounted a volume that contains "flash" it would copy that to a destination directory.

Anybody know of anything like that?

- T.



[ Reply to This | # ]
DSW + iBackup instead of rolling your own
Authored by: improbable on May 09, '08 07:12:19PM
I had this hint bookmarked, as for a year I'd been making backups based on it: use DoSomethingWhen to run an Rsync via AppleScript.

Today I found iBackup http://www.grapefruit.ch/iBackup/ which is the perfect companion for DSW. It's a rather nice front-end for rsync, with an easy menu of what to back up and where to. It can overwrite your old backup, or can label each folder with the date, and limit to N folders, etc.

Actually I'm using it to back up my computer onto hard drives / iPods etc, rather than the other way around. I'm not certain you could make it do that, now that I think about it. Anyway. I wish I'd found it sooner.

[ Reply to This | # ]

Automated backup of flash drives
Authored by: xag on Aug 23, '09 04:35:42PM
A slightly different approach that uses rsyncx as well. Very quick, no click. It's a do-it-yourself, with a video demo.
http://mogrifiers.blogspot.com/2009/08/quick-usb-backup-from-mac.html

[ Reply to This | # ]