Automagic Airdisk Timemachine Backups

Picture 2

I’ve always loved timemachine, but have always hated how hard it is to get working right with an airdisk. After a few lines of code, it’s now working seamlessly.
The first problem was when the computer goes to sleep while it’s backing up to an airdisk. Timemachine freaks out and often causes you to force restart, and sometimes corrupts the backup image. This was easy to solve thanks to Mac OS X Hints. All you need to do is add the following lines of code to /etc/rc.sleep

hdiutil info -plist | grep /Volumes | sed 's//\"/' | sed 's/<\/string>/\"/'|xargs -I {} bash -c "if test -e \"\$0/Backups.backupdb\"; then hdiutil eject \"\$0\"; fi" {}

and
hdiutil eject /Volumes/your-backup-volume


The first line finds which disk image timemachine is using and ejects it. The second line ejects the physical volume. Just replace the
your-backup-volume with the name of your backup volume.

The next problem is when you wake up your computer and timemachine tries to backup. It will give you an error because it won’t automatically mount the volume for you. It’s easy to mount the volume through command line, but if you have a laptop, then everything will get screwed up when you’re on another person’s network. To solve this, a script is needed to check the SSID of the network. If the SSID is the same as your home network, then the script mounts the drive and checks to see if timemachine is turned on. If it isn’t then it turns it on and immediately runs a backup. If the SSID doesn’t match your home network, then it turns timemachine off. Here’s the applescript code that I used:

delay 5
set backup to do shell script "defaults read /Library/Preferences/com.apple.TimeMachine AutoBackup"
set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk '/ SSID/ {printf $2 $3 $4}'"
if SSID = "YOUR-NETWORK-HERE" then
open location "afp://IP-ADDRESS-OF-YOUR-DRIVE"
if backup = "0" then
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -boolean TRUE"
do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper"
end if
else
do shell script "defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -boolean FALSE"
end if


Replace the “YOUR-NETWORK-HERE” with the SSID of your network (no spaces). Also, replace the “$2 $3 $4” with the number of words in your SSID. For example, if your SSID is “Voiding Warranties”, then you would replace “$2 $3 $4” with just “$2 $3”. If your SSID is “Voiding Warranties Wireless Router”, then would would replace “$2 $3 $4” with “$2 $3 $4 $5”. Make sure to replace the “IP-ADDRESS-OF-YOUR-DRIVE” with the actual IP address of your drive.

Now, all that is left to do is to run the applescript when the computer wakes up. The first way I tried doing this was by adding the following line to /etc/rc.wakeup

/Users/Jordan/Desktop/applescript/TimeMachineAuto.app/Contents/MacOS/applet


however for some reason this didn’t work. It would open up the app, but the app would immediately quit. To fix this, I made another applescript that opened up the original applescript. I then added the following line to /etc/rc.wakeup

/Users/Jordan/Desktop/applescript/TimeMachineAutoLauncher.app/Contents/MacOS/applet


If anyone has a better solution to this, please leave a comment below.