-
Notifications
You must be signed in to change notification settings - Fork 1
/
xenbackup.pl
executable file
·673 lines (494 loc) · 16.4 KB
/
xenbackup.pl
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
#!/usr/bin/perl
# XenBackup-ng by Riccardo Bicelli - https://think-brick.blogspot.com
#
# This program is released under the MIT License
#
# built on "XEN Server backup by Filippo Zanardo - http://pipposan.wordpress.com"
#
# Usage: "perl xenbackup.pl <job name>", where job name is the name of the job file located in subfolder "jobs", without ".conf" suffix.
#
# Difference between this and Xenbackup
# 1. Subroutines
# 2. Separation of configuration files and hierarchy:
# - conf/strings.conf -> Where strings of log and mail notification can be localized
# - conf/xenbackup.conf -> global configuration file, where you can specify also default parameters if you are not using jobs
# - jobs/job.conf -> Job File, loaded after xenbackup.conf
# 3. Now backup is possible with VM Selection
# 4. Quiesce snapshot for Windows VM (Only if guest has Xenserver VSS provider installed)
# 5. Mail Notification now comes with a head Section with a checklist of backed up VM and verbose log attached, if wanted
# 6. More controls on commands executed (e.g. Exit codes checked after some command execution)
# 7. Keeps care of running state of the VM (e.g if a VM in in state halted it is not started after backup job)
# 8. Versioning (deletion of files) executed only if backup process went fine
#SCRIPT_VERSION = "1.1.0";
use Switch;
#Load Strings File
loadFile("./conf/strings.conf");
#Load Default Config File
loadFile("./conf/xenbackup.conf");
#Load Job File
loadFile("./jobs/".$ARGV[0].".conf");
#Init Some variables
$timestamp = `date +%d%m%Y%H%M%S`;
$timestamp = substr($timestamp,0,-1);
$hostname = `hostname`;
$hostname =~ s/\r|\n//g;
$tstarted = `date`; # Get the starting date
$gstarttime = time();
$backupResult = $OK;
$tmp_maillog = "/tmp/xenbackup-$timestamp";
#xe command
if ($xe_remote eq true) {
$xe_opts = "-s $xe_remote_host -pwf $xe_remote_pwfile";
} else {
$xe_opts='';
}
#Open Log Files
if ($appendLog eq true) { # If Verbose Logging is Selected, then open job's log file, that will be appended to mail notification.
open (F_LOG_A, ">$tmp_maillog");
print F_LOG_A "$Line\n";
print F_LOG_A "$BackupLog\n";
print F_LOG_A "$Line\n";
}
if ($logging eq true) { # Open Log File
open (F_LOG, ">>$logfile");
}
logLine("$Beginning $hostname, $Job: [$jobName]");
$b=substr($backupdir, 0, -1);
# Mount Backup Media
if ($Automount eq true ){
$ismounted=`mount |grep $b`;
if ($ismounted == "") {
$mount = `$MountCommand`;
logLine("$Mounting\n");
}else{
logLine("$NotMount\n");
}
}
# Check Space: check prior size of the VM, then check the space of the backup media.
if ($checkspace eq true) {
$actualspace = `df -B M | grep $b`;
@spaceman = split(' ', $actualspace);
$i = 0;
foreach my $val (@spaceman) {
if ($val eq $b ) {
$k = $i -2;
$aspace = @spaceman[$k];
$aspace=substr($aspace, 0, -1);
}
$i = $i +1;
}
if ($aspace <= $spacerequired) {
logLine("$backerror $nospace");
if ($Automount eq true ){
logLine("$Executing: $UMountCommand");
$mount = `$UMountCommand`;
}
if ($mailNotification){
sendLogMail();
}
die "$backerror $nospace";
}
}
# Expand Selection
$vmlist = `xe vm-list $xe_opts`; # Get the formatted list of guests
@lineList = split(/\n/,$vmlist); # Split the list of guests into and array of lines
@uuid = (); # Array to store uuid's in
foreach $line(@lineList){ #for each line in the array
if (substr($line,0,4) eq "uuid"){ #look for the word uuid at the beginning of the line
push(@uuid, substr($line,23,36)); #if its there add the uuid to the array
}
}
@uuid_b = (); #Array to store uuids to save
if ($fullbackup eq true) { #Full backup
logLine("$FullBackup $Selected");
foreach $guest(@uuid){ #Populate the array of guests to backup
$VMName = getVMName($guest);
$backupType = $FullBackup;
if (grep $_ eq $guest, @dom0){
logLine("$Skipping Dom0 $VMName ($guest)");
} else {
if (grep $_ eq $guest, @Selected) { #Skip VM in selection
logLine("$Skipping $SVMName ($guest)");
} else {
push(@uuid_b, $guest);
}
}
}
} else { #Selective Backup
logLine("$SelectiveBackup $Selected");
$backupType = $SelectiveBackup;
foreach $tags(@Selected_Tags) { #Add Tags to Selection
@vm_tag = vmList('tags:contains="' . $tags . '"');
@Selected = mergeSelection(\@Selected, \@vm_tag);
}
foreach $app(@Selected_vApps) { #Add Virtual Appliances to Selection
@app = vmAppliance($app);
@Selected = mergeSelection(\@Selected, \@app);
}
foreach $guest(@Selected) { #Cycle Selection of VM to backup
if (grep $_ eq $guest, @uuid) { #If guest exists in pool then add it to array of UUIDs to backup
$VMName = getVMName($guest);
push(@uuid_b, $guest);
} else {
logLine("$Selected $Guest $NonExistent: $guest");
}
}
}
if ($usesnap eq true) {
$backupMode = "Snapshot";
if ($quiesce eq true) {
$backupMode = "Snapshot/Quiesce";
}
} else {
$backupMode = "$ShutdownRestart";
}
foreach $guest(@uuid_b){
#otherwise
$tcurrent = `date`; #get the current date
$starttime=time();
$VMName=getVMName($guest);
logLine("$StartBackup $VMName: $guest");
$powerstate = getVMPowerstate($guest);
$VMResult = $OK;
if ( ($usesnap eq true) && ($powerstate ne 'halted') ){
#Begin Of Snapshot Backup
if ($removable eq true) {
detachRemovableDevices();
}
$SnapResult = 1;
if ($quiesce eq true) {
# Try Quiesce First
logLine("$TakingSnap $guest $Name $VMName $WithQuiesce");
$snapshotUUID= `xe vm-snapshot-with-quiesce vm=$guest new-name-label=$VMName-backup_vm $xe_opts`;
if ($? > 0) {
#If snapshot failed try with normal method
logLine("$QuiesceFailed");
$snapshotUUID= `xe vm-snapshot vm=$guest new-name-label=$VMName-backup_vm $xe_opts`;
}
} else {
logLine("$TakingSnap $guest $Name $VMName");
$snapshotUUID= `xe vm-snapshot vm=$guest new-name-label=$VMName-backup_vm $xe_opts`; #Snapshot the VM
}
$snapResult = $?;
#Reattach removable devices, anyway
if ($removable eq true) {
reattachRemovableDevices();
}
if ($snapResult eq 0) {
$snapshotUUID=~ s/\n//g; #remove the carrier return/linefeed
$snapshotUUID=~ s/\r//g;
logLine("$Snapshot $snapshotUUID $Created");
logLine("$Turning $snapshotUUID $Snap2Vm");
$status= `xe template-param-set is-a-template=false ha-always-run=false uuid=$snapshotUUID $xe_opts`;
$fdate = `date +%y%m%d-%H%M`;
$fdate=~ s/\n//g; #get the current date in a format we can write
$fdate=~ s/\r//g; #get the current date in a format we can write
logLine("$Exporting $snapshotUUID"); #export the snapshot
#End Of Snapshot Mode
} else {
logLine("$SnapshotFailed");
$VMResult = $Error;
}
}else{
#Shutdown-Export mode selected
if ($powerstate eq "running") {
logLine("$Shutting $VMName"); #export the snapshot
$shut = `xe vm-shutdown uuid=$guest $xe_opts`;
}
if ($removable eq true) {
detachRemovableDevices();
}
$fdate = `date +%y%m%d-%H%M`;
$fdate=~ s/\n//g; #get the current date in a format we can write
$fdate=~ s/\r//g; #get the current date in a format we can write
}
if ($subfolder eq true ){ #create folder structure
if (-d $backupdir.$VMName) {
}else {
mkdir($backupdir.$VMName,0777);
}
$exportstring = $inline_compress_cmd.$backupdir.$VMName."/".$VMName."-".$fdate.".xvatmp";
$finalname = $backupdir.$VMName."/".$VMName."-".$fdate.".xva";
$versiondir = $backupdir.$VMName;
}else{
$exportstring = $inline_compress_cmd.$backupdir.$VMName."-".$fdate.".xvatmp";
$finalname = $backupdir.$VMName."-".$fdate.".xva";
$versiondir = $backupdir;
}
logLine("$Exporting $exportstring - $finalname");
if ($compress eq true) {
$compressstring = "compress=true";
logLine("$Compression $Enabled");
} else {
$compressstring = "";
logLine("$Compression $Not $Enabled");
}
if (($usesnap eq true) && ($powerstate ne 'halted')){
$status= `xe vm-export $xe_opts vm=$snapshotUUID $compressstring filename=$exportstring`;
}else{
$status= `xe vm-export $xe_opts uuid=$guest $compressstring filename=$exportstring`;
}
logLine("$status");
logLine("$Rename");
$status= `mv -vf $exportstring $finalname`;
logLine("$status");
if ($compress_after eq true) {
#Compress backup
if (substr($compressext,0,1) eq ".") {
logLine("$Executing $compresscmd $finalname$compressext $finalname");
$status= `$compresscmd $finalname$compressext $finalname`;
#logLine("$status");
$status =`rm -f $finalname`;
$finalname = $finalname.$compressext;
logLine("$logdate $finalname");
}else{
logLine("$logdate $Executing $compresscmd $finalname.$compressext $finalname");
$status= `$compresscmd $finalname.$compressext $finalname`;
logLine("$status");
$finalname = $finalname.".".$compressext;
logLine("$finalname");
}
}
if (($usesnap eq true) && ($powerstate ne 'halted')) {
#Uninstall Snapshot
$status= `xe vm-uninstall uuid=$snapshotUUID force=true $xe_opts`;
if ($? eq 0) {
logLine("$DoneR $snapshotUUID");
} else {
$VMResult = "!?";
$backupResult = "!?";
logLine("$UnDoneR $snapshotUUID");
}
}else{
if ($removable eq true) {
reattachRemovableDevices();
}
if ($powerstate eq "running") {
logLine ("$Restarting $guest");
$status = `xe vm-start uuid=$guest $xe_opts`;
}
}
logLine ("$status");
if (-e $finalname){
if ($versioning eq true ){
switch ($delmethod) {
case "numbers" {
@files = <$versiondir/*>;
$count = @files;
$count = $count - $delnumber;
my @sorted_files = sort { -M $a < -M $b } @files;
for($i = 0; $i < $count; $i++) {
# PRINT A NEW ROW EACH TIME DELETING A FILE
logLine("$Deleting".@sorted_files[$i]);
unlink(@sorted_files[$i]);
}
}
case "hours" {
opendir(DIR, $versiondir);
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
if (-M $versiondir."/".$file > $delnumber/24){
logLine($Deleting.$versiondir."/".$file);
unlink($versiondir."/".$file);
}
}
}
case "days" {
opendir(DIR, $versiondir);
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
if (-M $versiondir."/".$file > $delnumber){
logLine ($Deleting.$versiondir."/".$file);
unlink($versiondir."/".$file);
}
}
}
}
}
$tcurrent = `date`; #get the current date
$finishtime = time();
$elapsedtime = hhmmss($finishtime-$starttime);
logLine("$Completed $VMName $Elapsed $elapsedtime");
}else{
logLine("$backerror $nofile");
$VMResult = "!!";
$backupResult = "!!";
}
$finishtime=time();
$elapsedtime = hhmmss($finishtime-$starttime);
$mailString .= "[$VMResult] VM: $VMName {$guest}, $Elapsed $elapsedtime\n";
}
if ($Automount eq true ){
logLine("$Unmounting");
$mount = `$UMountCommand`;
}
$tfinished = `date`;
$gfinishtime = time();
$gtotaltime = ($gfinishtime - $gstarttime);
logLine ("$Finished $tfinished");
#Init mail File
if ($appendLog eq true) {
close (F_LOG_A);
}
if ($logging eq true) {
close (F_LOG);
}
if ($mailNotification eq true){
sendLogMail();
}
#Delete Temp Files
unlink($tmp_maillog);
################################################################################################
# Functions/Sub Library
################################################################################################
sub logLine {
# Logs a line to the log file, requires log file to be opened before
#
local $logdate;
$logdate=localtime();
if ($_[0] ne '') {
if ($logging eq true) {
print F_LOG "$logdate $_[0]\n";
}
if ($appendLog eq true) {
print F_LOG_A "$logdate $_[0]\n";
}
}
}
sub loadFile {
# This function loads and evals a file,relative to script directory
# Get script Directory
$0=~/^(.+[\\\/])[^\\\/]+[\\\/]*$/;
$scriptdir= $1 || "./";
$filename = $scriptdir . $_[0];
open L_FILE, $filename or die "Program stopping, couldn't open the file '$filename'.\n";
my $lfile = join "", <L_FILE>;
close L_FILE;
eval $lfile;
die "Couldn't interpret the file ($filename) that was given.\nError details follow: $@\n" if $@;
}
sub vmList {
$vmlist = `xe vm-list $_[0] $xe_opts`; # Get the formatted list of guests
@lineList = split(/\n/,$vmlist); # Split the list of guests into and array of lines
local @uuid = (); # Array to store uuid's in
foreach $line(@lineList){ #for each line in the array
if (substr($line,0,4) eq "uuid"){ #look for the word uuid at the beginning of the line
push(@uuid, substr($line,23,36)); #if its there add the uuid to the array
}
}
return @uuid;
}
sub getVMName {
local $rval;
# This Function gets a VM name given uuid in $1
$rval = `xe vm-list uuid=$_[0] $xe_opts | grep name-label | cut -b24-`;
$rval=~ s/\n//g;
$rval=~ s/\r//g;
$rval =~ s/ /_/g;
return($rval);
}
sub getVMPowerstate {
local $rval;
# This Function gets a VM name given uuid in $1
$rval = `xe vm-list uuid=$_[0] $xe_opts | grep power-state | cut -b24-`;
$rval=~ s/\n//g;
$rval=~ s/\r//g;
$rval =~ s/ /_/g;
return($rval);
}
sub detachRemovableDevices {
@toreattach = ();
@device = ();
foreach $rid(@removableuuid) {
#logLine("xe vdi-list sr-uuid=$rid params=vbd-uuids --minimal");
$srvbduuid = `xe vdi-list sr-uuid=$rid params=vbd-uuids --minimal $xe_opts`;
if ($srvbduuid) {
foreach $char (split //, $srvbduuid) {
if ($char eq ",") {
@srvbduuid = split(/,/,$srvbduuid);
last;
}else{
@srvbduuid = $srvbduuid;
}
}
foreach $mline(@srvbduuid){
$e = `xe vbd-list vm-uuid=$guest uuid=$mline $xe_opts`;
if ($e) {
@mlineList = split(/\n/,$e);
foreach $xline(@mlineList){
if (substr($xline,9,4) eq "vdi-"){
push(@toreattach, substr($xline,25,36));
}
if (substr($xline,11,6) eq "device"){
push(@device, substr($xline,25,4));
}
}
$unplug = `xe vbd-unplug uuid=$mline $xe_opts`;
$dest = `xe vbd-destroy uuid=$mline $xe_opts`;
}
}
}
}
}
sub reattachRemovableDevices {
logLine("$Reattaching $RemovableDevices");
if (@toreattach) {
$i = 0;
foreach $zline(@toreattach){
$create = `xe vbd-create vm-uuid=$guest vdi-uuid=$zline device=@device[$i] --minimal $xe_opts`;
$plug = `xe vbd-plug uuid=$create $xe_opts`;
$i=$i+1;
}
}
}
sub sendLogMail {
open (F_MAIL, ">$tmp_maillog-msg");
print F_MAIL "To:$MailTo\n";
print F_MAIL "From:$MailFrom\n";
print F_MAIL "Subject: [$backupResult] $SubjectHeader [$jobName]\n";
print F_MAIL "\n";
print F_MAIL "$MailIntro\n\n";
print F_MAIL "$HostName $hostname\n";
print F_MAIL "$JobName $jobName\n";
print F_MAIL "$BackupMode $backupType, $backupMode\n";
print F_MAIL "\n$JobDetails\n\n";
print F_MAIL "$mailString\n";
$timeelapsed = hhmmss($gtotaltime);
print F_MAIL "\n$TotalElapsed $timeelapsed \n";
close (F_MAIL);
if ($appendLog eq true) {
close F_LOG_A;
$send = `cat $tmp_maillog-msg $tmp_maillog > $tmp_maillog-send`;
$send = `$mailcmd $MailTo < $tmp_maillog-send`;
unlink("$tmp_maillog-send");
} else {
$send = `$mailcmd $MailTo < $tmp_maillog-msg`;
}
unlink("$tmp_maillog-msg");
}
sub mergeSelection {
@A = @{$_[0]};
@B = @{$_[1]};
@seen{@A} = ();
@merged = (@A, grep{!exists $seen{$_}} @B);
return @merged;
}
sub vmAppliance {
local $line = `xe appliance-list name-label=$_[0] $xe_opts | grep VMs | cut -b30-`;
local @ret = split ';', $line;
return @ret;
}
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
sub hhmmss {
my $hh=int($_[0]/3600);
my $lo=$_[0] % 3600;
my $mm=int($lo/60);
my $ss=int($lo % 60);
return sprintf ("%02d:%02d:%02d", $hh,$mm,$ss)
}