cra
mr

Creating a Read-only Mirror for your GitHub Server

You're viewing an archived post which may have broken links or images. If this post was valuable and you'd like me to restore it, let me know!

Recently we’ve been transitioning our git repositories to GitHub. We chose to go this route for a variety of reasons, but mostly because they have kickass pull requests, which we’re going to test run as code reviews. However, one of the requirements of this process was that our original git-server still remain functional, in at least a read-only state. This saves us the time of having to update deploy and other scripts which read from this mirror and perform various tasks.

I was a bit surprised when I originally searched around for this, as I was either failing horribly at Google (granted, my queries were “how to setup git-server mirror”), or there just wasn’t much information out there on it. After a bit of crawling I found what seems to be a pretty easy way to get the behavior we wanted. For a recap, here’s a checklist of what we needed:

#-

#-

So, given this, we created a simple bash script that runs on a 1 minute cron timer (it’s as close to real-time as we needed):

#!/bin/bash

mkdir -p  /var/git/mirrors/

cd /var/git/mirrors/

# clone our newly acquired GitHub mirror
git clone --mirror git@github.com:organization/repo-name.git

cd disqus.git

# Add our local remote
git remote add local /var/git/repositories/repo-name.git

# Unsure if we need to fetch from local, but let's do it anyways
git fetch origin
git fetch local

# push all changes to local using --mirror (ensures all refs in remotes are pushed)
git push local --mirror

Since we were already using gitosis for permissions, it was easy for us to deprecate the legacy repo by simply moving everyone into a readable group that lacks write privileges.

Would love to hear some feedback from avid git users if there’s a better way to do this.