From 851270c7b527ddd9f01668bf510cf392c1d2aaab Mon Sep 17 00:00:00 2001 From: wiro Date: Thu, 20 Dec 2018 13:26:20 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + config.yaml-example | 12 +++++++ gitlab-project-export.py | 77 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 config.yaml-example create mode 100755 gitlab-project-export.py diff --git a/.gitignore b/.gitignore index 894a44c..0786bfc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +config.yaml # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/config.yaml-example b/config.yaml-example new file mode 100644 index 0000000..ac79779 --- /dev/null +++ b/config.yaml-example @@ -0,0 +1,12 @@ +gitlab: + access: + gitlab_url: "https://gitlab.com" + token: "MY_PERSONAL_SECRET_TOKEN" + projects: + - rvojcik/example-project + +backup: + project_dirs: True + destination: "/data/backup" + backup_name: "gitlab-com-{PROJECT_NAME}-{TIME}.tar.gz" + backup_time_format: "%Y%m%d" diff --git a/gitlab-project-export.py b/gitlab-project-export.py new file mode 100755 index 0000000..102437a --- /dev/null +++ b/gitlab-project-export.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +from __future__ import print_function +import sys +import os +import argparse +import yaml +from lib import config, gitlab +from datetime import date +import requests + +return_code = 0 + +if __name__ == '__main__': + # Parsing arguments + parser = argparse.ArgumentParser( + description= 'GitLab project Export', + epilog='Created by Robert Vojcik ') + + # Arguments + parser.add_argument('-c', dest='config', default='config.yaml', + help='sum the integers (default: find the max)') + + args = parser.parse_args() + + if not os.path.isfile(args.config): + print("Unable to find config file %s" % (args.config)) + + c = config.Config(args.config) + token = c.config["gitlab"]["access"]["token"] + gitlab_url = c.config["gitlab"]["access"]["gitlab_url"] + + # Init gitlab api object + gitlab = gitlab.Api(gitlab_url, token) + + # Export each project + for project in c.config["gitlab"]["projects"]: + status = gitlab.project_export(project) + + # Export successful + if status: + # Download project to our destination + if c.config["backup"]["project_dirs"]: + destination = c.config["backup"]["destination"] + "/" + project + else: + destination = c.config["backup"]["destination"] + + # Prepare actual date + d = date.today() + # File template from config + file_tmpl = c.config["backup"]["backup_name"] + # Projectname in dest_file + dest_file = destination + "/" + file_tmpl.replace("{PROJECT_NAME}", project.replace("/", "-")) + # Date in dest_file + dest_file = dest_file.replace("{TIME}",d.strftime(c.config["backup"]["backup_time_format"])) + + # Create directories + if not os.path.isdir(destination): + os.makedirs(destination) + + # Get URL from gitlab object + url = gitlab.download_url["api_url"] + + # Download file + r = requests.get(url, allow_redirects=True, stream=True, headers={"PRIVATE-TOKEN": token}) + + with open(dest_file, 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) + + else: + # Export for project unsuccessful + print("Export failed for project %s" % (project), file=sys.stderr) + return_code += 1 + + sys.exit(return_code)