Return to Snippet

Revision: 56174
at March 14, 2012 08:36 by slinky


Initial Code
#!/bin/bash 

#
# Crude script to backup an organisations repositories and its members forks.
#

# Name of organisation
ORG="change"

# Needed to talk to API
USER="changeme"
PASS="changeme"

API="https://api.github.com"

BACKUP_DIR="/data/github_backups"
TSTAMP=`date "+%Y%m%d-%H%M"`

GITCMD="git clone --mirror [email protected]:"
#GITCMD="git clone --mirror git://github.com/"
#GITCMD="git clone --mirror https://${USER}@github.com/"

mkdir -p $BACKUP_DIR

# crudely get list of repositories in the organisation
echo "# getting list of repos..."
REPOLIST=`curl --silent -u $USER:$PASS ${API}/orgs/${ORG}/repos -q | grep name | awk -F': "' '{print $2}' | sed -e 's/",//g'`

# for each repository, backit and forks up
for REPO in $REPOLIST; do

   # backup org repo
   echo "Backing up ${ORG}/${REPO}"
   TSTAMP=`date "+%Y%m%d-%H%M"`; 
   ${GITCMD}${ORG}/${REPO}.git ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git
   tar zcf ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.tar.gz ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git
   rm -rf ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git

   # backup forks
   echo "# getting list of forks..."
   FORKLIST=`curl --silent -u $USER:$PASS ${API}/repos/${ORG}/${REPO}/forks -q | grep login | awk -F': "' '{print $2}' | sed -e 's/",//g'`
   for F in $FORKLIST; do
      echo "+ Backing up fork $F/${REPO}"
      TSTAMP=`date "+%Y%m%d-%H%M"`; 
      ${GITCMD}${F}/${REPO}.git ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
      tar zcf ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.tar.gz ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
      rm -rf ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
   done

done

# clean up 
echo "pruning archive"
#find $BACKUP_DIR -name '*.tar.gz' -mtime +3 -exec rm -fv {} \;

echo "Backup process completed"

Initial URL
http://www.danslinky.co.uk/?p=53

Initial Description
A crude script to query the GitHub API for a given Organisation, and backup its repositories and associated members forks.

Initial Title
bash script to backup github organisation repositories and member forks

Initial Tags
Bash, script, git

Initial Language
Bash