-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitLabBackup.py
executable file
·147 lines (135 loc) · 5.45 KB
/
GitLabBackup.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
import argparse
import sys
import os
import json
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime
import requests
# Start timestamp
print("Backup starts at : " + str(datetime.now()))
# Extract url from arguments
parser = argparse.ArgumentParser()
parser.add_argument("url", help="the GitLab server's url")
parser.add_argument("token", help="the private token of your GitLab profile")
parser.add_argument("--ssh_port", help="the ssh port of GitLab projects clone operation (default 22)")
parser.add_argument("--backup_dir", help="the directory where repositories will be backup (default ./repos_backup")
parser.add_argument("--config_email", help="the configuration file (.json) for email notification (default ./config_email.json)")
args = parser.parse_args()
# Make the backup repositories directory
backup_directory = "./repos_backup"
if args.backup_dir:
backup_directory = args.backup_dir
try:
os.mkdir(backup_directory)
except OSError as e:
if e.errno == 17:
print("Backup directory already exists. Skip creation...")
else:
raise
page = 1
while True:
# Request GitLab API to retrieve projects
gitlab_url = args.url + "/api/v3/projects?page=" + str(page) + "&private_token=" + args.token
r = requests.get(gitlab_url, verify=False)
if r.status_code != 200:
r.raise_for_status()
# Iterate on projects and clone them in mirror mode or update them if already exist
projects = r.json()
for project in projects:
url = project["ssh_url_to_repo"]
if args.ssh_port:
url = "ssh://" + url.replace(":", ":" + args.ssh_port + "/")
localPath = backup_directory + "/" + project["path"] + ".git"
if not os.path.exists(localPath):
print("Create backup for " + localPath)
os.system("git clone --mirror " + url + " " + localPath)
else:
print("Update backup for " + localPath)
os.system("cd " + localPath + "; git remote update")
try:
page = int(r.headers['X-Next-Page'])
except:
break
# Send mail if enabled
configEmailFilename = args.config_email
if not configEmailFilename:
configEmailFilename = "./config_email.json"
if not os.path.exists(configEmailFilename):
print("No configuration file found for email notification. Skip...")
else:
config = None
with open(configEmailFilename) as configEmail:
config = json.loads(configEmail.read())
# Mandatory fields
if config is None:
print("An error occured when reading email notifications configuration file. Skip email notification...")
sys.exit(-1)
if "from" not in config:
print("Field 'from' not found in email notifications configuration file. Skip email notification...")
sys.exit(-1)
if "to" not in config:
print("Field 'to' not found in email notifications configuration file. Skip email notification...")
sys.exit(-1)
if "smtp_url" not in config:
print("Field 'smtp_url' not found in email notifications configuration file. Skip email notification...")
sys.exit(-1)
if "smtp_login" not in config:
print("Field 'smtp_login' not found in email notifications configuration file. Skip email notification...")
sys.exit(-1)
if "smtp_password" not in config:
print("Field 'smtp_password' not found in email notifications configuration file. Skip email notification...")
sys.exit(-1)
# Optional fields
subject = "Backup GitLab"
if "subject" not in config:
print(
"Field 'subject' not found in email notifications configuration file. Default will be used ('" + subject + "')")
else:
subject = config["subject"]
message = "Your GitLab projects were backup sucessfully."
if "message" not in config:
print(
"Field 'message' not found in email notifications configuration file. Default will be used ('" + message + "')")
else:
subject = config["subject"]
enable_ssl = True
if "enable_ssl" not in config:
print("Field 'enable_ssl' not found in email notifications configuration file. Default will be used (" + str(
enable_ssl) + ")")
else:
if type(config["enable_ssl"]) != bool:
print("Field 'enable_ssl' isn't a boolean. Default will be used (" + str(enable_ssl) + ")")
else:
enable_ssl = config["enable_ssl"]
port = 465
if not enable_ssl:
port = 587
if "port" not in config:
print(
"Field 'port' not found in email notifications configuration file. Default will be used (" + str(
port) + ")")
else:
if type(config["port"]) != int:
print("Field 'port' isn't an integer. Default will be used (" + str(port) + ")")
else:
port = config["port"]
# Send mail
msg = MIMEMultipart()
msg["From"] = config["from"]
msg["To"] = config["to"]
msg["Subject"] = subject
msg.attach(MIMEText(message))
mailserver = None
if enable_ssl:
mailserver = smtplib.SMTP_SSL(config["smtp_url"], port)
else:
mailserver = smtplib.SMTP(config["smtp_url"], port)
mailserver.ehlo()
mailserver.login(config["smtp_login"], config["smtp_password"])
mailserver.sendmail(config["from"], config["to"], msg.as_string())
mailserver.quit()
print("Mail sent succesfully")
# End timestamp
print("Backup ends at : " + str(datetime.now()))