Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@davidB
Created November 6, 2012 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidB/4024777 to your computer and use it in GitHub Desktop.
Save davidB/4024777 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#require PyGithub (pip install PyGithub)
#see https://github.com/jacquev6/PyGithub
import sys
import urllib2
import os
import os.path
from pipes import quote
from github import Github
try:
import json
except ImportError:
import simplejson as json
if len(sys.argv) != 3:
print "Usage: %s <username> <directory>" % sys.argv[0]
sys.exit(2)
(username, backupdir) = sys.argv[1:]
if not os.path.isdir(backupdir):
print "Error: '%s' does not exist or is not a directory" % backupdir
sys.exit(3)
#f = urllib2.urlopen('https://api.github.com/users/%s/repos' % username)
#repos = json.load(f)
g = Github( "login", "password" )
for repo in g.get_user().get_repos():
#for repo in repos:
repo_name = repo.name
repo_url = repo.ssh_url
dir = os.path.join(backupdir, os.path.basename(repo_name))
cwd = os.getcwd()
if not os.path.exists(dir):
print "adding new repository: %s" % repo_url
os.mkdir(dir)
os.chdir(dir)
os.system('git --bare init')
os.system('git remote add --mirror origin %s' % quote(repo_url))
else:
os.chdir(dir)
print "fetching repository: %s" % repo_url
os.system('git fetch -q')
os.system('git remote prune origin')
os.chdir(cwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment