A few weeks ago, I wrote about forcing Time Machine drives to unmount. From a comment to that post by bil_kleb, I learned about Bernhard Baehr's SleepWatcher program that provided a way to create a complete solution. Here's what I did.
Download and install SleepWatcher. There are two installs that have to be done in the right order (StartupItem last). Now whenever you restart SleepWatcher will start as well.
Modifiy your sudoers list. This allows umount to run as the superuser without a password (otherwise you have to type the superuser password everytime you put your Mac to sleep). Use the command sudo visudo to edit the sudoers file. If you don't know vi, get someone to help with this step. You want to add the following line to the end of the sudoers file:
%admin ALL= NOPASSWD: /sbin/umount
This tells the sudo program to allow everyone in the %admin group to execute the umount command as the superuser without a password.
Create a .sleep file. This is where you actually unmount the drive. Put the following in a .sleep account in your home directory (you'll have to create this file)
#/bin/bash # always unmount the drive before sleep sudo /sbin/umount -f /Volumes/Phil\\ Backup
You'll want to change /Volumes/Phil\\ Backup to whatever the name of your backup drive is (look in /Volumes to find it). Make sure you make the .sleep file executable:
chmod u+x $HOME/.sleep
You can test it by executing the .sleep file from the command line. Your disk should unmount without asking for a password if you've done these steps correctly.
That's it. Now when you sleep your machine the Time Machine drive will unmount automatically. You can add other things to the .sleep file if you like to accomplish anything else you'd like done before the machine sleeps. SleepWatcher will also execute a .wakeup file for things you want done when the machine wakes up.
I've been using this for a few weeks now and it seems to work fine. Every time I sleep my machine, the drive unmounts cleanly as part of the sleeping process.
Anyone have good ideas for other things to do automatically on sleeping and waking? Now that I've got a new hammer, I've looking for new nails!