Bill Morefield My thoughts, discoveries, and occasional rambiings.

August 13, 2013

Managing Time Machine Backups to Windows Server Continued

Filed under: osx — Tags: , — Bill Morefield @ 10:13 am

Back in June I posted my experience getting Time Machine backups to work with a Windows Server as my main storage. It worked well, but had three problems. First I’d often get an error as it tried to connect to my server when I was away from my home network. I also had to manually mount the drive before the backups would run. Third, I sometimes found issues if I put the computer to sleep in the middle of a backup and woke it up off the home network.

To fix these issues I came up with a few scripts to address those. This first pair are designed to run when I come to my home network and consists of an Apple Script and a shell script.

I’d started these over a year ago, but never perfected them and lived with the limitations. I’d worked on them off an on, but didn’t complete them until I had to recreate my backup system around the time of that last post. The code for starting and stopping Time Machine came from http://apple.stackexchange.com/questions/11177/quicksilver-accessible-script-for-disabling-and-enabling-time-machine and the other code came from places now lost.

First the Apple Script to start Time Machine backups:

   1:  tell application "System Preferences" to activate
   2:  tell application "System Events"
   3:      tell process "System Preferences"
   4:              click menu item "Time Machine" of menu "View" of menu bar 1
   5:              tell button "ON" of window 1 to click
   6:      end tell
   7:  end tell
   8:  tell application "System Preferences" to quit
   9:   
  10:  tell Application "Finder"
  11:  Mount volume "cifs://<SERVER>/<SHARE>"
  12:  end tell

Lines 1 – 8 simply start Time Machine by in effect going into preferences and turning it on. Lines 10 – 12 then use mount the server share containing the disk image.

Next I need to mount the disk image. This is trickier than it might seem in my case as I had encrypted the image. I didn’t want to have to type the password each time since I’d already saved it in my Keychain. So I combined a script to pull the password for the disk image named MacBook-Backup.sparsebundle. This would need to change to the name of your disk image if running the script. I then pipe the output of that command to the mount command to mount the disk image. This in effects types in the password read from the Keychain to the prompt when I mount the drive. The path (here /Volumes/TimeMachine/MacBook-Backup.sparsebundle) would need to be changed if you use this script.

security find-generic-password -w -D "disk image password" -l "MacBook-Backup.sparsebundle" | hdiutil attach /Volumes/TimeMachine/MacBook-Backup.sparsebundle

Finally a script to turn off Time Machine.

   1:  tell application "System Preferences" to activate
   2:  tell application "System Events"
   3:      tell process "System Preferences"
   4:              click menu item "Time Machine" of menu "View" of menu bar 1
   5:              tell button "OFF" of window 1 to click
   6:      end tell
   7:  end tell
   8:  tell application "System Preferences" to quit

There are several ways to use these. I initially ran them manually when needed. For automation the best method I found was to use Keyboard Maestro’s ability to run scripts when a wireless network was either connected to or disconnected from. I ran the first two scripts when I connected to my home wireless network and the last when I disconnected from it. I used that process for about a week and a half and found it worked very well.

Since then though I’ve moved from the Windows Home Server to a new Synology NAS. This new NAS supports native Time Machine backups using AFS so I no longer need the disk image process I detailed here. It worked for me well over a year with only one problem and the scripts worked for about a month so I’d feel comfortable going back to them if the need arises.

Powered by WordPress