This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
/
BareosFdPluginVMware.py
1244 lines (1100 loc) · 46.8 KB
/
BareosFdPluginVMware.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2014-2014 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation, which is
# listed in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Author: Stephan Duehr
#
# Bareos python class for VMware related backup and restore
#
import bareosfd
from bareos_fd_consts import bJobMessageType, bFileType, bRCs, bIOPS
from bareos_fd_consts import bEventType, bVariable, bCFs
import BareosFdPluginBaseclass
import json
import os
import time
import tempfile
import subprocess
import shlex
import signal
# if OrderedDict is not available from collection (eg. SLES11),
# the additional package python-ordereddict must be used
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
from pyVmomi import vmodl
class BareosFdPluginVMware(BareosFdPluginBaseclass.BareosFdPluginBaseclass):
'''
VMware related backup and restore BAREOS fd-plugin methods
'''
def __init__(self, context, plugindef):
bareosfd.DebugMessage(
context, 100,
"Constructor called in module %s with plugindef=%s\n" %
(__name__, plugindef))
super(BareosFdPluginVMware, self).__init__(context, plugindef)
self.events = []
self.events.append(bEventType['bEventStartBackupJob'])
self.events.append(bEventType['bEventStartRestoreJob'])
bareosfd.RegisterEvents(context, self.events)
self.mandatory_options_default = ['vcserver', 'vcuser', 'vcpass']
self.mandatory_options_vmname = ['dc', 'folder', 'vmname']
self.vadp = BareosVADPWrapper()
def parse_plugin_definition(self, context, plugindef):
'''
Parses the plugin arguments
'''
bareosfd.DebugMessage(
context, 100,
"parse_plugin_definition() was called in module %s\n" %
(__name__))
super(BareosFdPluginVMware, self).parse_plugin_definition(
context, plugindef)
self.vadp.options = self.options
return bRCs['bRC_OK']
def check_options(self, context, mandatory_options=None):
'''
Check Plugin options
'''
if mandatory_options is None:
# not passed, use default
mandatory_options = self.mandatory_options_default
if 'uuid' not in self.options:
mandatory_options += self.mandatory_options_vmname
for option in mandatory_options:
if (option not in self.options):
bareosfd.DebugMessage(
context, 100, "Option \'%s\' not defined.\n" %
(option))
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Option \'%s\' not defined.\n" %
option)
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100, "Using Option %s=%s\n" %
(option, self.options[option]))
return bRCs['bRC_OK']
def start_backup_job(self, context):
'''
Start of Backup Job
'''
check_option_bRC = self.check_options(context)
if check_option_bRC != bRCs['bRC_OK']:
return check_option_bRC
accurate_enabled = bareosfd.GetValue(context, bVariable['bVarAccurate'])
if accurate_enabled != 0:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"start_backup_job: Accurate backup not allowed please disable in Job\n")
return bRCs['bRC_Error']
if not self.vadp.connect_vmware(context):
return bRCs['bRC_Error']
return self.vadp.prepare_vm_backup(context)
def start_backup_file(self, context, savepkt):
'''
Defines the file to backup and creates the savepkt.
'''
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:start_backup_file() called\n")
if not self.vadp.files_to_backup:
self.vadp.disk_device_to_backup = self.vadp.disk_devices.pop(0)
self.vadp.files_to_backup = []
if 'uuid' in self.options:
self.vadp.files_to_backup.append('/VMS/%s/%s' % (
self.options['uuid'],
self.vadp.disk_device_to_backup['fileNameRoot'])
)
else:
self.vadp.files_to_backup.append('/VMS/%s%s/%s/%s' % (
self.options['dc'],
self.options['folder'].rstrip('/'),
self.options['vmname'],
self.vadp.disk_device_to_backup['fileNameRoot'])
)
self.vadp.files_to_backup.insert(
0,
self.vadp.files_to_backup[0] + '_cbt.json')
self.vadp.file_to_backup = self.vadp.files_to_backup.pop(0)
bareosfd.DebugMessage(
context, 100,
"file: %s\n" %
(self.vadp.file_to_backup))
if self.vadp.file_to_backup.endswith('_cbt.json'):
if not self.vadp.get_vm_disk_cbt(context):
return bRCs['bRC_Error']
# create a stat packet for a restore object
statp = bareosfd.StatPacket()
savepkt.statp = statp
# see src/filed/backup.c how this is done in c
savepkt.type = bFileType['FT_RESTORE_FIRST']
# set the fname of the restore object to the vmdk name
# by stripping of the _cbt.json suffix
v_fname = self.vadp.file_to_backup[:-len('_cbt.json')]
if chr(self.level) != 'F':
# add level and timestamp to fname in savepkt
savepkt.fname = "%s+%s+%s" % (v_fname,
chr(self.level),
repr(self.vadp.create_snap_tstamp))
else:
savepkt.fname = v_fname
savepkt.object_name = savepkt.fname
savepkt.object = bytearray(self.vadp.changed_disk_areas_json)
savepkt.object_len = len(savepkt.object)
savepkt.object_index = int(time.time())
else:
# start bareos_vadp_dumper
self.vadp.start_dumper(context, 'dump')
# create a regular stat packet
statp = bareosfd.StatPacket()
savepkt.statp = statp
savepkt.fname = self.vadp.file_to_backup
if chr(self.level) != 'F':
# add level and timestamp to fname in savepkt
savepkt.fname = "%s+%s+%s" % (self.vadp.file_to_backup,
chr(self.level),
repr(self.vadp.create_snap_tstamp))
savepkt.type = bFileType['FT_REG']
bareosfd.JobMessage(
context, bJobMessageType['M_INFO'],
"Starting backup of %s\n" %
self.vadp.file_to_backup)
return bRCs['bRC_OK']
def start_restore_job(self, context):
'''
Start of Restore Job
'''
check_option_bRC = self.check_options(context)
if check_option_bRC != bRCs['bRC_OK']:
return check_option_bRC
if not self.vadp.connect_vmware(context):
return bRCs['bRC_Error']
return self.vadp.prepare_vm_restore(context)
def start_restore_file(self, context, cmd):
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:start_restore_file() called with %s\n" %
(cmd))
return bRCs['bRC_OK']
def create_file(self, context, restorepkt):
'''
For the time being, assumes that the virtual disk already
exists with the same name that has been backed up.
This should work for restoring the same VM.
For restoring to a new different VM, additional steps
must be taken, because the disk path will be different.
'''
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:create_file() called with %s\n" %
(restorepkt))
tmp_path = '/var/tmp/bareos-vmware-plugin'
objectname = '/' + os.path.relpath(restorepkt.ofname, restorepkt.where)
json_filename = tmp_path + objectname + '_cbt.json'
# for now, restore requires no snapshot to exist so disk to
# be written must be the the root-disk, even if a manual snapshot
# existed when the backup was run. So the diskPath in JSON will
# be set to diskPathRoot
cbt_data = self.vadp.restore_objects_by_objectname[objectname]['data']
cbt_data['DiskParams']['diskPath'] = cbt_data['DiskParams']['diskPathRoot']
self.vadp.writeStringToFile(context,
json_filename,
json.dumps(cbt_data))
self.cbt_json_local_file_path = json_filename
self.vadp.start_dumper(context, 'restore')
if restorepkt.type == bFileType['FT_REG']:
restorepkt.create_status = bCFs['CF_EXTRACT']
return bRCs['bRC_OK']
def plugin_io(self, context, IOP):
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:plugin_io() called with function %s\n" %
(IOP.func))
bareosfd.DebugMessage(
context, 100, "self.FNAME is set to %s\n" % (self.FNAME))
self.vadp.keepalive()
if IOP.func == bIOPS['IO_OPEN']:
self.FNAME = IOP.fname
bareosfd.DebugMessage(
context, 100,
"self.FNAME was set to %s from IOP.fname\n" %
(self.FNAME))
try:
if IOP.flags & (os.O_CREAT | os.O_WRONLY):
bareosfd.DebugMessage(
context, 100,
"Open file %s for writing with %s\n" %
(self.FNAME, IOP))
# this is a restore
# create_file() should have started
# bareos_vadp_dumper, check:
if self.vadp.dumper_process:
bareosfd.DebugMessage(
context, 100,
("plugin_io: bareos_vadp_dumper running with"
" PID %s\n") %
(self.vadp.dumper_process.pid))
else:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"plugin_io: bareos_vadp_dumper not running\n")
return bRCs['bRC_Error']
else:
bareosfd.DebugMessage(
context, 100,
"plugin_io: trying to open %s for reading\n" %
(self.FNAME))
# this is a backup
# start_backup_file() should have started
# bareos_vadp_dumper, check:
if self.vadp.dumper_process:
bareosfd.DebugMessage(
context, 100,
("plugin_io: bareos_vadp_dumper running with"
" PID %s\n") %
(self.vadp.dumper_process.pid))
else:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"plugin_io: bareos_vadp_dumper not running\n")
return bRCs['bRC_Error']
except:
IOP.status = -1
bareosfd.DebugMessage(
context, 100, "plugin_io: failed to open %s\n" %
(self.FNAME))
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"plugin_io: failed to open %s\n" %
(self.FNAME))
return bRCs['bRC_Error']
return bRCs['bRC_OK']
elif IOP.func == bIOPS['IO_CLOSE']:
if self.jobType == 'B':
# Backup Job
bareosfd.DebugMessage(
context, 100,
("plugin_io: calling end_dumper() to wait for"
" PID %s to terminate\n") %
(self.vadp.dumper_process.pid))
bareos_vadp_dumper_returncode = self.vadp.end_dumper(context)
if bareos_vadp_dumper_returncode != 0:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
("plugin_io[IO_CLOSE]: bareos_vadp_dumper returncode:"
" %s\n") %
(bareos_vadp_dumper_returncode))
return bRCs['bRC_Error']
elif self.jobType == 'R':
# Restore Job
bareosfd.DebugMessage(
context, 100,
"Closing Pipe to bareos_vadp_dumper for %s\n" %
(self.FNAME))
self.vadp.dumper_process.stdin.close()
bareosfd.DebugMessage(
context, 100,
("plugin_io: calling end_dumper() to wait for"
" PID %s to terminate\n") %
(self.vadp.dumper_process.pid))
bareos_vadp_dumper_returncode = self.vadp.end_dumper(context)
if bareos_vadp_dumper_returncode != 0:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
("plugin_io[IO_CLOSE]: bareos_vadp_dumper returncode:"
" %s\n") %
(bareos_vadp_dumper_returncode))
return bRCs['bRC_Error']
else:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"plugin_io[IO_CLOSE]: unknown Job Type %s\n" %
(self.jobType))
return bRCs['bRC_Error']
return bRCs['bRC_OK']
elif IOP.func == bIOPS['IO_SEEK']:
return bRCs['bRC_OK']
elif IOP.func == bIOPS['IO_READ']:
IOP.buf = bytearray(IOP.count)
IOP.status = self.vadp.dumper_process.stdout.readinto(
IOP.buf)
IOP.io_errno = 0
return bRCs['bRC_OK']
elif IOP.func == bIOPS['IO_WRITE']:
self.vadp.dumper_process.stdin.write(IOP.buf)
IOP.status = IOP.count
IOP.io_errno = 0
return bRCs['bRC_OK']
def handle_plugin_event(self, context, event):
if event in self.events:
self.jobType = chr(bareosfd.GetValue(context, bVariable['bVarType']))
bareosfd.DebugMessage(
context, 100,
"jobType: %s\n" %
(self.jobType))
if event == bEventType['bEventJobEnd']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventJobEnd\n")
bareosfd.DebugMessage(
context, 100,
"Disconnecting from VSphere API on host %s with user %s\n" %
(self.options['vcserver'], self.options['vcuser']))
self.vadp.cleanup()
elif event == bEventType['bEventEndBackupJob']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventEndBackupJob\n")
bareosfd.DebugMessage(
context, 100,
"removing Snapshot\n")
self.vadp.remove_vm_snapshot(context)
elif event == bEventType['bEventEndFileSet']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventEndFileSet\n")
elif event == bEventType['bEventStartBackupJob']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventStartBackupJob\n")
return self.start_backup_job(context)
elif event == bEventType['bEventStartRestoreJob']:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with bEventStartRestoreJob\n")
return self.start_restore_job(context)
else:
bareosfd.DebugMessage(
context, 100,
"handle_plugin_event() called with event %s\n" %
(event))
return bRCs['bRC_OK']
def end_backup_file(self, context):
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:end_backup_file() called\n")
if self.vadp.disk_devices or self.vadp.files_to_backup:
bareosfd.DebugMessage(
context, 100,
"end_backup_file(): returning bRC_More\n")
return bRCs['bRC_More']
bareosfd.DebugMessage(
context, 100,
"end_backup_file(): returning bRC_OK\n")
return bRCs['bRC_OK']
def restore_object_data(self, context, ROP):
"""
Note:
This is called in two cases:
- on diff/inc backup (should be called only once)
- on restore (for every job id being restored)
But at the point in time called, it is not possible
to distinguish which of them it is, because job type
is "I" until the bEventStartBackupJob event
"""
bareosfd.DebugMessage(
context, 100,
"BareosFdPluginVMware:restore_object_data() called with ROP:%s\n" %
(ROP))
bareosfd.DebugMessage(
context, 100,
"ROP.object_name(%s): %s\n" %
(type(ROP.object_name), ROP.object_name))
bareosfd.DebugMessage(
context, 100,
"ROP.plugin_name(%s): %s\n" %
(type(ROP.plugin_name), ROP.plugin_name))
bareosfd.DebugMessage(
context, 100,
"ROP.object_len(%s): %s\n" %
(type(ROP.object_len), ROP.object_len))
bareosfd.DebugMessage(
context, 100,
"ROP.object_full_len(%s): %s\n" %
(type(ROP.object_full_len), ROP.object_full_len))
bareosfd.DebugMessage(
context, 100,
"ROP.object(%s): %s\n" %
(type(ROP.object), ROP.object))
ro_data = self.vadp.json2cbt(str(ROP.object))
ro_filename = ro_data['DiskParams']['diskPathRoot']
# self.vadp.restore_objects_by_diskpath is used on backup
# in get_vm_disk_cbt()
# self.vadp.restore_objects_by_objectname is used on restore
# by create_file()
self.vadp.restore_objects_by_diskpath[ro_filename] = []
self.vadp.restore_objects_by_diskpath[ro_filename].append(
{'json': ROP.object,
'data': ro_data})
self.vadp.restore_objects_by_objectname[ROP.object_name] = \
self.vadp.restore_objects_by_diskpath[ro_filename][-1]
return bRCs['bRC_OK']
class BareosVADPWrapper(object):
'''
VADP specific class.
'''
def __init__(self):
self.si = None
self.si_last_keepalive = None
self.vm = None
self.create_snap_task = None
self.create_snap_result = None
self.file_to_backup = None
self.files_to_backup = None
self.disk_devices = None
self.disk_device_to_backup = None
self.cbt_json_local_file_path = None
self.dumper_process = None
self.changed_disk_areas_json = None
self.restore_objects_by_diskpath = {}
self.restore_objects_by_objectname = {}
self.options = None
self.skip_disk_modes = ['independent_nonpersistent',
'independent_persistent']
def connect_vmware(self, context):
# this prevents from repeating on second call
if self.si:
bareosfd.DebugMessage(
context, 100,
"connect_vmware(): connection to server %s already exists\n" %
(self.options['vcserver']))
return True
bareosfd.DebugMessage(
context, 100,
"connect_vmware(): connecting server %s\n" %
(self.options['vcserver']))
try:
self.si = SmartConnect(host=self.options['vcserver'],
user=self.options['vcuser'],
pwd=self.options['vcpass'],
port=443)
self.si_last_keepalive = int(time.time())
except IOError:
pass
if not self.si:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Cannot connect to host %s with user %s and password\n" %
(self.options['vcserver'], self.options['vcuser']))
return False
bareosfd.DebugMessage(
context, 100,
("Successfully connected to VSphere API on host %s with"
" user %s\n") %
(self.options['vcserver'], self.options['vcuser']))
return True
def cleanup(self):
Disconnect(self.si)
# the Disconnect Method does not close the tcp connection
# is that so intentionally?
# However, explicitly closing it works like this:
self.si._stub.DropConnections()
self.si = None
self.log = None
def keepalive(self):
# FIXME: prevent from vSphere API timeout, needed until pyvmomi fixes
# https://github.com/vmware/pyvmomi/issues/239
# otherwise idle timeout occurs after 20 minutes (1200s),
# so call CurrentTime() every 15min (900s) to keep alive
if int(time.time()) - self.si_last_keepalive > 900:
self.si.CurrentTime()
self.si_last_keepalive = int(time.time())
def prepare_vm_backup(self, context):
'''
prepare VM backup:
- get vm details
- take snapshot
- get disk devices
'''
if 'uuid' in self.options:
vmname = self.options['uuid']
if not self.get_vm_details_by_uuid(context):
bareosfd.DebugMessage(
context, 100, "Error getting details for VM %s\n" %
(vmname))
return bRCs['bRC_Error']
else:
vmname = self.options['vmname']
if not self.get_vm_details_dc_folder_vmname(context):
bareosfd.DebugMessage(
context, 100, "Error getting details for VM %s\n" %
(vmname))
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100, "Successfully got details for VM %s\n" %
(vmname))
# check if the VM supports CBT and that CBT is enabled
if not self.vm.capability.changeTrackingSupported:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error VM %s does not support CBT\n" %
(vmname))
return bRCs['bRC_Error']
if not self.vm.config.changeTrackingEnabled:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error VM %s is not CBT enabled\n" %
(vmname))
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100, "Creating Snapshot on VM %s\n" %
(vmname))
if not self.create_vm_snapshot(context):
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error creating snapshot on VM %s\n" %
(vmname))
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100,
"Successfully created snapshot on VM %s\n" %
(vmname))
bareosfd.DebugMessage(
context, 100, "Getting Disk Devices on VM %s from snapshot\n" %
(vmname))
self.get_vm_snap_disk_devices(context)
if not self.disk_devices:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error getting Disk Devices on VM %s from snapshot\n" %
(vmname))
return bRCs['bRC_Error']
return bRCs['bRC_OK']
def prepare_vm_restore(self, context):
'''
prepare VM restore:
- get vm details
- ensure vm is powered off
- get disk devices
'''
if 'uuid' in self.options:
vmname = self.options['uuid']
if not self.get_vm_details_by_uuid(context):
bareosfd.DebugMessage(
context, 100, "Error getting details for VM %s\n" %
(vmname))
return bRCs['bRC_Error']
else:
vmname = self.options['vmname']
if not self.get_vm_details_dc_folder_vmname(context):
bareosfd.DebugMessage(
context, 100, "Error getting details for VM %s\n" %
(vmname))
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100, "Successfully got details for VM %s\n" %
(vmname))
vm_power_state = self.vm.summary.runtime.powerState
if vm_power_state != 'poweredOff':
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error VM %s must be poweredOff for restore, but is %s\n" %
(vmname, vm_power_state))
return bRCs['bRC_Error']
if self.vm.snapshot is not None:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error VM %s must not have any snapshots before restore\n" %
(vmname))
return bRCs['bRC_Error']
bareosfd.DebugMessage(
context, 100, "Getting Disk Devices on VM %s\n" %
(vmname))
self.get_vm_disk_devices(context)
if not self.disk_devices:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Error getting Disk Devices on VM %s\n" %
(vmname))
return bRCs['bRC_Error']
# make sure backed up disks match VM disks
if not self.check_vm_disks_match(context):
return bRCs['bRC_Error']
return bRCs['bRC_OK']
def get_vm_details_dc_folder_vmname(self, context):
'''
Get details of VM given by plugin options dc, folder, vmname
and save result in self.vm
Returns True on success, False otherwise
'''
content = self.si.content
dcView = content.viewManager.CreateContainerView(content.rootFolder,
[vim.Datacenter],
False)
vmListWithFolder = {}
dcList = dcView.view
dcView.Destroy()
for dc in dcList:
if dc.name == self.options['dc']:
folder = ''
self._get_dcftree(vmListWithFolder, folder, dc.vmFolder)
if self.options['folder'].endswith('/'):
vm_path = "%s%s" % (self.options['folder'], self.options['vmname'])
else:
vm_path = "%s/%s" % (self.options['folder'], self.options['vmname'])
if vm_path not in vmListWithFolder:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"No VM with Folder/Name %s found in DC %s\n" %
(vm_path, self.options['dc']))
return False
self.vm = vmListWithFolder[vm_path]
return True
def get_vm_details_by_uuid(self, context):
'''
Get details of VM given by plugin options uuid
and save result in self.vm
Returns True on success, False otherwise
'''
search_index = self.si.content.searchIndex
self.vm = search_index.FindByUuid(None, self.options['uuid'], True, True)
if self.vm is None:
return False
else:
return True
def _get_dcftree(self, dcf, folder, vm_folder):
'''
Recursive function to get VMs with folder names
'''
for vm_or_folder in vm_folder.childEntity:
if isinstance(vm_or_folder, vim.VirtualMachine):
dcf[folder + '/' + vm_or_folder.name] = vm_or_folder
elif isinstance(vm_or_folder, vim.Folder):
self._get_dcftree(dcf, folder + '/' + vm_or_folder.name, vm_or_folder)
def create_vm_snapshot(self, context):
'''
Creates a snapshot
'''
try:
self.create_snap_task = self.vm.CreateSnapshot_Task(
name='BareosTmpSnap',
description='Description for BareosTmpSnap',
memory=False,
quiesce=True)
except vmodl.MethodFault as e:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"Failed to create snapshot %s\n" % (e.msg))
return False
self.vmomi_WaitForTasks([self.create_snap_task])
self.create_snap_result = self.create_snap_task.info.result
self.create_snap_tstamp = time.time()
return True
def remove_vm_snapshot(self, context):
'''
Removes the snapshot taken before
'''
if not self.create_snap_result:
bareosfd.JobMessage(
context, bJobMessageType['M_WARNING'],
"No snapshot was taken, skipping snapshot removal\n")
return False
try:
rmsnap_task = \
self.create_snap_result.RemoveSnapshot_Task(
removeChildren=True)
except vmodl.MethodFault as e:
bareosfd.JobMessage(
context, bJobMessageType['M_WARNING'],
"Failed to remove snapshot %s\n" % (e.msg))
return False
self.vmomi_WaitForTasks([rmsnap_task])
return True
def get_vm_snap_disk_devices(self, context):
'''
Get the disk devices from the created snapshot
Assumption: Snapshot successfully created
'''
self.get_disk_devices(context, self.create_snap_result.config.hardware.device)
def get_vm_disk_devices(self, context):
'''
Get the disk devices from vm
'''
self.get_disk_devices(context, self.vm.config.hardware.device)
def get_disk_devices(self, context, devicespec):
'''
Get disk devices from a devicespec
'''
self.disk_devices = []
for hw_device in devicespec:
if type(hw_device) == vim.vm.device.VirtualDisk:
if hw_device.backing.diskMode in self.skip_disk_modes:
bareosfd.JobMessage(
context, bJobMessageType['M_INFO'],
"Skipping Disk %s because mode is %s\n" %
(self.get_vm_disk_root_filename(hw_device.backing),
hw_device.backing.diskMode))
continue
self.disk_devices.append(
{'deviceKey': hw_device.key,
'fileName': hw_device.backing.fileName,
'fileNameRoot': self.get_vm_disk_root_filename(
hw_device.backing),
'changeId': hw_device.backing.changeId})
def get_vm_disk_root_filename(self, disk_device_backing):
'''
Get the disk name from the ende of the parents chain
When snapshots exist, the original disk filename is
needed. If no snapshots exist, the disk has no parent
and the filename is the same.
'''
actual_backing = disk_device_backing
while actual_backing.parent:
actual_backing = actual_backing.parent
return actual_backing.fileName
def get_vm_disk_cbt(self, context):
'''
Get CBT Information
'''
cbt_changeId = '*'
if self.disk_device_to_backup['fileNameRoot'] in self.restore_objects_by_diskpath:
if len(self.restore_objects_by_diskpath[
self.disk_device_to_backup['fileNameRoot']]) > 1:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"ERROR: more then one CBT info for Diff/Inc exists\n")
return False
cbt_changeId = self.restore_objects_by_diskpath[
self.disk_device_to_backup['fileNameRoot']][0][
'data']['DiskParams']['changeId']
bareosfd.DebugMessage(
context, 100,
"get_vm_disk_cbt(): using changeId %s from restore object\n" %
(cbt_changeId))
self.changed_disk_areas = self.vm.QueryChangedDiskAreas(
snapshot=self.create_snap_result,
deviceKey=self.disk_device_to_backup['deviceKey'],
startOffset=0,
changeId=cbt_changeId)
self.cbt2json(context)
return True
def check_vm_disks_match(self, context):
'''
Check if the backed up disks match selecte VM disks
'''
backed_up_disks = set(self.restore_objects_by_diskpath.keys())
vm_disks = set([disk_dev['fileNameRoot'] for disk_dev in self.disk_devices])
if backed_up_disks == vm_disks:
bareosfd.DebugMessage(
context, 100,
"check_vm_disks_match(): OK, VM disks match backed up disks\n")
return True
bareosfd.JobMessage(
context, bJobMessageType['M_WARNING'],
"VM Disks: %s\n" % (', '.join(vm_disks)))
bareosfd.JobMessage(
context, bJobMessageType['M_WARNING'],
"Backed up Disks: %s\n" % (', '.join(backed_up_disks)))
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],
"ERROR: VM disks not matching backed up disks\n")
return False
def cbt2json(self, context):
'''
Convert CBT data into json serializable structure and
return it as json string
'''
# the order of keys in JSON data must be preserved for
# bareos_vadp_dumper to work properly, this is done
# by using the OrderedDict type
cbt_data = OrderedDict()
cbt_data['ConnParams'] = {}
cbt_data['ConnParams']['VmMoRef'] = 'moref=' + self.vm._moId
cbt_data['ConnParams']['VsphereHostName'] = self.options['vcserver']
cbt_data['ConnParams']['VsphereUsername'] = self.options['vcuser']
cbt_data['ConnParams']['VspherePassword'] = self.options['vcpass']
cbt_data['ConnParams'][
'VsphereSnapshotMoRef'] = self.create_snap_result._moId
# disk params for bareos_vadp_dumper
cbt_data['DiskParams'] = {}
cbt_data['DiskParams']['diskPath'] = self.disk_device_to_backup['fileName']
cbt_data['DiskParams']['diskPathRoot'] = self.disk_device_to_backup['fileNameRoot']
cbt_data['DiskParams']['changeId'] = self.disk_device_to_backup['changeId']
# cbt data for bareos_vadp_dumper
cbt_data['DiskChangeInfo'] = {}
cbt_data['DiskChangeInfo']['startOffset'] = self.changed_disk_areas.startOffset
cbt_data['DiskChangeInfo']['length'] = self.changed_disk_areas.length
cbt_data['DiskChangeInfo']['changedArea'] = []
for extent in self.changed_disk_areas.changedArea:
cbt_data['DiskChangeInfo']['changedArea'].append(
{'start': extent.start, 'length': extent.length})
self.changed_disk_areas_json = json.dumps(cbt_data)
self.writeStringToFile(
context,
'/var/tmp' +
self.file_to_backup,
self.changed_disk_areas_json)
def json2cbt(self, cbt_json_string):
'''
Convert JSON string from restore object to ordered dict
to preserve the key order required for bareos_vadp_dumper
to work properly
return OrderedDict
'''
# the order of keys in JSON data must be preserved for
# bareos_vadp_dumper to work properly
cbt_data = OrderedDict()
cbt_keys_ordered = ['ConnParams', 'DiskParams', 'DiskChangeInfo']
cbt_data_tmp = json.loads(cbt_json_string)
for cbt_key in cbt_keys_ordered:
cbt_data[cbt_key] = cbt_data_tmp[cbt_key]
return cbt_data
def dumpJSONfile(self, context, filename, data):
"""
Write a Python data structure in JSON format to the given file.
Note: obsolete, no longer used because order of keys in JSON
string must be preserved
"""
bareosfd.DebugMessage(
context, 100, "dumpJSONfile(): writing JSON data to file %s\n" %
(filename))
try:
out = open(filename, 'w')
json.dump(data, out)
out.close()
bareosfd.DebugMessage(
context, 100,
"dumpJSONfile(): successfully wrote JSON data to file %s\n" %
(filename))
except IOError as io_error:
bareosfd.JobMessage(
context, bJobMessageType['M_FATAL'],