forked from wefixit-AT/oVirtBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
482 lines (417 loc) · 15.8 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python3
import logging
from argparse import ArgumentParser, FileType
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
import sys
import time
import datetime
from vmtools import VMTools
from config import Config
VMS_MAX_LIST = 400
"""
Main class to make the backups
"""
logger = logging.getLogger()
def initialize_logger(logger_fmt, logger_file_path, debug):
logger_options = {
"format": logger_fmt,
"level": logging.DEBUG if debug else logging.INFO,
}
if logger_file_path:
logger_options['filename'] = logger_file_path
logging.basicConfig(**logger_options)
def create_argparser():
p = ArgumentParser()
# General options
p.add_argument(
"-c", "--config-file",
help="Path to the config file, pass dash (-) for stdin",
dest="config_file",
required=True,
type=FileType(),
)
p.add_argument(
"-d", "--debug",
help="Debug flag",
dest="debug",
action="store_true",
default=False,
)
p.add_argument(
"--dry-run",
help="When set no operation takes effect",
dest="dry_run",
action="store_true",
default=None, # None because we need to recognize whether it was set.
)
osg = p.add_argument_group("oVirt server related options")
osg.add_argument(
"--server",
help="URL to connect to your engine",
dest="server",
default=None,
)
osg.add_argument(
"--username",
help="Username to connect to the engine",
dest="username",
default=None,
)
osg.add_argument(
"--password",
help="Password to connect to the engine",
dest="password",
default=None,
)
vmg = p.add_argument_group("VM's related arguments")
vmg.add_argument(
"-a", "--all-vms",
help="Backup all VMs and override the list of VM's in the config "
"file",
dest="all_vms",
action="store_true",
default=None,
)
vmg.add_argument(
"--tag",
help="define the tag used to override the list of VM's that should"
" be backed up",
dest="vm_tag",
default=None,
)
vmg.add_argument(
"--vm-names-skip",
help="List of names which VMs should not be backed up",
dest="vm_names_skip",
default=None,
)
vmg.add_argument(
"--vm-names",
help="List of names which VMs should be backed up",
dest="vm_names",
default=None,
)
vmg.add_argument(
"--vm-middle",
help="Middle part for the exported VM name",
dest="vm_middle",
default=None,
)
dcg = p.add_argument_group("Data Centre's related options")
dcg.add_argument(
"--export-domain",
help="Name of the NFS Export Domain",
dest="export_domain",
default=None,
)
dcg.add_argument(
"--datacenter-name",
help="Datacenter where export domain is attached",
dest="datacenter_name",
default=None,
)
dcg.add_argument(
"--storage-domain",
help="Storage domain where VMs are located",
dest="storage_domain",
default=None,
)
dcg.add_argument(
"--cluster-name",
help="Name of the cluster where VMs should be cloned",
dest="cluster_name",
default=None,
)
mscg = p.add_argument_group("Miscellaneous options")
mscg.add_argument(
"--snapshot-description",
help="Description which should be set to created snapshot",
dest="snapshot_description",
default=None,
)
mscg.add_argument(
"--timeout",
help="Timeout in seconds to wait for time consuming operation",
dest="timeout",
default=None,
)
mscg.add_argument(
"--backup-keep-count",
help="Number of days to keep backups",
dest="backup_keep_count",
default=None,
)
mscg.add_argument(
"--vm-name-max-length",
help="Limit for length of VM's name ",
dest="vm_name_max_length",
default=None,
)
mscg.add_argument(
"--use-short-suffix",
help="If set it will use short suffix for VM's name",
dest="use_short_suffix",
action="store_true",
default=None,
)
mscg.add_argument(
"--storage-space-threshold",
help="The number in interval (0, 1), to free space on storage domain.",
dest="storage_space_threshold",
type=float,
default=None,
)
mscg.add_argument(
"--persist-memorystate",
help="If set, the VM is being paused during snapshot creation.",
dest="persist_memorystate",
action="store_true",
default=None,
)
lg = p.add_argument_group("Logging related options")
lg.add_argument(
"--logger-fmt",
help="This value is used to format log messages",
dest="logger_fmt",
default=None,
)
lg.add_argument(
"--logger-file-path",
help="Path to file where we to store log messages",
dest="logger_file_path",
default=None,
)
return p
def arguments_to_dict(opts):
result = {}
ignored_keys = ('config_file', 'dry_run', 'debug')
for key, val in vars(opts).items():
if key in ignored_keys:
continue # This doesn't have a place in config file
if val is not None:
result[key] = val
return result
def close_and_exit(err_msg):
logger.error(err_msg)
api.close()
sys.exit(1)
def main(argv):
p = create_argparser()
opts = p.parse_args(argv)
config_arguments = arguments_to_dict(opts)
global config
with opts.config_file:
config = Config(opts.config_file, opts.debug, config_arguments)
initialize_logger(
config.get_logger_fmt(), config.get_logger_file_path(), opts.debug,
)
time_start = int(time.time())
has_errors = False
# Connect to server
connect()
system_service = api.system_service()
# Test if data center is valid
# Retrieve the data center service:
if system_service.data_centers_service().list(search='name=%s' % config.get_datacenter_name())[0] is None:
close_and_exit(err_msg="!!! Check the datacenter_name in the config")
# Test if config export_domain is valid
if system_service.storage_domains_service().list(search='name=%s' % config.get_export_domain())[0] is None:
close_and_exit(err_msg=f"!!! Check the export_domain in the config {config.get_export_domain()}")
# Test if config cluster_name is valid
if system_service.clusters_service().list(search='name=%s' % config.get_cluster_name())[0] is None:
close_and_exit(err_msg="!!! Check the cluster_name in the config")
# Test if config storage_domain is valid
if system_service.storage_domains_service().list(search='name=%s' % config.get_storage_domain())[0] is None:
close_and_exit(err_msg="!!! Check the storage_domain in the config")
vms_service = system_service.vms_service()
# Add all VM's to the config file
if config.get_all_vms():
vms = vms_service.list(max=VMS_MAX_LIST)
config.set_vm_names([vm.name for vm in vms])
# Update config file
if opts.config_file.name != "<stdin>":
config.write_update(opts.config_file.name)
# Add VM's with the tag to the vm list
if config.get_vm_tag():
vms = vms_service.list(max=VMS_MAX_LIST, search=f"tag={config.get_vm_tag()}")
config.set_vm_names([vm.name for vm in vms])
# Update config file
if opts.config_file.name != "<stdin>":
config.write_update(opts.config_file.name)
if config.get_vm_names_skip():
vms = vms_service.list(max=VMS_MAX_LIST)
vm_names_skip = config.get_vm_names_skip()
config.set_vm_names([vm.name for vm in vms if vm.name not in vm_names_skip])
# Test if all VM names are valid
for vm_from_list in config.get_vm_names():
if vms_service.list(search=f"name={vm_from_list}") is None:
close_and_exit(err_msg=f"!!! There are no VM with the following name in your cluster: {vm_from_list}")
# Test if config vm_middle is valid
if not config.get_vm_middle():
close_and_exit(err_msg="!!! It's not valid to leave vm_middle empty")
vms_with_failures = list()
dcs_service = system_service.data_centers_service()
dc = dcs_service.list(search='name=%s' % config.get_datacenter_name())[0]
dc_service = dcs_service.data_center_service(dc.id)
sds_service = dc_service.storage_domains_service()
sds_export_service = sds_service.list(search='name=%s' % config.get_export_domain())[0]
if sds_export_service is None:
close_and_exit(err_msg="")
for vm_from_list in config.get_vm_names():
vms_with_failures.append(vm_from_list)
config.clear_vm_suffix()
vm_clone_name = vm_from_list + config.get_vm_middle() + config.get_vm_suffix()
# Check VM name length limitation
length = len(vm_clone_name)
if length > config.get_vm_name_max_length():
logger.error("!!! VM name with middle and suffix are to long (size: %s, allowed %s) !!!", length,
config.get_vm_name_max_length())
logger.info("VM name: %s", vm_clone_name)
continue
logger.info("Start backup for: %s", vm_from_list)
vm_time_start = int(time.time())
if config.get_dry_run():
vms_with_failures.remove(vm_from_list)
continue
try:
VMTools.check_storage_domain_status(
api,
config.get_datacenter_name(),
config.get_export_domain()
)
# Cleanup: Delete the cloned VM
VMTools.delete_vm(api, config, vm_from_list)
# Get the VM
vm = vms_service.list(search='name=%s' % vm_from_list)
if len(vm) == 0:
logger.warning(
"The VM (%s) doesn't exist anymore, skipping backup ...",
vm_from_list
)
continue
vm = vm[0]
# Delete old backup snapshots
VMTools.delete_snapshots(api, vm, config, vm_from_list)
# Check free space on the storage
VMTools.check_free_space(api, config, vm)
# Create a VM snapshot:
try:
logger.info("Snapshot creation started ...")
snapshots_service = vms_service.vm_service(vm.id).snapshots_service()
if not config.get_dry_run():
# Add the new snapshot:
snapshots_service.add(
types.Snapshot(
description=config.get_snapshot_description(),
persist_memorystate=config.get_persist_memorystate(),
),
)
VMTools.wait_for_snapshot_operation(api, vm, config, "creation")
logger.info("Snapshot created")
except Exception as e:
logger.info("Can't create snapshot for VM: %s", vm_from_list)
logger.info("DEBUG: %s", e)
has_errors = True
continue
# Workaround for some SDK problems see issue #17
time.sleep(config.get_timeout())
# Clone the snapshot into a VM
snapshots = snapshots_service.list()
snap = None
for i in snapshots:
if i.description == config.get_snapshot_description():
snap = i
if not snap:
logger.error("!!! No snapshot found !!!")
has_errors = True
continue
logger.info("Clone into VM (%s) started ..." % vm_clone_name)
if not config.get_dry_run():
cloned_vm = vms_service.add(
vm=types.Vm(
name=vm_clone_name,
memory=vm.memory,
snapshots=[
types.Snapshot(
id=snap.id
)
],
cluster=types.Cluster(
name=config.get_cluster_name()
)
)
)
# Find the service that manages the cloned virtual machine:
cloned_vm_service = vms_service.vm_service(cloned_vm.id)
# Wait till the virtual machine is down, as that means that the creation
# of the disks of the virtual machine has been completed:
while True:
time.sleep(config.get_timeout())
logger.debug("Cloning into VM (%s) in progress ..." % vm_clone_name)
cloned_vm = cloned_vm_service.get()
if cloned_vm.status == types.VmStatus.DOWN:
break
logger.info("Cloning finished")
# Delete backup snapshots
VMTools.delete_snapshots(api, vm, config, vm_from_list)
# Delete old backups
if config.get_backup_keep_count():
VMTools.delete_old_backups(api, config, vm_from_list)
if config.get_backup_keep_count_by_number():
VMTools.delete_old_backups_by_number(api, config, vm_from_list)
# Export the VM
try:
vm_clone = api.system_service().vms_service().list(search='name=%s' % vm_clone_name)[0]
logger.info("Export of VM (%s) started ..." % vm_clone_name)
if not config.get_dry_run():
cloned_vm_service = vms_service.vm_service(vm_clone.id)
cloned_vm_service.export(
exclusive=True,
discard_snapshots=True,
storage_domain=types.StorageDomain(
name=config.get_export_domain()
)
)
while True:
time.sleep(config.get_timeout())
cloned_vm = cloned_vm_service.get()
if cloned_vm.status == types.VmStatus.DOWN:
break
logger.info("Exporting finished")
except Exception as e:
logger.info("Can't export cloned VM (%s) to domain: %s", vm_clone_name, config.get_export_domain())
logger.info("DEBUG: %s", e)
has_errors = True
continue
# Delete the CLONED VM
VMTools.delete_vm(api, config, vm_from_list)
vm_backup_time = str(datetime.timedelta( seconds = int(time.time()) - vm_time_start ))
logger.info("VM \"%s\" backup duration: %s ", vm_from_list, vm_backup_time )
logger.info("VM exported as %s", vm_clone_name)
logger.info("Backup done for: %s", vm_from_list)
vms_with_failures.remove(vm_from_list)
except Exception as e:
close_and_exit(err_msg=f"!!! Got unexpected exception: {e}")
backup_time = str(datetime.timedelta( seconds = int(time.time()) - time_start ))
logger.info("Overall duration: %s ", backup_time)
logger.info("All backups done")
if vms_with_failures:
logger.info("Backup failure for:")
for vm_with_failures in vms_with_failures:
logger.info(f" {vm_with_failures}")
if has_errors:
close_and_exit(err_msg="Some errors occurred during the backup, please check the log file")
# Disconnect from the server
api.close()
def connect():
global api
api = sdk.Connection(
url=config.get_server(),
username=config.get_username(),
password=config.get_password(),
insecure=True,
debug=False
)
if __name__ == "__main__":
main(sys.argv[1:])