Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wiro committed Dec 20, 2018
1 parent 66f4dda commit 851270c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config.yaml
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
12 changes: 12 additions & 0 deletions config.yaml-example
Original file line number Diff line number Diff line change
@@ -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"
77 changes: 77 additions & 0 deletions gitlab-project-export.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>')

# 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)

0 comments on commit 851270c

Please sign in to comment.