10.5: Safer sleep during networked Time Machine backups

Nov 19, '08 07:30:00AM

Contributed by: jsteinhu

The problem: You have Time Machine set up using a sparsebundle on a network drive, such as a Time Capsule or an AirPort Extreme-connected hard drive. Most of the time, everything works great. Consider this reproducible scenario, however:

  1. You are working away at home and a hourly backup starts. Time machine automagically mounts the sparsebundle.
  2. You need to leave and put your laptop to sleep, not paying attention to the fact that time machine is working.
  3. You move your laptop to work/school and wake it up.
  4. The sparsebundle is still mounted, but obviously does not work. Finder and Spotlight grow increasing stuck until you have to reboot your machine. It occurs to you that the image back home was not closed properly and that eventually, this will corrupt your backups!
Solution:
  1. Install SleepWatcher (I used the MacPorts build; don't forget to specify the server variant!).
  2. Modify /opt/local/etc/rc.sleep or /etc/rc.sleep to include these two lines:
    logger -t $0 "Ejecting any mounted Time Machine images"
    hdiutil info -plist | grep /Volumes | sed 's/<string>/\"/' | sed 's/<\/string>/\"/'|xargs -I {} bash -c "if test -e \"\$0/Backups.backupdb\";  then hdiutil eject \"\$0\"; fi" {}
I am sure that someone better than I at script-fu can clean up that mess, but basically, it finds disk images that are mounted and are being used for Time Machine, and ejects them. backupd notices this automatically, and cancels the backup gracefully. The image is then safely offline before the machine disconnects from the network, as validated by console logs.

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20081115172028293