forked from MyMedsAndMe/jira-confluence-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira_confluence_backup.py
283 lines (249 loc) · 9.69 KB
/
jira_confluence_backup.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import requests
import json
import time
import datetime
from sys import stdout, exit
import argparse
import getpass
import os
import logging
from logging.handlers import SysLogHandler
# Argparse action extension to alternatively accept passwords directly
# or via a prompt in the console
class Password(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
if values is None:
values = getpass.getpass()
setattr(namespace, self.dest, values)
def set_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--instance',
'-i',
help='Atlassian account instance (format: \
account.attlassian.net)', required=True)
parser.add_argument('--application', '-a',
help='Atlassian application to backup \
(Jira or Confluence)', required=True)
parser.add_argument('--username', '-u',
help='Atlassian username used to \
login to the web application', required=True)
parser.add_argument('--timeout', '-t',
help='Timeout for the remote backup to complete,\
defaults to 180 minutes',
default=180)
parser.add_argument('--password', '-p',
action=Password,
nargs='?', dest='password',
help='Atlassian password used to login into the web \
application',
required=True)
parser.add_argument('--location',
'-l',
default='/tmp/',
help='Location of resulting backup file, defaults to\
\'/tmp/APPLICATION\'')
parser.add_argument('--tasks',
nargs='+',
help='Perform specified tasks (with a space between \
each task): t (trigger), m (monitor) and d (download).\
If d add argument for file name')
parser.add_argument('--log',
help='Enable logging',
dest='log',
action='store_true')
args = parser.parse_args()
# Ensure valid tasks where supplied
if args.tasks is not None:
targs = ['t', 'm', 'd']
if all((x not in args.tasks for x in targs)):
print "Error: please supply either 't' (trigger), 'm' (monitor) or\
d (download) with tasks"
exit(1)
return args
class LogMessage:
'''
Simple local syslog logger
'''
def __init__(self, application):
self._logger = logging.getLogger('BackupLogger')
self._logger.setLevel(logging.INFO)
self._handler = logging.handlers.SysLogHandler(address='/dev/log')
self._logger.addHandler(self._handler)
self._tag = str.upper('ATLASSIAN_BACKUP_' + application)
self._format = logging.Formatter('%(name)s %(tag)s: %(message)s')
self._handler.setFormatter(self._format)
self._logger.addHandler
def log_message(self, message):
global log
if log:
self._logger.info(message, extra={'tag': self._tag})
def set_urls():
global trigger_url
global progress_url
global download_url
if application.upper() == 'CONFLUENCE':
trigger_url = 'https://' + instance + '/wiki/rest/obm/1.0/runbackup'
progress_url = 'https://' + instance +\
'/wiki/rest/obm/1.0/getprogress.json'
download_url = 'https://' + instance + '/wiki/download/'
return
if application.upper() == 'JIRA':
trigger_url = 'https://' + instance + '/rest/obm/1.0/runbackup'
progress_url = 'https://' + instance + '/rest/obm/1.0/getprogress.json'
download_url = 'https://' + instance
return
if application.upper() != 'JIRA' or 'CONFLUENCE':
print "Invalid application specified. \
Request either \"Jira\" or \"Confluence\""
exit(1)
def create_session(username, password):
s = requests.session()
# create a session
r = s.post('https://' + instance + '/login',
{'username': username,
'password': password})
if int(r.status_code) == 200:
return s
else:
print "Session creation failed"
exit(1)
def trigger(s):
r = s.post(url=trigger_url,
data=json.dumps({'cbAttachments': 'true'}),
headers={'Content-Type': 'application/json',
'X-Atlassian-Token': 'no-check',
'X-Requested-With': 'XMLHttpRequest'})
print "Trigger response: %s" % r.status_code
if int(r.status_code) == 200:
print "Trigger response successful"
result = ['Trigger response successful', True]
return result
else:
print 'Trigger failed'
if int(r.status_code) == 500:
print 'Returned text data: %s' % str(r.text)
result = ['Trigger failed with message: %s' % str(r.text), False]
return result
def monitor(s):
r = s.get(url=progress_url)
try:
progress_data = json.loads(r.text)
except ValueError:
print """No JSON object could be decoded.
Get progress failed to return expected data.
Return code: %s """ % (r.status_code)
result = ['No JSON object could be decoded\
- get progress failed to return expected data\
Return code: %s """ % (r.status_code)', False]
# Timeout waiting for remote backup to complete
# (since it sometimes fails) in 5s multiples
global timeout
timeout_count = timeout*12 # timeout x 12 = number of iterations of 5s
time_left = timeout
while 'fileName' not in progress_data or timeout_count > 0:
# Clears the line before re-writing to avoid artifacts
stdout.write("\r\x1b[2k")
stdout.write("\r\x1b[2K%s. Timeout remaining: %sm"
% (progress_data['alternativePercentage'],
str(time_left)))
stdout.flush()
r = s.get(url=progress_url)
progress_data = json.loads(r.text)
time.sleep(5)
timeout_count = timeout_count - 5
if timeout_count % 12 == 0:
time_left = time_left - 1
if 'fileName' in progress_data:
result = [progress_data['fileName'], True]
return result
def get_filename(s):
print "Fetching file name"
r = s.get(url=progress_url)
try:
progress_data = json.loads(r.text)
except ValueError:
print """No JSON object could be decoded.
Get progress failed to return expected data.
Return code: %s """ % (r.status_code)
return False
if 'fileName' not in progress_data:
print 'File name to download not found in server response.'
return False
else:
return progress_data['fileName']
def create_backup_location(l):
if not os.path.exists(location):
try:
os.makedirs(location, 0744)
except OSError:
return False
return True
def download(s, l):
filename = get_filename(s)
if not filename:
return False
print "Filename found: %s" % filename
print "Checking if url is valid"
r = s.get(url=download_url + filename, stream=True)
print "Status code: %s" % str(r.status_code)
if int(r.status_code) == 200:
print "Url returned '200', downloading file"
if not create_backup_location(l):
result = ['Failed to create backup location', False]
return result
date_time = datetime.datetime.now().strftime("%Y%m%d")
with open(l + '/' + application + '-' + date_time + '.zip', 'wb') as f:
file_total = 0
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
file_total = file_total + 1024
file_total_m = float(file_total)/1048576
# Clears the line before re-writing to avoid artifacts
stdout.write("\r\x1b[2k")
stdout.write("\r\x1b[2K%.2fMB downloaded" % file_total_m)
stdout.flush()
stdout.write("\n")
result = ['Backup downloaded successfully', True]
return result
else:
print "Download file not found on remote server - response code %s" % \
str(r.status_code)
print "Download url: %s" % download_url + filename
result = ['Download file not found on remote server', False]
return result
if __name__ == "__main__":
args = set_arguments()
global trigger_url, progress_url, download_url,\
instance, application, log, timeout
application = args.application
instance = args.instance + ".atlassian.net"
username = args.username
password = args.password
location = args.location
timeout = args.timeout
if args.log:
log = True
set_urls()
log = LogMessage(application)
session = create_session(username, password)
if args.tasks is None or 't' in args.tasks:
print "Triggering backup"
result = trigger(session)
log.log_message(result[0])
if not result[1]:
exit(1)
if args.tasks is None or 'm' in args.tasks:
print "Monitoring remote backup progress"
result = monitor(session)
log.log_message(result[0])
if not result[1]:
exit(1)
if args.tasks is None or 'd' in args.tasks:
print "Downloading file"
result = download(session, location)
log.log_message(result[0])
if not result[1]:
exit(1)
log.log_message('All tasks completed successfully')
print "All tasks completed."