Skip to content

Instantly share code, notes, and snippets.

@camharris
Created April 26, 2017 23:16
Show Gist options
  • Save camharris/9780926f703e8dd3d929bf5e2f88ed65 to your computer and use it in GitHub Desktop.
Save camharris/9780926f703e8dd3d929bf5e2f88ed65 to your computer and use it in GitHub Desktop.
backup-github-orgs.py
#!/usr/bin/python
import subprocess
import os
import simplejson
import urllib2
import base64
import re
# Simple script to backup all our repos under all our orginizations
# This script requires the system user running it to have an ssh-key installed for the github user in question
# Array of Orginizations to backup
orgs = [ 'MyOrg', ]
# Username to use
username = ""
# Password to use
password = ""
# Backup Path IE: /opt/backups/
backup_path = ''
# Don't change anything below this line
data = []
for o in orgs:
if not os.path.exists(backup_path + o):
os.makedirs(backup_path + o)
os.chdir(backup_path + o)
print "------------------- pulling repos for organization "+ o
request =urllib2.Request("https://api.github.com/orgs/" + o + "/repos?per_page=20")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
mylink = response.info().getheader('Link')
#get number of pages
pages = int(re.findall( r'(?<=\<)(.*?)(?=\>)', mylink.split(' ')[2] )[0][-1:])
counter = 1;
while (counter < pages):
nextrequest = urllib2.Request("https://api.github.com/orgs/" + o + "/repos?per_page=20&page=" + str(counter))
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
nextrequest.add_header("Authorization", "Basic %s" % base64string)
response2 = urllib2.urlopen(nextrequest)
data += simplejson.load(response2)
counter += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment