====== Github Mirror ======
Yesterday I thought it would be a good idea to automatically mirror my [[http://github.com/chimeric|github repositories]], just in case sth. goes bad ;-). So I hacked togeter the following little bash script. It uses the [[http://develop.github.com/|github API]] to fetch the repository information, and the ever-handy [[http://xmlstar.sourceforge.net/|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
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:**
blog chimeric
tags bash, scripting, git