Posted By

slinky on 03/14/12


Tagged

Bash script repository git github forks organisation


Versions (?)

bash script to backup github organisation repositories and member forks


 / Published in: Bash
 

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

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

  1. #!/bin/bash
  2.  
  3. #
  4. # Crude script to backup an organisations repositories and its members forks.
  5. #
  6.  
  7. # Name of organisation
  8. ORG="change"
  9.  
  10. # Needed to talk to API
  11. USER="changeme"
  12. PASS="changeme"
  13.  
  14. API="https://api.github.com"
  15.  
  16. BACKUP_DIR="/data/github_backups"
  17. TSTAMP=`date "+%Y%m%d-%H%M"`
  18.  
  19. GITCMD="git clone --mirror [email protected]:"
  20. #GITCMD="git clone --mirror git://github.com/"
  21. #GITCMD="git clone --mirror https://${USER}@github.com/"
  22.  
  23. mkdir -p $BACKUP_DIR
  24.  
  25. # crudely get list of repositories in the organisation
  26. echo "# getting list of repos..."
  27. REPOLIST=`curl --silent -u $USER:$PASS ${API}/orgs/${ORG}/repos -q | grep name | awk -F': "' '{print $2}' | sed -e 's/",//g'`
  28.  
  29. # for each repository, backit and forks up
  30. for REPO in $REPOLIST; do
  31.  
  32. # backup org repo
  33. echo "Backing up ${ORG}/${REPO}"
  34. TSTAMP=`date "+%Y%m%d-%H%M"`;
  35. ${GITCMD}${ORG}/${REPO}.git ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git
  36. tar zcf ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.tar.gz ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git
  37. rm -rf ${BACKUP_DIR}/${ORG}-${REPO}-${TSTAMP}.git
  38.  
  39. # backup forks
  40. echo "# getting list of forks..."
  41. FORKLIST=`curl --silent -u $USER:$PASS ${API}/repos/${ORG}/${REPO}/forks -q | grep login | awk -F': "' '{print $2}' | sed -e 's/",//g'`
  42. for F in $FORKLIST; do
  43. echo "+ Backing up fork $F/${REPO}"
  44. TSTAMP=`date "+%Y%m%d-%H%M"`;
  45. ${GITCMD}${F}/${REPO}.git ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
  46. tar zcf ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.tar.gz ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
  47. rm -rf ${BACKUP_DIR}/${F}-${REPO}-${TSTAMP}.git
  48. done
  49.  
  50. done
  51.  
  52. # clean up
  53. echo "pruning archive"
  54. #find $BACKUP_DIR -name '*.tar.gz' -mtime +3 -exec rm -fv {} \;
  55.  
  56. echo "Backup process completed"

Report this snippet  

You need to login to post a comment.