10.5: Disable Spotlight during Time Machine backups

Oct 28, '08 07:30:00AM

Contributed by: matzh

I am using Time Machine with a Western Digital MyBook World, and had the hardest time getting it to make backups (even incremental ones of only a few megabytes) with reasonable speed. Apart from having to turn off any virus scanner, Spotlight tried to index the backup drive, which made it unbearably slow. I was not able to add the backup mount to the Privacy tab in the Spotlight System Preferences panel -- neither with the preferences pane, nor with any mdutil commands.

So I had to turn it off whenever I was doing the backup with this command:

launchctl unload /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
After the backup had finished, I then reloaded the daemon. If you want to use Spotlight and scheduled backups, this is not really practical. So I found a blunt force method that works to me.

First, a daemon runs a script every minute to check if a process with backupd (the Spotlight process) in its name is running. If it is, it stops the Spotlight process, then turns it back on when it no longer finds the backupd process. Here's the script:

#!/bin/sh

pn=`ps -e | grep backupd | wc -l`
flagfile="/full/path/to/some/flagfile"
if [ $pn -gt 1 ] ; then
  if [ ! -f $flagfile ] ; then
  launchctl unload /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
  date > $flagfile
  fi
fi
if [ $pn -eq 1 ] ; then
  if [ -f $flagfile ] ; then
  launchctl load /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
  rm -f $flagfile
  fi
fi
(I am not that good at shell scripts and have trouble with their if-test-then-&& syntax, so there may be a more compact method to replace the nested ifs.) Use chmod 755 on the shell script to make it executable, and save it somewhere safe.

The flagfile can be any valid and accessible path and it shows when this script has triggered the unloading of the indexing daemon. Omitting this would cause tons of com.appl....mds: Already loaded messages to be sent to the console. To make this work, write another daemon that calls this script every 60 seconds:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/
PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.acme.disablespotlightonbackup</string>
        <key>OnDemand</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/full/path/to/shell/script/created/above</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Munute</key>
                <integer>1</integer>
        </dict>
</dict>
</plist>
Save the plist to /Library » LaunchDaemons » com.acme.disablespotlightonbackup.plist. Also change its permissions with chmod 644, and use chown to set the ownership to root:admin, and then load it with sudo launchctl load /Library....backup.plist. You may want to change acme to your user name, although it's not strictly necessary. This works for me like a charm for me.

[robg adds: I haven't tested this one.]

Comments (14)


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