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!
#!/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 checkout ${branch} git pull github ${branch} done cd ${backup_dir} done # vim:ts=4:sw=4:et:enc=utf-8:
Related Articles:
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:52This morning I found the script no longer working, telling me I'm “in the middle of a conflicted merge”.
After some investigation I think the problem is that git branch will only change into the given branch, when the branch is newly created. Otherwise it stays where it is and starts to pull all remote branches into the local one which will create conflicts.
Placing a
git checkout ${branch}
between git branch and git pull seems to work.
Nice one chi… :)