Skip to content

Instantly share code, notes, and snippets.

@tzach
Created July 7, 2015 07:05
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 tzach/32f2b791ff0281f6e6a7 to your computer and use it in GitHub Desktop.
Save tzach/32f2b791ff0281f6e6a7 to your computer and use it in GitHub Desktop.
Backup user repos from github, including wiki
#!/usr/bin/env python
# prerequisites
# sudo pip install pyopenssl ndg-httpsclient pyasn1 gitpython --upgrade
#
# Usage: ./backup-repos.py user
#
import requests
import json
import os
import sys
from git import Repo
from time import gmtime, strftime
USER = sys.argv[1]
URL = 'https://api.github.com/users/' + USER + '/repos'
now = strftime("%Y-%m-%d-%H-%M-%S", gmtime())
dir_name = USER + "_repo_backup_" + now
os.makedirs(dir_name)
directory = os.path.join(os.getcwd(), dir_name)
dictionary = requests.get(URL).json()
for repo in dictionary:
print(repo["clone_url"])
try:
Repo.clone_from(repo["clone_url"], os.path.join(directory, repo["name"]))
except:
print "cant clone repo " + repo["clone_url"]
if (repo["has_wiki"]):
base, ext = os.path.splitext(repo["clone_url"])
wiki = base + ".wiki" + ext
try:
Repo.clone_from(wiki, os.path.join(directory, repo["name"] + ".wiki"))
except:
print "no wiki " + wiki
@dorlaor
Copy link

dorlaor commented Jul 7, 2015

Please add 'gitdb' to the list of pip packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment