July, 2013


130729

Health Quality Ontario Website Wins 2013 Summit Creative Award

Photograph of Mike holding award. Inset zoom detailed award.

Health Quality Ontario was the recipient of the Silver Summit International Award in the Government Website category. Summit International Awards recognize "best in class" creative and communications excellence. HQO's site competed with over 5,000 submissions from 23 countries. I am very proud to have received this award; it is a reflection of the hard work and dedication I put towards designing and developing something useful and engaging for all citizens living in Ontario. I'd like to thank my colleagues at HQO for their patience and assistance with making this project what it is today.

If you haven't had a chance to check the website out, go here:
http://hqontario.ca/

Share Button

130724

Time Machine Auto-mount/unmount Drive OS X

I have a USB backup drive connected through my monitor to my Macbook Pro and I often find myself wanting to quickly unplug and go without the consequences of the drive not being ejected properly. As well, I need the drive to be mounted so that Time Machine can do its automatic backups.

Well I found a way to avoid having to manually mount and unmount (eject) my drive by using shell scripts and LaunchAgents to achieve the task. The added benefit is that I have control over the timing of the frequency of the Time Machine backups too!

Note: This works on OS X Lion or later. There is one caveat, in that when attempting to restore files from your Time Machine drive, you will need to disable the launch agents, as well you should re-activate Time Machine in the system preferences so that the system recognizes the drive for restore purposes.

Step 1

Create a plain text file named timemachine_mounter.sh with the following contents:

#!/bin/bash
# Set the drive name that we mount for our backups 
vol_mount="Time Machine"
# Conditional argument: if mounted entry has the volume name in the variable string
if ! mount | grep "$vol_mount" ; then
	# Drive isn't mounted, so: mount. A LaunchAgent will pick this OnMount action up and run the backup script
	diskutil mount "$vol_mount"
else
	# Drive is mounted, so: run the backup script
	timemachine_backup.sh
fi

Save the file in the /usr/local/bin/ directory at the base of your system drive. You may need to first save the file to your desktop then use finder to move it to that directory, it will ask to authenticate. Or use sudo in Terminal to move the file there instead, and make sure that ownership belongs to you (the user that logs in), in my case the assigned user:group was mike:staff.

Step 2

Create a plain text file named timemachine_backup.sh with the following contents:

#!/bin/bash
# Set the drive name that we mount for our backups 
vol_mount="Time Machine"
# Conditional argument: if mounted entry has the volume name in the variable string
if mount | grep "$vol_mount" ; then
	# Drive is mounted, so: backup and then eject after
	tmutil startbackup -b && 
	diskutil unmount "$vol_mount"
fi

Save the file in the /usr/local/bin/ directory again with the same ownership credentials as the previous script.

Step 3

Create a plain text file named com.apple.TimeMachine_OnLoadSchedule.plist with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.apple.TimeMachine_OnLoadSchedule</string>
	<key>RunAtLoad</key>
	<true/>
	<key>StartInterval</key>
	<integer>7200</integer>
	<key>Program</key>
	<string>/usr/local/bin/timemachine_mounter.sh</string>
</dict>
</plist>

Save the file in the ~/Library/LaunchAgents/ directory inside your User folder/directory (~/).

What the above file does is register in the system to run the mounter script:

  • when I first log in
  • consecutively every 2 hours

Step 4

Create a plain text file named com.apple.TimeMachine_OnMount.plist with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.apple.TimeMachine_OnMount</string>
	<key>StartOnMount</key>
	<true/>
	<key>Program</key>
	<string>/usr/local/bin/timemachine_backup.sh</string>
</dict>
</plist>

Save the file in the ~/Library/LaunchAgents/ directory as well.

What the above file does is register the system to run the Time Machine backup script whenever the drive gets mounted. This can happen when:

  • I physically connect the drive
  • the scheduler LaunchAgent above, runs the driver mounter script

In Terminal, make sure the shell scripts are executable, unload the default plist (if you didn't turn off your Time Machine automatic backups in the System Preferences) and finally load the new LaunchAgent in your local LaunchAgent startup folder:

chmod +x /usr/local/bin/timemachine_*.sh
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.backupd-auto.plist
launchctl load ~/Library/LaunchAgents/com.apple.TimeMachine_OnMount.plist
launchctl load ~/Library/LaunchAgents/com.apple.TimeMachine_OnLoadSchedule.plist

Lastly...

You can change the com.apple.TimeMachine_OnLoadSchedule.plist's integer value for the starting interval 7200 integer to something more reasonable (e.g., 3600 seconds is one hour,... multiply at will.) I have my backups run every two hours.

The good thing about using a LaunchAgent is that:

  • if your computer goes to sleep or is shut off, the backup will begin at the next time you log in and reset the countdown from that point onward
  • if Time Machine is already running it won't try to run the process until the next projected sync time
  • if the drive is freshly connected or mounted already, Time Machine will begin backing up your changes and eject after its done
Share Button