forked from rvojcik/gitlab-project-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-project-export.py
executable file
·162 lines (135 loc) · 5.06 KB
/
gitlab-project-export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import argparse
import yaml
from datetime import date
import requests
import re
# Find our libs
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from lib import config, gitlab
return_code = 0
if __name__ == '__main__':
# Parsing arguments
parser = argparse.ArgumentParser(
description="""
GitLab Project Export is
small project using Gitlab API for exporting whole gitlab
project with wikis, issues etc.
Good for migration or simple backup your gitlab projects.
""",
epilog='Created by Robert Vojcik <[email protected]>')
# Arguments
parser.add_argument(
'-c', dest='config', default='config.yaml',
help='config file'
)
parser.add_argument(
'-d', dest='debug', default=False, action='store_const',
const=True, help='Debug mode'
)
parser.add_argument(
'-r', dest='regularity', default="day",
help='Specify the regularity of this backup. See config.yaml-example'
)
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
if args.debug:
print("%s, token" % (gitlab_url))
gitlab = gitlab.Api(gitlab_url, token)
# Export each project
export_projects = []
# Get All member projects from gitlab
projects = gitlab.project_list()
if not projects:
print("Unable to get projects for your account", file=sys.stderr)
sys.exit(1)
if args.debug:
print("regularity is %s" % args.regularity)
# Check projects against config
# Create export_projects array
for project_pattern in c.config["gitlab"]["projects"]:
for gitlabProject in projects:
if re.match(project_pattern["name"], gitlabProject):
for regularity_pattern in project_pattern["regularity"]:
if re.match(regularity_pattern, args.regularity):
export_projects.append(gitlabProject)
if args.debug:
print("Projects to export: " + str(export_projects))
for project in export_projects:
if args.debug:
print("Exporting %s" % (project))
# Download project to our destination
destination = c.config["backup"]["destination"]
if c.config["backup"]["project_dirs"]:
destination = destination + "/" + project
if c.config["backup"]["regularity_dirs"]:
destination = destination + "/" + args.regularity
# Create directories
if not os.path.isdir(destination):
try:
os.makedirs(destination)
except:
print("Unable to create directories %s" % (destination), file=sys.stderr)
sys.exit(1)
if args.debug:
print(" Destination %s" % (destination))
# Prepare actual date
d = date.today()
# File template from config
file_tmpl = c.config["backup"]["backup_name"]
file_tmpl_reg = file_tmpl.replace("{REGULARITY}", args.regularity)
# Projectname in dest_file
dest_file = destination + "/" + file_tmpl_reg.replace(
"{PROJECT_NAME}",
project.replace("/", "-")
)
# Date in dest_file
dest_file = dest_file.replace(
"{TIME}", d.strftime(c.config["backup"]["backup_time_format"]))
if args.debug:
print(" Destination file %s" % (dest_file))
if os.path.isfile(dest_file):
print("File %s already exists" % (dest_file), file=sys.stderr)
return_code += 1
continue
status = gitlab.project_export(project)
# Export successful
if status:
if args.debug:
print("Success for %s" % (project))
# Get URL from gitlab object
url = gitlab.download_url["api_url"]
if args.debug:
print(" URL: %s" % (url))
# Download file
r = requests.get(
url,
allow_redirects=True,
stream=True,
headers={"PRIVATE-TOKEN": token})
if r.status_code >= 200 and r.status_code < 300:
with open(dest_file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
else:
print(
"Unable to download project %s. Got code %d: %s" % (
project,
r.status_code,
r.text),
file=sys.stderr)
return_code += 1
else:
# Export for project unsuccessful
print("Export failed for project %s" % (project), file=sys.stderr)
return_code += 1
sys.exit(return_code)