#!/usr/bin/python import os import json import argparse import time import requests from functools import partial class GithubBackup(object): def __init__(self, parameters): self.folder = time.strftime("%Y%m%d%H%M%S") self.gists_backup_directory = os.path.join(parameters.directory, self.folder, 'gists') self.repos_backup_directory = os.path.join(parameters.directory, self.folder, 'repos') os.makedirs(self.gists_backup_directory) os.makedirs(self.repos_backup_directory) self.user = parameters.user self.errors = { "gists" : [], "repos" : [], } def fetch(self, url): R = requests.get(url) return json.loads(R.content) def clone(self, url, path): status = os.system("git clone '%s' '%s'" % (url, path)) == 0 if not status: if url.startswith("https://gist.github.com"): key = "gists" else: key = "repos" self.errors[key].append(url) return status def get_gists(self): url = "https://api.github.com/users/%s/gists" % self.user path = partial(os.path.join, self.gists_backup_directory) return all([ self.clone(item["git_pull_url"], path(item["id"])) for item in self.fetch(url) ]) def get_repos(self): url = "https://api.github.com/users/%s/repos" % self.user path = partial(os.path.join, self.repos_backup_directory) return all([ self.clone(item["clone_url"], path(item["name"])) for item in self.fetch(url) ]) def backup(self): if self.get_gists(): print "All Gists backuped OK to directory -> %s" % self.gists_backup_directory else: print "Errors encountered while cloning the following gists:" for url in self.errors["gists"]: print "* %s" % url if self.get_repos(): print "All repositories backuped OK to directory -> %s" % self.repos_backup_directory else: print "Errors encountered while cloning the following repositories:" for url in self.errors["repos"]: print "* %s" % url if __name__ == "__main__": DEFAULT_BACKUP_DIR = os.path.join(os.path.expanduser('~'), 'github-backups') argparser = argparse.ArgumentParser('Github & Gists Backups') argparser.add_argument('-u', '--user', help='Github user', required=True) argparser.add_argument('-z', '--compress', help='Use gzip compression', default=False, action='store_true') argparser.add_argument('-d', '--directory', help='Backup directory', default=DEFAULT_BACKUP_DIR) backup = GithubBackup(argparser.parse_args()) backup.backup()