A Handy GitHub Backup Script

Posted by Baron Schwartz on Mar 7, 2015 5:11:00 AM

We use GitHub to host all of our source code at VividCortex. We use the GitHub API in a very simple way to ensure we’ve backed up all of the source code, because we add new repositories often.

GitHub_Backup_Script

Here’s a little script you might find helpful to keep an up-to-date clone of all of your organization’s GitHub repositories. You’ll need to change the values at the top of the script:

#!/bin/sh
LOGIN=YOURLOGIN
TOKEN=YOURTOKEN
ORG=YOURORG
DONE=0
PAGE=0
# sample output:
#  "total_private_repos": 50,
#  "public_repos": 41,
NUMREPOS=$(curl -s "https://$LOGIN:$TOKEN@api.github.com/orgs/$ORG" | awk '
    /public_repos/{t+=substr($2, 0, length($2)-1)}
    /total_private_repos/{t+=substr($2, 0, length($2)-1)}
    END{print t}')
while [ $DONE -lt $NUMREPOS ]; do
    # Sample:
    #     "full_name": "VividCortex/wlr",
    for repo in $(curl -s "https://$LOGIN:$TOKEN@api.github.com/orgs/$ORG/repos?type=all&page=$PAGE" | awk '/full_name/{print substr($2, 2, length($2)-3)}'); do
        if [ ! -e "$HOME/repos/${repo#*/}" ]; then
            (cd "$HOME/repos" && git clone git@github.com:$repo.git)
        else
            (cd "$HOME/repos/${repo#*/}" && git pull || echo "^^ $repo")
        fi
        DONE=$(($DONE + 1))
    done
    PAGE=$(($PAGE + 1))
done

A gist of this code is here. Enjoy!

Image credit: https://www.flickr.com/photos/swanksalot/2704017177/

Recent Posts

Posts by Topic

see all