Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Github repositories & Gists backup script
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(, self.folder, 'gists')
self.repos_backup_directory = os.path.join(, self.folder, 'repos')
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(""):
key = "gists"
key = "repos"
return status
def get_gists(self):
url = "" % 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 = "" % 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
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
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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment