Return to Snippet

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

Initial Code

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

# Name of organisation

# Needed to talk to API


TSTAMP=`date "+%Y%m%d-%H%M"`

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

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


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

echo "Backup process completed"

Initial URL

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