How to run a nightly backup job on Snow Leopard Server via launchd

launchdosx-serverplist

I have been trying to configure a nightly backup job on my Mac Mini server, and after a lot of Googling and digging and man-page'ing, I figured out that launchd seemed to be the correct too, and tried setting up my own plist fil. I've come up with this (stored in /Library/LaunchDaemons/dk.revealit.NightlyRSyncBackup.plist):

<?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>dk.revealit.NightlyRSyncBackup</string>
        <key>ProgramArguments</key>
        <array>
                <string>/var/root/run-rsync</string>
        </array>
        <key>LowPriorityIO</key>
        <true/>
        <key>Nice</key>
        <integer>1</integer>
        <key>WorkingDirectory</key>
        <string>/var/root</string>
        <key>RunAtLoad</key>
        <false/>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>3</integer>
                <key>Minute</key>
                <integer>15</integer>
        </dict>
</dict>
</plist>

Problem is, it doesn't work. I can load it with launchctl, and it appears on launchctl list with exit status 0. But if I check my backup files, they are not updated.

Any clues as to what I'm doing wrong (assuming my backup script works as it should)?

Best Answer

Several things come to mind:

  • Did you wait overnight for it to run? launchctl load won't make it run out of sequence -- if you want to make it run at some time other than 3:15 am, use sudo launchctl start dk.revealit.NightlyRSyncBackup after it's loaded.

  • Does the run-rsync script fire off any background processes, or does it do everything inline? 'Cause if the script exits and there are background processes still going, launchd thinks something has gone wrong and kills the background processes. If you don't want it to do this, add <key>AbandonProcessGroup</key><true/> to the .plist.

  • Does the script depend on PATH including any nonstandard directories (e.g. /opt/local/bin or something like that)?

One useful debugging technique is to add something like:

<key>StandardOutPath</key>
<string>/var/root/NightlyRSyncBackup.out</string>
<key>StandardErrorPath</key>
<string>/var/root/NightlyRSyncBackup.err</string>

to the .plist and see what shows up in those files.