Subscribe to RSS feed - electronic brain surgery since 2001

Github Mirror

Michael Klier recently decided to shut down his blog. Luckily he provides a tarball of all his posts and used a liberal license for his contents. With his permission I will repost a few of his old blog posts that I think should remain online for their valuable information.

This post was originally posted November, 30th 2009 at and is licensed under the Creative Commons BY-NC-SA License.

Yesterday I thought it would be a good idea to automatically mirror my github repositories, just in case sth. goes bad ;-). So I hacked togeter the following little bash script. It uses the github API to fetch the repository information, and the ever-handy xmlstarlet to extract the needed data. It also keeps track of remote branches, but it contains no error logic atm., so use it at your own risk (as a daily cronjob for example). The rest is pretty selfexplanatory, just set your username, API token and backup directory in the script, make it executable and you're done. Comments for possible improvements are of course welcome!
# github mirror script
# @author Michael Klier <>
# the API returns only 30 repos per page, let's check the first 10 pages
for i in `seq 1 10`
    rep="$(wget --quiet --post-data="login=${login}&token=${token}" -O - ${api_url}/repos/show/${login}?page=$i | xmlstarlet sel -T -t -m '//repository' -v name -o ' ')"
    repos="$repos $rep"
[[ ! -d ${backup_dir} ]] && mkdir -p ${backup_dir}
cd ${backup_dir}
for repo in $repos;
    branches="$(wget --quiet --post-data="login=${login}&token=${token}" -O - ${api_url}/repos/show/${login}/${repo}/branches | xmlstarlet sel -T -t -m '//branches/*' -v 'name()' -o ' ')"
    if [ ! -d ${repo} ]; then
        git clone -o github${login}/${repo}.git ${repo}
    cd ${repo}
    for branch in $branches;
        git branch --track ${branch} github/${branch} 2>/dev/null
        git checkout ${branch}
        git pull github ${branch}
    cd ${backup_dir}
# vim:ts=4:sw=4:et:enc=utf-8:

Update 2011-02-05: The script now works even if you have more than 30 repositories.

guestpost,, github, git, bash
Similar posts:
Posted on Tuesday, December the 14th 2010 (8 years ago).


blog comments powered by Disqus