So I recently realized I was foolishly not backing up the database dumps for a project, and figured a good stopgap / semi-permanent solution would be to just rsync the backups that were stored on the server on a regular basis. Of course, the way to run periodic scripts in OS X is launchd, not cron, so I figured I’d use this as an excuse to try that route.

I had everything seemingly set up, but I kept getting permission denied errors from rsync, even when exactly the same rsync command run manually from the command line worked fine. The problem turned out to be that my private SSH key was password-protected. I had forgotten about this b/c Terminal and Keychain had been magically handling that password for some time. So I deleted that entry in Keychain and made a new public/private key pair. Once that was done, all went smoothly.

Here’s a summary of what I did.

  1. Set up passwordless SSH without a password on the private key.
  2. Added the following plist to ~/Library/LaunchAgents/

    <?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>org.projectname.backup</string>
      <key>ProgramArguments</key>
      <array>
        <string>/opt/local/bin/rsync</string>
        <string>-avz</string>
        <string>projectname.org:/path/to/backup/</string>
        <string>/path/to/local/backup/</string>
      </array>
      <key>LowPriorityIO</key>
      <true/>
      <key>Nice</key>
      <integer>1</integer>
      <key>StartCalendarInterval</key>
      <dict>  
        <key>Hour</key>
        <integer>22</integer>
        <key>Minute</key>
        <integer>00</integer>
      </dict>
    </dict>
    </plist>
  3. Loaded the daemon script into launchd:

    launchctl load ~/Library/LaunchAgents/org.projectname.backup
  4. Tested it and watched the log in Console:

    launchctl start org.projectname.backup

Now it should run every day at 10pm.

Here are some resources I found useful:

Comments are closed.