lauantai 2. elokuuta 2014

Doing backups and creating disk images

There's a reason why I keep Time Machine's automatic backing up off. Cause it's unreliable and confusing. I had this major problem in Snow Leopard, where Time Machine eventually took 4-6 hours to complete, for whatever reason. I guess it was related to it's incremental features, which kept getting more complex the more your used it. This is more likely fixed in Mavericks, but my trust was yet again broken and not easily fixed so I've kept using my own fix-script that I did for the Snow Leopard.



The fix is basically that I manually start backup when I'm putting my computer to sleep, but with a script that makes it as simple as only one dialog box asking: "Backup before sleep?" So answering "Yes" would mount my external disk, start Time Machine -backing up, wait for it to complete, write a log when it's done, run my quit sequence -script which also unmounts the external disk and finally sleep. With this fix I reduce the amount of backups and stay completely aware about when I've backed up the system and what to expect if I need to find something from the backup disk.

Here's the backup script:

--mount the disk
set failedMount to false
set volumeNames to paragraphs of (do shell script "ls /volumes/")
tell application "Finder"
try
mount volume "afp://audiodrive._afpovertcp._tcp.local/audiodrive" --you'll get this address by checking your disk's basic info when you've mounted it.
end try
end tell
set counter to 1
repeat while volumeNames does not contain "audiodrive" --my disk is called audiodrive, yours is something else.
set volumeNames to paragraphs of (do shell script "ls /volumes/")
delay 1
set counter to counter + 1
if counter > 15 then
set failedMount to true
exit repeat
end if
end repeat

--do the backup
if failedMount is false then
repeat 2 times
activate application "System Preferences"
delay 0.5
end repeat
tell application "System Events" to tell process "System Preferences"
click menu item "Time Machine" of menu "View" of menu bar 1
delay 1
set backupInProgress to ""
try
set backupInProgress to value of static text 4 of group 1 of window "Time Machine"
end try
if backupInProgress is "" then
do shell script "tmutil startbackup"
end if
delay 2
repeat while get value of static text 4 of group 1 of group 1 of window "Time Machine" is not "" --more info about this below the script
delay 5
end repeat
delay 0.5
tell button "OFF" of window 1 to click --just making sure it stays OFF
end tell
delay 0.5
tell application "System Preferences" to quit
tell application "Finder"
try
eject disk "audiodrive"
end try
end tell

--unmount the disk
set counter to 1
repeat while volumeNames contains "audiodrive"
set volumeNames to paragraphs of (do shell script "ls /volumes/")
delay 1
set counter to counter + 1
if counter > 15 then
exit repeat
end if
end repeat
end if

There's this interesting way to check if the backup is still running or not. Since there's not really a clear indicator anywhere that you could interpret as a sort of "on/off" state, you have to read the Time Machine's messages directly.

There's a static text element that exists all the time, but contains text only when the backup is running. So the script is checking whether that text element is empty or not. How to find these text elements? I use this tool: http://pfiddlesoft.com/uibrowser/

I also do a second monthly backup as a disk image. The script before the question "Backup before sleep?" checks the day of the month and if it's 28th or later it'll pop up another question: "End of the month, let's do an image?". By answering "Yes" it'll mount the external hard drive, read through a bunch of important folders (Applications, Desktop etc.) and write every item there is on a log file as a list. Then it'll automatically suggest a name for the disk image, like 140802_antonvalle_system_backup and after that it'll start doing the disk image with Disk Utility.

--check if it's time for creating a disk image
set imageOn to "No"
if day of (current date) is greater than 28 then
set imageOn to (button returned of (display dialog "End of the month, let's do an image?" buttons {"No", "Yes"} default button "Yes" with icon 0))
end if
if imageOn is "No" then
set backupOn to (button returned of (display dialog "Backup before sleep?" buttons {"Cancel", "Yes", "No"} default button "No"))
end if

Here's the script to write a list of contents of important folders:

set thisDay to ((do shell script "date '+%Y%m%d'") - 20000000) as string
set myLogFile to open for access file ((path to desktop as text) & thisDay & "_my_system_backup") with write permission
write thisDay & " SYSTEM BACKUP IMAGE" & return to myLogFile starting at eof
--display dialog ""
addFolder(myLogFile, "APPLICATIONS", (path to startup disk as text) & "Applications:")
addFolder(myLogFile, "SERVICES", (path to home folder as text) & "Library:Services:")
addFolder(myLogFile, "DESKTOP", (path to home folder as text) & "Desktop:")
close access myLogFile

on addFolder(getMyLogFile, title, pathToFolder)
write return & "_____" & title & "_____" & return to getMyLogFile starting at eof
set listOfNames to {}
try
tell application "Finder"
set filelist to every item of the folder pathToFolder
repeat with currentFile in filelist
set currentFileName to (the name of currentFile)
set modFileDate to (the modification date of currentFile) as text
tell application "System Events" to set ChangeDate to modification date of currentFile
set modFileDatePrint to text -2 thru -1 of (year of ChangeDate as text) & text -2 thru -1 of ("00" & (month of ChangeDate as number)) & text -2 thru -1 of ("00" & day of ChangeDate)
copy modFileDatePrint & currentFileName to the end of listOfNames
write modFileDatePrint & " - " & currentFileName & return to getMyLogFile starting at eof
end repeat
end tell
end try
end addFolder

To make the actual disk image I use GUI scripting to actually push the buttons in Disk Utility. I'll write about GUI scripting in another post.

Making a disk image is time consuming (for me about 4-6 hours) and I don't really want anything to interrupt it so the script quits cloud applications and such programs which might recieve data from internet and confuse the image creation. I've also left unwritten any kind of monitoring script for the disk image creation process.

Ei kommentteja:

Lähetä kommentti