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 chimeric.de 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!
- 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 # the API returns only 30 repos per page, let's check the first 10 pages repos='' for i in `seq 1 10` do 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" done [[ ! -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:
Update 2011-02-05: The script now works even if you have more than 30 repositories.