Skip to content

Instantly share code, notes, and snippets.

@Dgadavin
Created April 26, 2018 10:55
Show Gist options
  • Save Dgadavin/eaa5db669ebfbc2fce735407881c789f to your computer and use it in GitHub Desktop.
Save Dgadavin/eaa5db669ebfbc2fce735407881c789f to your computer and use it in GitHub Desktop.
import boto3
from git import Repo
import os
import sys
from docopt import docopt
import shutil
import tarfile
GIT_API_KEY = os.getenv("GITHUB_API")
BUCKET = 'glomex-github-repo-backup'
DELIMITER = "=" * 50
def main():
"""
Usage:
github_backup.py single-repo --repo-name=<repo>
github_backup.py multiple-repos --repo-file-list=<repo_file>
github_backup.py (-h | --help)
github_backup.py --version
Commands:
single-repo Clone the repo and upload it into S3
multiple-repos Clone each repo from a file and upload it into S3
Options:
-h --help Show this screen.
--repo-name=<repo> Name of repo that you want to backup
--repo-file-list=<repo_file> File with list of repos that you want to backup
"""
args = docopt(main.__doc__, version="1.0")
if args['single-repo']:
repo_name = args['--repo-name']
check_if_repo_exist(repo_name)
clone_and_write_s3(repo_name)
if args['multiple-repos']:
repo_file_list = args['--repo-file-list']
with open(repo_file_list, 'r') as file:
repos_list = file.read().splitlines()
for repo in repos_list:
check_if_repo_exist(repo)
clone_and_write_s3(repo)
def clone_and_write_s3(repo_name):
session = boto3.Session()
s3_client = session.resource('s3')
repo_name_arch = repo_name + '.tar.gz'
remote_url = "https://{}:[email protected]/glomex/{}".format(GIT_API_KEY, repo_name)
print(DELIMITER + repo_name + DELIMITER)
print("Cloning {} repo into {}".format(repo_name, os.getcwd()))
Repo.clone_from(remote_url, repo_name)
with tarfile.open(repo_name_arch, 'w:gz') as tar:
tar.add(os.path.basename(repo_name), arcname=repo_name)
try:
print("Uploading {} file into {} bucket".format(repo_name + '.tar.gz', BUCKET))
s3_client.Bucket(BUCKET).upload_file(repo_name_arch, repo_name_arch)
clear_data(repo_name)
except Exception as q:
print('Cannot upload files %s' % q)
print("Clearing uploaded data")
clear_data(repo_name)
sys.exit(1)
print("Please go to the https://github.com/glomex/{}/settings and delete the repo".format(repo_name))
def clear_data(repo_name):
print("Deleting cloned repo: {}".format(repo_name))
shutil.rmtree(repo_name, ignore_errors=True)
print("Deleted *.tar.gz file: {}".format(repo_name + '.tar.gz'))
os.remove(repo_name + '.tar.gz')
def check_if_repo_exist(repo_name):
if os.path.exists(repo_name):
shutil.rmtree(repo_name)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment