forked from avoinsystems/odoo-backup-restore-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
349 lines (277 loc) · 12 KB
/
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
from datetime import datetime
from xmlrpc import client
import configargparse
import os
import sys
import time
from boto3.session import Session
import logging
from logging.config import dictConfig
from io import BytesIO
logging_config = dict(
version=1,
formatters={
'f': {'format': '%(levelname)-8s %(message)s'}
},
handlers={
'h': {'class': 'logging.StreamHandler',
'formatter': 'f',
'level': logging.INFO}
},
root={
'handlers': ['h'],
'level': logging.INFO,
},
)
dictConfig(logging_config)
_logger = logging.getLogger()
actions = dict()
def main(args):
# Create the AWS S3 connection
aws_conn = Session(
args['aws_access_key_id'],
args['aws_secret_access_key'],
region_name=args['aws_region']
)
args['s3'] = aws_conn.resource('s3')
if args['protocol'] == 'xmlrpc':
args['conn'] = client.ServerProxy(
'http://{odoo_host}:{odoo_port}/xmlrpc/db'.format(**args)
)
action_name = args['mode'] + '_' + args['protocol']
return actions[action_name](**args)
def backup_xmlrpc(s3, conn, databases, odoo_host, odoo_port, odoo_master_password, odoo_version,
aws_access_key_id, aws_secret_access_key, aws_region, s3_bucket,
s3_path, **kwargs):
# Get the database list
db_list = conn.list()
dbs_not_found = set(databases) - set(db_list)
if dbs_not_found:
raise Exception(
"Unable to perform backup. "
"Database(s) {} can't be found.".format(dbs_not_found))
# Iterate through the databases to backup
for database in databases:
filename = "{}_{}.zip".format(database,
time.strftime('%Y-%m-%d_%H-%M-%S'))
# Download the backup dump from Odoo
if odoo_version == '8':
data = conn.dump(odoo_master_password, database)
else: # >= 9
data = conn.dump(odoo_master_password, database, 'zip')
data = base64.b64decode(data)
_logger.info(u"Successfully dumped database '{}'. Uploading to S3 ..."
.format(database))
# Upload the dump to S3
upload_path = s3_path + '/' + filename
s3.Bucket(s3_bucket).put_object(Key=upload_path, Body=data)
_logger.info(u"Upload to S3 finished. Database '{}' dump saved as {}"
.format(database, filename))
actions['backup_xmlrpc'] = backup_xmlrpc
def backup_http(s3, databases, odoo_host, odoo_port, odoo_master_password, odoo_version,
aws_access_key_id, aws_secret_access_key, aws_region, s3_bucket,
s3_path, **kwargs):
import requests
# Iterate through the databases to backup
backup_url = "http://{}:{}/web/database/backup".format(odoo_host, odoo_port)
if odoo_version == '8':
pwd_field = 'backup_pwd'
db_field = 'backup_db'
else:
pwd_field = 'master_pwd'
db_field = 'name'
request_data = {
pwd_field: odoo_master_password,
'backup_format': 'zip'
}
if odoo_version == '8':
request_data['token'] = 'dummy'
request_args = dict(
url=backup_url,
stream=True,
data=request_data
)
for database in databases:
filename = "{}_{}.zip".format(database,
time.strftime('%Y-%m-%d_%H-%M-%S'))
request_data[db_field] = database
# Download the backup dump from Odoo
response = requests.post(**request_args)
if response.status_code >= 400:
raise Exception("Odoo returned error {} when trying to backup database {}.".format(
response.status_code,
database
))
_logger.info(u"Successfully dumped database '{}'. Uploading to S3 ..."
.format(database))
# Upload the dump to S3
upload_path = s3_path + '/' + filename
s3.Bucket(s3_bucket).upload_fileobj(response.raw, Key=upload_path)
_logger.info(u"Upload to S3 finished. Database '{}' dump saved as {}"
.format(database, filename))
actions['backup_http'] = backup_http
def restore_xmlrpc(conn, s3, databases, odoo_host, odoo_port, odoo_master_password,
aws_access_key_id, aws_secret_access_key, aws_region, s3_bucket,
s3_path, restore_filename, **kwargs):
if databases:
assert len(databases) == 1, 'You can only restore one database ' \
'at once'
database = databases[0]
# Add path to restore filename
restore_key = s3_path + '/' + restore_filename \
if restore_filename else False
# Get the database list
db_list = conn.list()
bucket = s3.Bucket(s3_bucket)
restore_key = check_and_fix_restore_key(bucket, database, restore_key, s3_bucket, s3_path)
if not database:
database = guess_database_from_restore_key(restore_key)
if database in db_list:
raise Exception(
"Unable to perform restore. "
"Database '{}' already exists.".format(database))
# Download the backup from S3
_logger.info('Downloading {} from S3 ...'.format(restore_key))
file = BytesIO()
bucket.download_fileobj(restore_key, file)
data = base64.encodebytes(file.getvalue()).decode('utf-8')
_logger.info('Successfully downloaded {} from S3. Restoring dump '
'to database {}'.format(restore_key,
database))
# Restore the backup
conn.restore(odoo_master_password, database, data)
_logger.info(u"Successfully restored {} to database '{}'."
.format(restore_key, database))
actions['restore_xmlrpc'] = restore_xmlrpc
def restore_http(s3, databases, odoo_host, odoo_port, odoo_master_password,
aws_access_key_id, aws_secret_access_key, aws_region, s3_bucket,
s3_path, restore_filename, odoo_version, **kwargs):
import requests
import tempfile
if databases:
assert len(databases) == 1, 'You can only restore one database ' \
'at once'
database = databases[0]
# Add path to restore filename
restore_key = s3_path + '/' + restore_filename \
if restore_filename else False
base_url = 'http://{}:{}/web/database/'.format(odoo_host, odoo_port)
# Get the database list
if odoo_version in ('8', '9'):
# Only 10+ supports getting dblist via HTTP
conn = client.ServerProxy(
'http://{odoo_host}:{odoo_port}/xmlrpc/db'.format(odoo_host=odoo_host, odoo_port=odoo_port)
)
db_list = conn.list()
else:
response = requests.post(
base_url + 'list',
headers={'content-type': 'application/json'},
data='{}'
)
db_list = response.json()['result']
# Get a list of the backup files in the path
bucket = s3.Bucket(s3_bucket)
restore_key = check_and_fix_restore_key(bucket, database, restore_key, s3_bucket, s3_path)
if not database:
database = guess_database_from_restore_key(restore_key)
if database in db_list:
raise Exception(
"Unable to perform restore. "
"Database '{}' already exists.".format(database))
_logger.info('Downloading {} from S3 ...'.format(restore_key))
with tempfile.TemporaryFile() as file:
bucket.download_fileobj(restore_key, file)
file.seek(0)
_logger.info('Successfully downloaded {} from S3. Restoring dump '
'to database {}'.format(restore_key,
database))
# Post the file to Odoo
if odoo_version == '8':
pwd_field = 'restore_pwd'
db_field = 'new_db'
file_field = 'db_file'
else:
pwd_field = 'master_pwd'
db_field = 'name'
file_field = 'backup_file'
data = {
pwd_field: odoo_master_password,
db_field: database
}
if odoo_version == '8':
data['mode'] = 'restore'
response = requests.post(
base_url + 'restore',
files={file_field: ('s3_db.zip', file, 'application/zip')},
data=data,
)
# Ugly af, I know.
if response.status_code >= 400 or 'Database restore error:' in response.text:
if 'Database restore error:' in response.text:
text = response.text.split("Database restore error:", 1)[1].split("\n", 1)[0]
else:
text = response.text
raise Exception("There was an error restoring the database to Odoo.\n{}".format(text))
_logger.info(u"Successfully restored {} to database '{}'."
.format(restore_key, database))
def check_and_fix_restore_key(bucket, database, restore_key, s3_bucket, s3_path):
backup_files = bucket.objects.filter(Prefix=s3_path)
if restore_key and all(restore_key != file.key for file in backup_files):
raise FileNotFoundError(
'Backup file {} not found in S3 bucket {}.'
.format(restore_key, s3_bucket))
elif not restore_key:
# If the filename is not specified, find the latest dump
latest = False
for iter_file in backup_files:
if not latest or iter_file.last_modified > latest:
latest = iter_file.last_modified
restore_key = iter_file.key
if database and database not in restore_key:
_logger.warning("Latest dump key is {} but it doesn't "
"contain the database name '{}'."
.format(restore_key, database))
return restore_key
def guess_database_from_restore_key(restore_key):
# path/database_2017-08-07_13-05-43.zip
# If key is a path, the filename is the last level
path_parts = restore_key.split('/')
restore_key = path_parts[-1]
trim = 0
if restore_key.endswith('.zip') or restore_key.endswith('.sql'):
trim = 4
elif restore_key.endswith('.dump'):
trim = 5
return restore_key[0:len(restore_key) - trim]
actions['restore_http'] = restore_http
if __name__ == "__main__":
env = os.environ
supported_versions = ('8', '9', '10', '11')
parser = configargparse.ArgParser()
parser.add_argument('mode', default='backup', choices=('backup', 'restore'))
parser.add_argument('-c', '--config', is_config_file=True, help='Path to configuration file. Either .ini or .yaml syntax accepted')
parser.add_argument('--databases', env_var='DATABASES', required=True, type=lambda s: s.split(','))
parser.add_argument('--odoo-host', env_var='ODOO_HOST', default='odoo')
parser.add_argument('--odoo-port', env_var='ODOO_PORT', default=8069)
parser.add_argument('--odoo-master-password', env_var='ODOO_MASTER_PASSWORD', default='admin')
parser.add_argument('--odoo-version', env_var='ODOO_VERSION', default=supported_versions[-1], choices=supported_versions)
parser.add_argument('--aws-access-key-id', env_var='AWS_ACCESS_KEY_ID', required=True)
parser.add_argument('--aws-secret-access-key', env_var='AWS_SECRET_ACCESS_KEY', required=True)
parser.add_argument('--aws-region', env_var='AWS_REGION', required=True)
parser.add_argument('--s3-bucket', env_var='S3_BUCKET', required=True)
parser.add_argument('--s3-path', env_var='S3_PATH', default='backup')
parser.add_argument('--check-url', env_var='CHECK_URL', help="After every backup, send an HTTP GET request to this address. Designed with healthchecks.io in mind.")
parser.add_argument('--restore-filename', env_var='RESTORE_FILENAME')
parser.add_argument('--protocol', env_var='PROTOCOL', default='xmlrpc', choices=('xmlprc', 'http'))
args = parser.parse_args()
if not args.databases:
if args.mode == 'backup':
raise ValueError("--databases is required when in backup mode")
main(vars(args))
if args.check_url:
import requests
requests.get(args.check_url)