|
|
Automated backup of flash drives
I made use of the comments in this hint and put them together to create a Folder Action Script. The script:
(*
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
|
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.09 seconds |
|