Created
July 7, 2015 07:05
-
-
Save tzach/32f2b791ff0281f6e6a7 to your computer and use it in GitHub Desktop.
Backup user repos from github, including wiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please add 'gitdb' to the list of pip packages