2009/11/30

Github Mirror

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!

githubmirr.sh
#!/bin/bash
# github mirror script
# @author Michael Klier <chi@chimeric.de>
 
login=USERNAME
token=APITOKEN
backup_dir=BACKUPDIR
 
api_url=http://github.com/api/v2/xml
 
repos="$(wget --quiet --post-data="login=${login}&token=${token}" -O - ${api_url}/repos/show/${login} | xmlstarlet sel -T -t -m '//repository' -v name -o ' ')"
 
[[ ! -d ${backup_dir} ]] && mkdir -p ${backup_dir}
cd ${backup_dir}
 
for repo in $repos;
do
    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 git@github.com:${login}/${repo}.git ${repo}
    fi
 
    cd ${repo}
    for branch in $branches;
    do
        git branch --track ${branch} github/${branch} 2>/dev/null
        git pull github ${branch}
    done
    cd ${backup_dir}
done
 
# vim:ts=4:sw=4:et:enc=utf-8:

Related Articles:

Comments

1

Nice one chi… :)

Mischa The Evil
2009/11/30 21:55
2

Great idea, thanks for the script.

gregf
2009/12/01 04:12
3

That's awesome, thanks. I put in my crons :)

Mattias Fliesberg
2009/12/08 09:52
4

When you change the clone line to

git clone -o github git://github.com/${login}/${repo}.git ${repo}

you can pull from the read only repo and don't need to setup any SSH keys.

2010/01/06 13:52



ZQHEY