Posted on Fri - November 11, 2005

Time folder backup


I work with two main computers and use a thumb drive to transfer files. This Applescript backs up my current files into a time-stamped folder on the desktop, ready to make into a disk image and to archive. Because I work with multiple Palm Desktop identities, there is a fair amount of extra stuff here. Green words are variables, bold blue words are commands. I'll go through and explain as we go along. The complete script is at the bottom of the entry.

This first bit checks to see which hard drives are mounted and then defines a variable based on that result. Since I usually don't run the computers in disk mode on my home network (that is what Remote Desktop is for), it's a quick and dirty test to see which computer the script is running on. Oh, and "mike" as in "my computer," I stole that from an Isaac Asimov story.
tell application "Finder"
set mike to 0
if disk "MaCei HD" exists then
set mike to "Cei"
else
if disk "Artus iBook" exists then
set mike to "AiB"
else
say "I am confuzzled. I don't recognize the disks"
end if
end if

Nothing complex here. The script checks for the most common applications whose files are about to get backed up and shuts them down. The voice is just in case I forgot about a hidden application that is running.
if process "Palm Desktop" exists then
say "Palm Desktop is quitting"
tell application "Palm Desktop"
quit
end tell
else
say "Palm Desktop already safe"
end if
if process "iBlog" exists then
say "Local web logs are quitting"
tell application "iBlog"
quit
end tell
else
say "Local web logs already safe"
end if
if process "Safari" exists then
say "Safari is quitting"
tell application "Safari"
quit
end tell
else
say "Safari already safe"
end if

Now we get into the fun stuff on Palm Desktop. Palm Desktop doesn't like multiple users running at the same time on the same Macintosh. So even though this is the exact same info, I use one User Data file to overwrite the other. There is another script that copies the Palm document file to the other account's drop box, and still another that overwrites the Palm document file with the file from the drop box. I know it is a kludge, I am basically working around Palm's desktop software.
tell application "Finder"
activate
set MasterPalm to 0
display dialog "I need to know which Palm Desktop file is the most recent." buttons {"NeoWayland", "othername"} default button "othername"
set MasterPalm to (button returned of result)
end tell
if MasterPalm is "othername" then
if file "Users:neo:Documents:Palm:Users:NeoWayland:User Data" exists then
delete file "Users:neo:Documents:Palm:Users:NeoWayland:User Data"
duplicate "Users:neo:Documents:Palm:Users: othername:User Data" to folder "Users:neo:Documents:Palm:Users:NeoWayland"
else
duplicate "Users:neo:Documents:Palm:Users: othername:User Data" to folder "Users:neo:Documents:Palm:Users:NeoWayland"
end if
end if
if MasterPalm is "NeoWayland" then
if file "Users:neo:Documents:Palm:Users: othername:User Data" exists then
delete file "Users:neo:Documents:Palm:Users: othername:User Data"
duplicate "Users:neo:Documents:Palm:Users:NeoWayland:User Data" to folder "Users:neo:Documents:Palm:Users: othername"
else
duplicate "Users:neo:Documents:Palm:Users:NeoWayland:User Data" to folder "Users:neo:Documents:Palm:Users: othername"
end if
end if

I'm combining a couple of extra things here to back up the iBlog files. The long folder names are from when I was still debugging an earlier version of the script and sometimes had to do things manually.
tell application "Finder"
if folder "Users:neo:Sites:put in library-application support:iBlog" exists then
delete folder "Users:neo:Sites:put in library-application support:iBlog"
duplicate "Users:neo:Library:Application Support:iBlog" to folder "Users:Neo:Sites:put in library-application support"
else
duplicate "Users:neo:Library:Application Support:iBlog" to folder "Users:Neo:Sites:put in library-application support"
end if
end tell
tell application "Finder"
if file "Users:neo:sites:put in library-preferences:com.lifli.iBlog.plist" exists then
delete file "Users:neo:sites:put in library-preferences:com.lifli.iBlog.plist"
duplicate "Users:Neo:Library:Preferences:com.lifli.iBlog.plist" to folder "Users:Neo:sites:put in library-preferences"
else
duplicate "Users:Neo:Library:Preferences:com.lifli.iBlog.plist" to folder "Users:Neo:sites:put in library-preferences"
end if
end tell

Same bit here, I am backing up my web graphics folder. It is REAL tempting to use the webgrfx folder in user Sites folder, but remember it is just a backup. That also keeps file paths from crossing and files getting lost
tell application "Finder"
if folder "Users:Neo:Sites:webgrfx" exists then
delete folder "Users:Neo:Sites:webgrfx"
duplicate "Users:neo:Pictures:webgrfx" to folder "Users:Neo:Sites"
else
duplicate "Users:neo:Pictures:webgrfx" to folder "Users:Neo:Sites"
end if
end tell

Now this bit took me forever to get right. It is a modified date array. I finally got around 99% of the problems by adding the timestamp to the folder name. The odds are vanishingly small that one computer is running two backup processes at the exact same time on the exact same day, so it should generate a unique folder name. That variable mike shows up at the end and helps define the time folder name.
if the day of (the current date) < 10 then
set dom to "0" & the day of (the current date)
else
set dom to the day of (the current date)
end if
if the month of (the current date) = January then
set mnt to "Jan"
end if
if the month of (the current date) = February then
set mnt to "Feb"
end if
if the month of (the current date) = March then
set mnt to "Mar"
end if
if the month of (the current date) = April then
set mnt to "Apr"
end if
if the month of (the current date) = May then
set mnt to the month of (the current date)
end if
if the month of (the current date) = June then
set mnt to "Jun"
end if
if the month of (the current date) = July then
set mnt to "Jul"
end if
if the month of (the current date) = August then
set mnt to "Aug"
end if
if the month of (the current date) = September then
set mnt to "Sep"
end if
if the month of (the current date) = October then
set mnt to "Oct"
end if
if the month of (the current date) = November then
set mnt to "Nov"
end if
if the month of (the current date) = December then
set mnt to "Dec"
end if
set y_r to the year of (the current date)
set timestamp to the time of (the current date)
set neuname to mike & "=" & dom & mnt & y_r & "•" & timestamp


Why create the extra nested folder inside the Time folder? Because it is the simplest way I could think of to solve a problem with sibling script. The other computer has no real way to know which name is equivalent to the variable neuname. But if you tell it to look for a folder "xfer" on the desktop, you don't have to worry about it.
make new folder at folder "Desktop" of folder "neo" of folder "Users" of startup disk with properties {name:neuname}
make new folder at folder neuname of folder "Desktop" of folder "neo" of folder "Users" of startup disk with properties {name:"xfer"}

The next bit is pure crunch. Once the backups are done and the new folders are created, the rest is just straight copying. And notice that there is another backup put in the folder currents of the Documents folder.
duplicate "Users:neo:Projects" to folder "xfer" of folder neuname
duplicate "Users:neo:Library:Safari" to folder "xfer" of folder neuname
duplicate "Users:neo:Library:Scripts" to folder "xfer" of folder neuname
duplicate "Users:neo:Documents:Palm" to folder "xfer" of folder neuname
duplicate "Users:Neo:Sites:put in library-application support" to folder "xfer" of folder neuname
duplicate "Users:Neo:Sites:put in library-preferences" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:iBlog" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:webgrfx" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:neowaylanddotnet" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:frame pages" to folder "xfer" of folder neuname
duplicate folder neuname to "Users:neo:Documents:currents"

I'm a big believer in multiple backups, so I always make a disk image and an archive of the folder. Once a month I go in and throw out all the old disk images. And after three months I delete all but that month's first and last day archives for both computers.
tell application "Disk Utility"
activate
end tell
say "I'm ready to make an image from the folder."
end tell




And here is the complete script in one block.
tell application "Finder"
set mike to 0
if disk "MaCei HD" exists then
set mike to "Cei"
else
if disk "Artus iBook" exists then
set mike to "AiB"
else
say "I am confuzzled. I don't recognize the disks"
end if
end if
if process "Palm Desktop" exists then
say "Palm Desktop is quitting"
tell application "Palm Desktop"
quit
end tell
else
say "Palm Desktop already safe"
end if
if process "iBlog" exists then
say "Local web logs are quitting"
tell application "iBlog"
quit
end tell
else
say "Local web logs already safe"
end if
if process "Safari" exists then
say "Safari is quitting"
tell application "Safari"
quit
end tell
else
say "Safari already safe"
end if
tell application "Finder"
activate
set MasterPalm to 0
display dialog "I need to know which Palm Desktop file is the most recent." buttons {"NeoWayland", "othername"} default button "othername"
set MasterPalm to (button returned of result)
end tell
if MasterPalm is "othername" then
if file "Users:neo:Documents:Palm:Users:NeoWayland:User Data" exists then
delete file "Users:neo:Documents:Palm:Users:NeoWayland:User Data"
duplicate "Users:neo:Documents:Palm:Users: othername:User Data" to folder "Users:neo:Documents:Palm:Users:NeoWayland"
else
duplicate "Users:neo:Documents:Palm:Users: othername:User Data" to folder "Users:neo:Documents:Palm:Users:NeoWayland"
end if
end if
if MasterPalm is "NeoWayland" then
if file "Users:neo:Documents:Palm:Users: othername:User Data" exists then
delete file "Users:neo:Documents:Palm:Users: othername:User Data"
duplicate "Users:neo:Documents:Palm:Users:NeoWayland:User Data" to folder "Users:neo:Documents:Palm:Users: othername"
else
duplicate "Users:neo:Documents:Palm:Users:NeoWayland:User Data" to folder "Users:neo:Documents:Palm:Users: othername"
end if
end if
tell application "Finder"
if folder "Users:neo:Sites:put in library-application support:iBlog" exists then
delete folder "Users:neo:Sites:put in library-application support:iBlog"
duplicate "Users:neo:Library:Application Support:iBlog" to folder "Users:Neo:Sites:put in library-application support"
else
duplicate "Users:neo:Library:Application Support:iBlog" to folder "Users:Neo:Sites:put in library-application support"
end if
end tell
tell application "Finder"
if file "Users:neo:sites:put in library-preferences:com.lifli.iBlog.plist" exists then
delete file "Users:neo:sites:put in library-preferences:com.lifli.iBlog.plist"
duplicate "Users:Neo:Library:Preferences:com.lifli.iBlog.plist" to folder "Users:Neo:sites:put in library-preferences"
else
duplicate "Users:Neo:Library:Preferences:com.lifli.iBlog.plist" to folder "Users:Neo:sites:put in library-preferences"
end if
end tell
tell application "Finder"
if folder "Users:Neo:Sites:webgrfx" exists then
delete folder "Users:Neo:Sites:webgrfx"
duplicate "Users:neo:Pictures:webgrfx" to folder "Users:Neo:Sites"
else
duplicate "Users:neo:Pictures:webgrfx" to folder "Users:Neo:Sites"
end if
end tell
if the day of (the current date) < 10 then
set dom to "0" & the day of (the current date)
else
set dom to the day of (the current date)
end if
if the month of (the current date) = January then
set mnt to "Jan"
end if
if the month of (the current date) = February then
set mnt to "Feb"
end if
if the month of (the current date) = March then
set mnt to "Mar"
end if
if the month of (the current date) = April then
set mnt to "Apr"
end if
if the month of (the current date) = May then
set mnt to the month of (the current date)
end if
if the month of (the current date) = June then
set mnt to "Jun"
end if
if the month of (the current date) = July then
set mnt to "Jul"
end if
if the month of (the current date) = August then
set mnt to "Aug"
end if
if the month of (the current date) = September then
set mnt to "Sep"
end if
if the month of (the current date) = October then
set mnt to "Oct"
end if
if the month of (the current date) = November then
set mnt to "Nov"
end if
if the month of (the current date) = December then
set mnt to "Dec"
end if
set y_r to the year of (the current date)
set timestamp to the time of (the current date)
set neuname to mike & "=" & dom & mnt & y_r & "•" & timestamp
make new folder at folder "Desktop" of folder "neo" of folder "Users" of startup disk with properties {name:neuname}
make new folder at folder neuname of folder "Desktop" of folder "neo" of folder "Users" of startup disk with properties {name:"xfer"}
duplicate "Users:neo:Projects" to folder "xfer" of folder neuname
duplicate "Users:neo:Library:Safari" to folder "xfer" of folder neuname
duplicate "Users:neo:Library:Scripts" to folder "xfer" of folder neuname
duplicate "Users:neo:Documents:Palm" to folder "xfer" of folder neuname
duplicate "Users:Neo:Sites:put in library-application support" to folder "xfer" of folder neuname
duplicate "Users:Neo:Sites:put in library-preferences" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:iBlog" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:webgrfx" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:neowaylanddotnet" to folder "xfer" of folder neuname
duplicate "Users:neo:Sites:frame pages" to folder "xfer" of folder neuname
duplicate folder neuname to "Users:neo:Documents:currents"
tell application "Disk Utility"
activate
end tell
say "I'm ready to make an image from the folder."
end tell

Have fun with it.

www.teknopagan.com
       



teknologi
© 2005 - 2008 All Rights Reserved