How to Automagically Sync Directories Across Multiple Macs with Git & Launchd

In an effort to minimize my downtime when the funny noises this MacBook Pro is making finally amount to something – I’ve wired up a git repository to OS X’s native launchd service. The git repository hold all of my active projects – whether development projects with their own repos, research projects, consulting project. Everything.

Right now, there’s a Mac mini holding the shared repo with a MacBook Pro and a MacBook Air pushing and pulling to it.

  1. Set up SSH keys the laptops and server (I like GitHub’s instructions)
  2. Set up the repo on the server
    mkdir active-projects.git
    cd active-projects.git
    git --bare init
  3. set up a repo on both client macs
    cd ~/Documents/Projects
    git init
    git add .
    git commit -a -m "initial commit"
  4. create the active-projects.sh backup script in your ~/Documents directory #!/bin/sh
    DATE=`date -u`
    cd /Users/YOUR-USER-NAME/Documents/Projects
    git pull origin master
    git add .
    git commit -a -m "Active Project Sync - $DATE"
    git push origin master
  5. make active-projects.sh executable chmod +x project-backup.sh
  6. Make the active-projects-backup.plist file for launchd

    < ?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">
    <dict>
    <key>Label</key>
    <string>YOURNAME.rsync.backup</string>
    <key>LowPriorityIO</key>
    <true />
    <key>Program</key>
    <string>/Users/YOUR-USER-NAME/Documents/active-projects.sh</string>
    <key>ProgramArguments</key>
    <array>
    <string>active-projects.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true />
    <key>QueueDirectories</key>
    <array>
    <string>/Users/YOUR-USER-NAME/Documents/Projects</string>
    </array>
    <key>WorkingDirectory</key>
    <array>
    <string>/Users/YOUR-USER-NAME/Documents/Projects</string>
    </array>
    </dict>
  7. save the active-projects-backup.plist file to ~/Library/LaunchAgents and load it up launchctl load ~/Library/LaunchAgents/active-projects-backup.plist
  8. Now, whenever a change is made in your ~/Documents/Projects – it will be automatically committed to the git repo and propagated to all computers connected to that repo. Like magic.

Thanks to culturedcode’s instructions for syncing Things with git & Launchd.

One thought on “How to Automagically Sync Directories Across Multiple Macs with Git & Launchd

  1. Hi Garrick

    Thanks for the tutorial on setting up git
    I’m trying to follow the instructions to setup git auto sync on a couple of Macs and I have some questions. I think there might be some steps missing:

    How is the server setup? Do I enable a Git service somewhere?
    How do I connect the clients to the remote repo?
    Is the LaunchDaemon for the server or the clients or both?
    Does the LaunchDaemon run at intervals or when I write to the directory?

    I would really appreciate if find the time to answer me, I’ve tried everything i can think of and I’m kinda stuck

Comments are closed.