Skip to content

Instantly share code, notes, and snippets.

@Dgadavin
Created January 11, 2018 09:13
Show Gist options
  • Save Dgadavin/485e38aeb90ff90555e7d49667e48914 to your computer and use it in GitHub Desktop.
Save Dgadavin/485e38aeb90ff90555e7d49667e48914 to your computer and use it in GitHub Desktop.
import boto3
from git import Repo
import json
import os
from docopt import docopt
import shutil
import tarfile
import requests
GIT_API_KEY = os.getenv("GITHUB_API")
def main():
"""
Usage:
github_backup_recovey.py backup --repo-name=<repo>
github_backup_recovey.py recovery --repo-name=<repo>
github_backup_recovey.py (-h | --help)
github_backup_recovey.py --version
Commands:
backup Clone the repo and upload it into S3 and delete from GitHub
recovery Get repo from S3, create repo on GitHUb and upload all files
Options:
-h --help Show this screen.
--repo-name=<repo> Name of repo that you want to backup/recovery
"""
args = docopt(main.__doc__, version="1.0")
repo_name = args['--repo-name']
repo_name_arch = repo_name + '.tar.gz'
bucket = 'glomex-github-repo-backup'
if os.path.exists(repo_name):
shutil.rmtree(repo_name)
if args['backup']:
remote_url = "https://{}:[email protected]/glomex/{}".format(GIT_API_KEY, repo_name)
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)
session = boto3.Session()
s3_client = session.resource('s3')
# s3_client.create_bucket(Bucket=bucket, CreateBucketConfiguration={'LocationConstraint':'eu-west-1'}, ACL='private')
s3_client.Bucket(bucket).upload_file(repo_name_arch, repo_name_arch)
if args['recovery']:
recovery_repo(bucket, repo_name_arch, repo_name)
def recovery_repo(bucket, repo_name_arch, repo_name):
session = boto3.Session()
s3_client = session.resource('s3')
s3_client.Bucket(bucket).download_file(repo_name_arch, repo_name_arch)
with tarfile.open(repo_name_arch, 'r:gz') as tar:
tar.extractall()
payload = {
"name": repo_name,
"description": "This is your first repository",
"homepage": "https://github.com",
"private": "false",
"has_issues": "true",
"has_projects": "true",
"has_wiki": "true",
"team_id": ""
}
headers = {'Authorization': 'token ' + GIT_API_KEY}
r = requests.post("https://api.github.com/orgs/glomex/repos", data=json.dumps(payload), headers=headers)
print r.content
if __name__ == "__main__":
main()
boto3
requests
docopt
GitPython
@Dgadavin
Copy link
Author

how to use:

export GITHUB_API=<YOUR_API_KEY>
python <skript_name>.py backup --repo-name=<REPO_NAME>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment