Skip to content

Instantly share code, notes, and snippets.

@brianv0
Created February 3, 2016 21:04
Show Gist options
  • Save brianv0/a1a4b407a6261039117a to your computer and use it in GitHub Desktop.
Save brianv0/a1a4b407a6261039117a to your computer and use it in GitHub Desktop.
Organization Snapshots
import datetime
import sqlite3
from subprocess import Popen, PIPE
import Github
import sys
g = Github(sys.stdin[1])
def get_repo_list(organization):
gh_org = g.get_organization(organization)
repo_list = gh_org.get_repos()
return repo_list
def initialize_db(curs):
curs.execute("CREATE TABLE Repos(name text, url text, "
"initial_backup timestamp, last_backup timestamp)")
curs.execute("CREATE TABLE Backups(backup_date timestamp, "
"backup_stage text, rc integer, stdout blob, stderr blob)")
def report_error(now, curs, stage, rc, stdout, stderr):
curs.execute("INSERT INTO Backups(backup_date, backup_stage, rc, stdout, stderr) VALUES (%s, %s, %d, %s, %s)",
now, stage, rc, stdout, stderr)
def report_success(now, curs, rc):
curs.execute("INSERT INTO Backups(backup_date, backup_stage, rc) VALUES (%s, %s, %d)", now, "ok", rc)
def backup_repo(repo):
stage = "clone"
now = datetime.datetime.utcnow()
now_date = now.isoformat()[:10]
sp = Popen(["git", "clone", "--mirror", repo.git_url, repo.name], stderr=PIPE)
out = sp.stdout.read()
err = sp.stderr.read()
rc = sp.wait()
if rc != 0:
report_error(now, curs, rc, stdout, stderr)
stage = "bundle"
bundle_name = "%s-%s.bundle" %(repo.name, dt)
sp2 = Popen(["git", "--git-dir", repo.name, "bundle", "create", bundle_name, "--all"])
out = sp2.stdout.read()
err = sp2.stderr.read()
rc = sp2.wait()
if rc != 0:
report_error(now, curs, rc, stdout, stderr)
Stage = "verify"
sp3 = Popen(["git", "bundle", "verify", bundle_name])
out = sp3.stdout.read()
err = sp3.stderr.read()
rc = sp3.wait()
if rc != 0:
report_error(now, curs, rc, stdout, stderr)
conn = sqlite3.connect("gh-backup.db")
curs = conn.cursor()
repo_list = get_repo_list(sys.stdin[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment