]> git.proxmox.com Git - pve-manager.git/blame - PVE/VZDump.pm
moved vzdump sources to this packages
[pve-manager.git] / PVE / VZDump.pm
CommitLineData
aaeeeebe
DM
1package PVE::VZDump;
2
3# Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
4#
5# Copyright: vzdump is under GNU GPL, the GNU General Public License.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; version 2 dated June, 1991.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the
18# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19# MA 02110-1301, USA.
20#
21# Author: Dietmar Maurer <dietmar@proxmox.com>
22
23use strict;
24use warnings;
25use Fcntl ':flock';
26use Sys::Hostname;
27use Sys::Syslog;
28use IO::File;
29use IO::Select;
30use IPC::Open3;
31use POSIX qw(strftime);
32use File::Path;
33use PVE::VZDump::OpenVZ;
34use Time::localtime;
35use Time::Local;
36
37my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
38
39my $lockfile = '/var/run/vzdump.lock';
40
41my $logdir = '/var/log/vzdump';
42
43my @plugins = qw (PVE::VZDump::OpenVZ);
44
45# Load available plugins
46my $pveplug = "/usr/share/perl5/PVE/VZDump/QemuServer.pm";
47if (-f $pveplug) {
48 eval { require $pveplug; };
49 if (!$@) {
50 PVE::VZDump::QemuServer->import ();
51 push @plugins, "PVE::VZDump::QemuServer";
52 } else {
53 warn $@;
54 }
55}
56
57# helper functions
58
59my $debugstattxt = {
60 err => 'ERROR:',
61 info => 'INFO:',
62 warn => 'WARN:',
63};
64
65sub debugmsg {
66 my ($mtype, $msg, $logfd, $syslog) = @_;
67
68 chomp $msg;
69
70 return if !$msg;
71
72 my $pre = $debugstattxt->{$mtype} || $debugstattxt->{'err'};
73
74 my $timestr = strftime ("%b %d %H:%M:%S", CORE::localtime);
75
76 syslog ($mtype eq 'info' ? 'info' : 'err', "$pre $msg") if $syslog;
77
78 foreach my $line (split (/\n/, $msg)) {
79 print STDERR "$pre $line\n";
80 print $logfd "$timestr $pre $line\n" if $logfd;
81 }
82}
83
84sub run_command {
85 my ($logfd, $cmdstr, %param) = @_;
86
87 my $timeout;
88 my $input;
89 my $output;
90
91 foreach my $p (keys %param) {
92 if ($p eq 'timeout') {
93 $timeout = $param{$p};
94 } elsif ($p eq 'input') {
95 $input = $param{$p};
96 } elsif ($p eq 'output') {
97 $output = $param{$p};
98 } else {
99 die "got unknown parameter '$p' for run_command\n";
100 }
101 }
102
103 my $reader = $output && $output =~ m/^>&/ ? $output : IO::File->new();
104 my $writer = $input && $input =~ m/^<&/ ? $input : IO::File->new();
105 my $error = IO::File->new();
106
107 my $orig_pid = $$;
108
109 my $pid;
110 eval {
111 # suppress LVM warnings like: "File descriptor 3 left open";
112 local $ENV{LVM_SUPPRESS_FD_WARNINGS} = "1";
113
114 $pid = open3 ($writer, $reader, $error, ($cmdstr)) || die $!;
115 };
116
117 my $err = $@;
118
119 # catch exec errors
120 if ($orig_pid != $$) {
121 debugmsg ('err', "command '$cmdstr' failed - fork failed: $!", $logfd);
122 POSIX::_exit (1);
123 kill ('KILL', $$);
124 }
125
126 die $err if $err;
127
128 if (ref($writer)) {
129 print $writer $input if defined $input;
130 close $writer;
131 }
132
133 my $select = new IO::Select;
134 $select->add ($reader) if ref($reader);
135 $select->add ($error);
136
137 my ($ostream, $estream, $logout, $logerr) = ('', '', '', '');
138
139 while ($select->count) {
140 my @handles = $select->can_read ($timeout);
141
142 if (defined ($timeout) && (scalar (@handles) == 0)) {
143 die "command '$cmdstr' failed: timeout\n";
144 }
145
146 foreach my $h (@handles) {
147 my $buf = '';
148 my $count = sysread ($h, $buf, 4096);
149 if (!defined ($count)) {
150 waitpid ($pid, 0);
151 die "command '$cmdstr' failed: $!\n";
152 }
153 $select->remove ($h) if !$count;
154
155 if ($h eq $reader) {
156 $ostream .= $buf;
157 $logout .= $buf;
158 while ($logout =~ s/^([^\n]*\n)//s) {
159 my $line = $1;
160 debugmsg ('info', $line, $logfd);
161 }
162 } elsif ($h eq $error) {
163 $estream .= $buf;
164 $logerr .= $buf;
165 while ($logerr =~ s/^([^\n]*\n)//s) {
166 my $line = $1;
167 debugmsg ('info', $line, $logfd);
168 }
169 }
170 }
171 }
172
173 debugmsg ('info', $logout, $logfd);
174 debugmsg ('info', $logerr, $logfd);
175
176 waitpid ($pid, 0);
177 my $ec = ($? >> 8);
178
179 return $ostream if $ec == 24 && ($cmdstr =~ m|^(\S+/)?rsync\s|);
180
181 die "command '$cmdstr' failed with exit code $ec\n" if $ec;
182
183 return $ostream;
184}
185
186sub storage_info {
187 my $storage = shift;
188
189 eval { require PVE::Storage; };
190 die "unable to query storage info for '$storage' - $@\n" if $@;
191 my $cfg = PVE::Storage::load_config();
192 my $scfg = PVE::Storage::storage_config ($cfg, $storage);
193 my $type = $scfg->{type};
194
195 die "can't use storage type '$type' for backup\n"
196 if (!($type eq 'dir' || $type eq 'nfs'));
197 die "can't use storage for backups - wrong content type\n"
198 if (!$scfg->{content}->{backup});
199
200 PVE::Storage::activate_storage ($cfg, $storage);
201
202 return {
203 dumpdir => $scfg->{path},
204 };
205}
206
207sub format_size {
208 my $size = shift;
209
210 my $kb = $size / 1024;
211
212 if ($kb < 1024) {
213 return int ($kb) . "KB";
214 }
215
216 my $mb = $size / (1024*1024);
217
218 if ($mb < 1024) {
219 return int ($mb) . "MB";
220 } else {
221 my $gb = $mb / 1024;
222 return sprintf ("%.2fGB", $gb);
223 }
224}
225
226sub format_time {
227 my $seconds = shift;
228
229 my $hours = int ($seconds/3600);
230 $seconds = $seconds - $hours*3600;
231 my $min = int ($seconds/60);
232 $seconds = $seconds - $min*60;
233
234 return sprintf ("%02d:%02d:%02d", $hours, $min, $seconds);
235}
236
237sub encode8bit {
238 my ($str) = @_;
239
240 $str =~ s/^(.{990})/$1\n/mg; # reduce line length
241
242 return $str;
243}
244
245sub escape_html {
246 my ($str) = @_;
247
248 $str =~ s/&/&amp;/g;
249 $str =~ s/</&lt;/g;
250 $str =~ s/>/&gt;/g;
251
252 return $str;
253}
254
255sub check_bin {
256 my ($bin) = @_;
257
258 foreach my $p (split (/:/, $ENV{PATH})) {
259 my $fn = "$p/$bin";
260 if (-x $fn) {
261 return $fn;
262 }
263 }
264
265 die "unable to find command '$bin'\n";
266}
267
268sub check_vmids {
269 my (@vmids) = @_;
270
271 my $res = [];
272 foreach my $vmid (@vmids) {
273 die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/;
274 $vmid = int ($vmid); # remove leading zeros
275 die "ERROR: got reserved VM ID '${vmid}'\n" if $vmid < 100;
276 push @$res, $vmid;
277 }
278
279 return $res;
280}
281
282
283sub read_vzdump_defaults {
284
285 my $fn = "/etc/vzdump.conf";
286
287 my $res = {
288 bwlimit => 0,
289 ionice => 7,
290 size => 1024,
291 lockwait => 3*60, # 3 hours
292 stopwait => 10, # 10 minutes
293 mode => 'snapshot',
294 maxfiles => 1,
295 };
296
297 my $fh = IO::File->new ("<$fn");
298 return $res if !$fh;
299
300 my $line;
301 while (defined ($line = <$fh>)) {
302 next if $line =~ m/^\s*$/;
303 next if $line =~ m/^\#/;
304
305 if ($line =~ m/tmpdir:\s*(.*\S)\s*$/) {
306 $res->{tmpdir} = $1;
307 } elsif ($line =~ m/dumpdir:\s*(.*\S)\s*$/) {
308 $res->{dumpdir} = $1;
309 } elsif ($line =~ m/storage:\s*(\S+)\s*$/) {
310 $res->{storage} = $1;
311 } elsif ($line =~ m/script:\s*(.*\S)\s*$/) {
312 $res->{script} = $1;
313 } elsif ($line =~ m/bwlimit:\s*(\d+)\s*$/) {
314 $res->{bwlimit} = int($1);
315 } elsif ($line =~ m/ionice:\s*([0-8])\s*$/) {
316 $res->{ionice} = int($1);
317 } elsif ($line =~ m/lockwait:\s*(\d+)\s*$/) {
318 $res->{lockwait} = int($1);
319 } elsif ($line =~ m/stopwait:\s*(\d+)\s*$/) {
320 $res->{stopwait} = int($1);
321 } elsif ($line =~ m/size:\s*(\d+)\s*$/) {
322 $res->{size} = int($1);
323 } elsif ($line =~ m/maxfiles:\s*(\d+)\s*$/) {
324 $res->{maxfiles} = int($1);
325 } elsif ($line =~ m/mode:\s*(stop|snapshot|suspend)\s*$/) {
326 $res->{mode} = $1;
327 } else {
328 debugmsg ('warn', "unable to parse configuration file '$fn' - error at line " . $., undef, 1);
329 }
330
331 }
332 close ($fh);
333
334 return $res;
335}
336
337
338sub find_add_exclude {
339 my ($self, $excltype, $value) = @_;
340
341 if (($excltype eq '-regex') || ($excltype eq '-files')) {
342 $value = "\.$value";
343 }
344
345 if ($excltype eq '-files') {
346 push @{$self->{findexcl}}, "'('", '-not', '-type', 'd', '-regex' , "'$value'", "')'", '-o';
347 } else {
348 push @{$self->{findexcl}}, "'('", $excltype , "'$value'", '-prune', "')'", '-o';
349 }
350}
351
352sub read_firstfile {
353 my $archive = shift;
354
355 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
356
357 # try to detect archive type first
358 my $pid = open (TMP, "tar tf '$archive'|") ||
359 die "unable to open file '$archive'\n";
360 my $firstfile = <TMP>;
361 kill 15, $pid;
362 close TMP;
363
364 die "ERROR: archive contaions no data\n" if !$firstfile;
365 chomp $firstfile;
366
367 return $firstfile;
368}
369
370my $sendmail = sub {
371 my ($self, $tasklist, $totaltime) = @_;
372
373 my $opts = $self->{opts};
374
375 my $mailto = $opts->{mailto};
376
377 return if !$mailto;
378
379 my $cmdline = $self->{cmdline};
380
381 my $ecount = 0;
382 foreach my $task (@$tasklist) {
383 $ecount++ if $task->{state} ne 'ok';
384 chomp $task->{msg} if $task->{msg};
385 $task->{backuptime} = 0 if !$task->{backuptime};
386 $task->{size} = 0 if !$task->{size};
387 $task->{tarfile} = 'unknown' if !$task->{tarfile};
388 $task->{hostname} = "VM $task->{vmid}" if !$task->{hostname};
389
390 if ($task->{state} eq 'todo') {
391 $task->{msg} = 'aborted';
392 }
393 }
394
395 my $stat = $ecount ? 'backup failed' : 'backup successful';
396
397 my $hostname = `hostname -f` || hostname();
398 chomp $hostname;
399
400
401 my $boundary = "----_=_NextPart_001_".int(time).$$;
402
403 my $rcvrarg = '';
404 foreach my $r (@$mailto) {
405 $rcvrarg .= " '$r'";
406 }
407
408 open (MAIL,"|sendmail -B 8BITMIME $rcvrarg") ||
409 die "unable to open 'sendmail' - $!";
410
411 my $rcvrtxt = join (', ', @$mailto);
412
413 print MAIL "Content-Type: multipart/alternative;\n";
414 print MAIL "\tboundary=\"$boundary\"\n";
415 print MAIL "FROM: vzdump backup tool <root>\n";
416 print MAIL "TO: $rcvrtxt\n";
417 print MAIL "SUBJECT: vzdump backup status ($hostname) : $stat\n";
418 print MAIL "\n";
419 print MAIL "This is a multi-part message in MIME format.\n\n";
420 print MAIL "--$boundary\n";
421
422 print MAIL "Content-Type: text/plain;\n";
423 print MAIL "\tcharset=\"UTF8\"\n";
424 print MAIL "Content-Transfer-Encoding: 8bit\n";
425 print MAIL "\n";
426
427 # text part
428
429 my $fill = ' '; # Avoid The Remove Extra Line Breaks Issue (MS Outlook)
430
431 print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
432 foreach my $task (@$tasklist) {
433 my $vmid = $task->{vmid};
434 if ($task->{state} eq 'ok') {
435
436 print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", $vmid,
437 $task->{state},
438 format_time($task->{backuptime}),
439 format_size ($task->{size}),
440 $task->{tarfile});
441 } else {
442 print MAIL sprintf ("${fill}%-10s %-6s %10s %8.2fMB %s\n", $vmid,
443 $task->{state},
444 format_time($task->{backuptime}),
445 0, '-');
446 }
447 }
448 print MAIL "${fill}\n";
449 print MAIL "${fill}Detailed backup logs:\n";
450 print MAIL "${fill}\n";
451 print MAIL "$fill$cmdline\n";
452 print MAIL "${fill}\n";
453
454 foreach my $task (@$tasklist) {
455 my $vmid = $task->{vmid};
456 my $log = $task->{tmplog};
457 if (!$log) {
458 print MAIL "${fill}$vmid: no log available\n\n";
459 next;
460 }
461 open (TMP, "$log");
462 while (my $line = <TMP>) { print MAIL encode8bit ("${fill}$vmid: $line"); }
463 close (TMP);
464 print MAIL "${fill}\n";
465 }
466
467 # end text part
468 print MAIL "\n--$boundary\n";
469
470 print MAIL "Content-Type: text/html;\n";
471 print MAIL "\tcharset=\"UTF8\"\n";
472 print MAIL "Content-Transfer-Encoding: 8bit\n";
473 print MAIL "\n";
474
475 # html part
476
477 print MAIL "<html><body>\n";
478
479 print MAIL "<table border=1 cellpadding=3>\n";
480
481 print MAIL "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
482
483 my $ssize = 0;
484
485 foreach my $task (@$tasklist) {
486 my $vmid = $task->{vmid};
487 my $name = $task->{hostname};
488
489 if ($task->{state} eq 'ok') {
490
491 $ssize += $task->{size};
492
493 print MAIL sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
494 $vmid, $name,
495 format_time($task->{backuptime}),
496 format_size ($task->{size}),
497 escape_html ($task->{tarfile}));
498 } else {
499 print MAIL sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
500
501 $vmid, $name, format_time($task->{backuptime}),
502 escape_html ($task->{msg}));
503 }
504 }
505
506 print MAIL sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
507 format_time ($totaltime), format_size ($ssize));
508
509 print MAIL "</table><br><br>\n";
510 print MAIL "Detailed backup logs:<br>\n";
511 print MAIL "<br>\n";
512 print MAIL "<pre>\n";
513 print MAIL escape_html($cmdline) . "\n";
514 print MAIL "\n";
515
516 foreach my $task (@$tasklist) {
517 my $vmid = $task->{vmid};
518 my $log = $task->{tmplog};
519 if (!$log) {
520 print MAIL "$vmid: no log available\n\n";
521 next;
522 }
523 open (TMP, "$log");
524 while (my $line = <TMP>) {
525 if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
526 print MAIL encode8bit ("$vmid: <font color=red>".
527 escape_html ($line) . "</font>");
528 } else {
529 print MAIL encode8bit ("$vmid: " . escape_html ($line));
530 }
531 }
532 close (TMP);
533 print MAIL "\n";
534 }
535 print MAIL "</pre>\n";
536
537 print MAIL "</body></html>\n";
538
539 # end html part
540 print MAIL "\n--$boundary--\n";
541
542};
543
544sub new {
545 my ($class, $cmdline, $opts) = @_;
546
547 mkpath $logdir;
548
549 check_bin ('cp');
550 check_bin ('df');
551 check_bin ('sendmail');
552 check_bin ('rsync');
553 check_bin ('tar');
554 check_bin ('mount');
555 check_bin ('umount');
556 check_bin ('cstream');
557 check_bin ('ionice');
558
559 if ($opts->{snapshot}) {
560 check_bin ('lvcreate');
561 check_bin ('lvs');
562 check_bin ('lvremove');
563 }
564
565 my $defaults = read_vzdump_defaults();
566
567 foreach my $k (keys %$defaults) {
568 if ($k eq 'dumpdir' || $k eq 'storage') {
569 $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
570 !defined ($opts->{storage});
571 } else {
572 $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k});
573 }
574 }
575
576 $opts->{mode} = 'stop' if $opts->{stop};
577 $opts->{mode} = 'suspend' if $opts->{suspend};
578 $opts->{mode} = 'snapshot' if $opts->{snapshot};
579
580 $opts->{dumpdir} =~ s|/+$|| if ($opts->{dumpdir});
581 $opts->{tmpdir} =~ s|/+$|| if ($opts->{tmpdir});
582
583 my $self = bless { cmdline => $cmdline, opts => $opts };
584
585 #always skip '.'
586 push @{$self->{findexcl}}, "'('", '-regex' , "'^\\.\$'", "')'", '-o';
587
588 $self->find_add_exclude ('-type', 's'); # skip sockets
589
590 if ($opts->{'exclude-path'}) {
591 foreach my $path (@{$opts->{'exclude-path'}}) {
592 $self->find_add_exclude ('-regex', $path);
593 }
594 }
595
596 if ($opts->{stdexcludes}) {
597 $self->find_add_exclude ('-files', '/var/log/.+');
598 $self->find_add_exclude ('-regex', '/tmp/.+');
599 $self->find_add_exclude ('-regex', '/var/tmp/.+');
600 $self->find_add_exclude ('-regex', '/var/run/.+pid');
601 }
602
603 foreach my $p (@plugins) {
604
605 my $pd = $p->new ($self);
606
607 push @{$self->{plugins}}, $pd;
608
609 if (!$opts->{dumpdir} && !$opts->{storage} &&
610 ($p eq 'PVE::VZDump::OpenVZ')) {
611 $opts->{dumpdir} = $pd->{dumpdir};
612 }
613 }
614
615 if (!$opts->{dumpdir} && !$opts->{storage}) {
616 die "no dumpdir/storage specified - use option '--dumpdir' or option '--storage'\n";
617 }
618
619 if ($opts->{storage}) {
620 my $info = storage_info ($opts->{storage});
621 $opts->{dumpdir} = $info->{dumpdir};
622 } elsif ($opts->{dumpdir}) {
623 die "dumpdir '$opts->{dumpdir}' does not exist\n"
624 if ! -d $opts->{dumpdir};
625 } else {
626 die "internal error";
627 }
628
629 if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
630 die "tmpdir '$opts->{tmpdir}' does not exist\n";
631 }
632
633 return $self;
634
635}
636
637sub get_lvm_mapping {
638
639 my $devmapper;
640
641 my $cmd = "lvs --units m --separator ':' --noheadings -o vg_name,lv_name,lv_size";
642 if (my $fd = IO::File->new ("$cmd 2>/dev/null|")) {
643 while (my $line = <$fd>) {
644 if ($line =~ m|^\s*(\S+):(\S+):(\d+(\.\d+))[Mm]$|) {
645 my $vg = $1;
646 my $lv = $2;
647 $devmapper->{"/dev/$vg/$lv"} = [$vg, $lv];
648 my $qlv = $lv;
649 $qlv =~ s/-/--/g;
650 my $qvg = $vg;
651 $qvg =~ s/-/--/g;
652 $devmapper->{"/dev/mapper/$qvg-$qlv"} = [$vg, $lv];
653 }
654 }
655 close ($fd);
656 }
657
658 return $devmapper;
659}
660
661sub get_mount_info {
662 my ($dir) = @_;
663
664 my $out;
665 if (my $fd = IO::File->new ("df -P -T '$dir' 2>/dev/null|")) {
666 <$fd>; #skip first line
667 $out = <$fd>;
668 close ($fd);
669 }
670
671 return undef if !$out;
672
673 my @res = split (/\s+/, $out);
674
675 return undef if scalar (@res) != 7;
676
677 return {
678 device => $res[0],
679 fstype => $res[1],
680 mountpoint => $res[6]
681 };
682}
683
684sub get_lvm_device {
685 my ($dir, $mapping) = @_;
686
687 my $info = get_mount_info ($dir);
688
689 return undef if !$info;
690
691 my $dev = $info->{device};
692
693 my ($vg, $lv);
694
695 ($vg, $lv) = @{$mapping->{$dev}} if defined $mapping->{$dev};
696
697 return wantarray ? ($dev, $info->{mountpoint}, $vg, $lv, $info->{fstype}) : $dev;
698}
699
700sub getlock {
701 my ($self) = @_;
702
703 my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
704
705 if (!open (SERVER_FLCK, ">>$lockfile")) {
706 debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1);
707 exit (-1);
708 }
709
710 if (flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
711 return;
712 }
713
714 if (!$maxwait) {
715 debugmsg ('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
716 exit (-1);
717 }
718
719 debugmsg('info', "trying to get global lock - waiting...", undef, 1);
720
721 eval {
722 alarm ($maxwait * 60);
723
724 local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
725
726 if (!flock (SERVER_FLCK, LOCK_EX)) {
727 my $err = $!;
728 close (SERVER_FLCK);
729 alarm (0);
730 die "$err\n";
731 }
732 alarm (0);
733 };
734 alarm (0);
735
736 my $err = $@;
737
738 if ($err) {
739 debugmsg ('err', "can't aquire lock '$lockfile' - $err", undef, 1);
740 exit (-1);
741 }
742
743 debugmsg('info', "got global lock", undef, 1);
744}
745
746sub run_hook_script {
747 my ($self, $phase, $task, $logfd) = @_;
748
749 my $opts = $self->{opts};
750
751 my $script = $opts->{script};
752
753 return if !$script;
754
755 my $cmd = "$script $phase";
756
757 $cmd .= " $task->{mode} $task->{vmid}" if ($task);
758
759 local %ENV;
760
761 foreach my $ek (qw(vmtype dumpdir hostname tarfile logfile)) {
762 $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
763 }
764
765 run_command ($logfd, $cmd);
766}
767
768sub exec_backup_task {
769 my ($self, $task) = @_;
770
771 my $opts = $self->{opts};
772
773 my $vmid = $task->{vmid};
774 my $plugin = $task->{plugin};
775
776 my $vmstarttime = time ();
777
778 my $logfd;
779
780 my $cleanup = {};
781
782 my $vmstoptime = 0;
783
784 eval {
785 die "unable to find VM '$vmid'\n" if !$plugin;
786
787 my $vmtype = $plugin->type();
788
789 my $tmplog = "$logdir/$vmtype-$vmid.log";
790
791 my $lt = localtime();
792
793 my $bkname = "vzdump-$vmtype-$vmid";
794 my $basename = sprintf "${bkname}-%04d_%02d_%02d-%02d_%02d_%02d",
795 $lt->year + 1900, $lt->mon + 1, $lt->mday,
796 $lt->hour, $lt->min, $lt->sec;
797
798 my $logfile = $task->{logfile} = "$opts->{dumpdir}/$basename.log";
799
800 my $ext = $opts->{compress} ? '.tgz' : '.tar';
801
802 if ($opts->{stdout}) {
803 $task->{tarfile} = '-';
804 } else {
805 my $tarfile = $task->{tarfile} = "$opts->{dumpdir}/$basename$ext";
806 $task->{tmptar} = $task->{tarfile};
807 $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
808 unlink $task->{tmptar};
809 }
810
811 $task->{vmtype} = $vmtype;
812
813 if ($opts->{tmpdir}) {
814 $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$";
815 } else {
816 # dumpdir is posix? then use it as temporary dir
817 my $info = get_mount_info ($opts->{dumpdir});
818 if ($vmtype eq 'qemu' ||
819 grep ($_ eq $info->{fstype}, @posix_filesystems)) {
820 $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp";
821 } else {
822 $task->{tmpdir} = "/var/tmp/vzdumptmp$$";
823 debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" .
824 "using $task->{tmpdir} for temporary files", $logfd);
825 }
826 }
827
828 rmtree $task->{tmpdir};
829 mkdir $task->{tmpdir};
830 -d $task->{tmpdir} ||
831 die "unable to create temporary directory '$task->{tmpdir}'";
832
833 $logfd = IO::File->new (">$tmplog") ||
834 die "unable to create log file '$tmplog'";
835
836 $task->{dumpdir} = $opts->{dumpdir};
837
838 $task->{tmplog} = $tmplog;
839
840 unlink $logfile;
841
842 debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
843
844 $plugin->set_logfd ($logfd);
845
846 # test is VM is running
847 my ($running, $status_text) = $plugin->vm_status ($vmid);
848
849 debugmsg ('info', "status = ${status_text}", $logfd);
850
851 # lock VM (prevent config changes)
852 $plugin->lock_vm ($vmid);
853
854 $cleanup->{unlock} = 1;
855
856 # prepare
857
858 my $mode = $running ? $opts->{mode} : 'stop';
859
860 if ($mode eq 'snapshot') {
861 my %saved_task = %$task;
862 eval { $plugin->prepare ($task, $vmid, $mode); };
863 if (my $err = $@) {
864 die $err if $err !~ m/^mode failure/;
865 debugmsg ('info', $err, $logfd);
866 debugmsg ('info', "trying 'suspend' mode instead", $logfd);
867 $mode = 'suspend'; # so prepare is called again below
868 %$task = %saved_task;
869 }
870 }
871
872 $task->{mode} = $mode;
873
874 debugmsg ('info', "backup mode: $mode", $logfd);
875
876 debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd)
877 if $opts->{bwlimit};
878
879 debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd);
880
881 if ($mode eq 'stop') {
882
883 $plugin->prepare ($task, $vmid, $mode);
884
885 $self->run_hook_script ('backup-start', $task, $logfd);
886
887 if ($running) {
888 debugmsg ('info', "stopping vm", $logfd);
889 $vmstoptime = time ();
890 $self->run_hook_script ('pre-stop', $task, $logfd);
891 $plugin->stop_vm ($task, $vmid);
892 $cleanup->{restart} = 1;
893 }
894
895
896 } elsif ($mode eq 'suspend') {
897
898 $plugin->prepare ($task, $vmid, $mode);
899
900 $self->run_hook_script ('backup-start', $task, $logfd);
901
902 if ($vmtype eq 'openvz') {
903 # pre-suspend rsync
904 $plugin->copy_data_phase1 ($task, $vmid);
905 }
906
907 debugmsg ('info', "suspend vm", $logfd);
908 $vmstoptime = time ();
909 $self->run_hook_script ('pre-stop', $task, $logfd);
910 $plugin->suspend_vm ($task, $vmid);
911 $cleanup->{resume} = 1;
912
913 if ($vmtype eq 'openvz') {
914 # post-suspend rsync
915 $plugin->copy_data_phase2 ($task, $vmid);
916
917 debugmsg ('info', "resume vm", $logfd);
918 $cleanup->{resume} = 0;
919 $self->run_hook_script ('pre-restart', $task, $logfd);
920 $plugin->resume_vm ($task, $vmid);
921 my $delay = time () - $vmstoptime;
922 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
923 }
924
925 } elsif ($mode eq 'snapshot') {
926
927 my $snapshot_count = $task->{snapshot_count} || 0;
928
929 $self->run_hook_script ('pre-stop', $task, $logfd);
930
931 if ($snapshot_count > 1) {
932 debugmsg ('info', "suspend vm to make snapshot", $logfd);
933 $vmstoptime = time ();
934 $plugin->suspend_vm ($task, $vmid);
935 $cleanup->{resume} = 1;
936 }
937
938 $plugin->snapshot ($task, $vmid);
939
940 $self->run_hook_script ('pre-restart', $task, $logfd);
941
942 if ($snapshot_count > 1) {
943 debugmsg ('info', "resume vm", $logfd);
944 $cleanup->{resume} = 0;
945 $plugin->resume_vm ($task, $vmid);
946 my $delay = time () - $vmstoptime;
947 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
948 }
949
950 } else {
951 die "internal error - unknown mode '$mode'\n";
952 }
953
954 # assemble archive image
955 $plugin->assemble ($task, $vmid);
956
957 # produce archive
958
959 if ($opts->{stdout}) {
960 debugmsg ('info', "sending archive to stdout", $logfd);
961 $plugin->archive ($task, $vmid, $task->{tmptar});
962 $self->run_hook_script ('backup-end', $task, $logfd);
963 return;
964 }
965
966 debugmsg ('info', "creating archive '$task->{tarfile}'", $logfd);
967 $plugin->archive ($task, $vmid, $task->{tmptar});
968
969 rename ($task->{tmptar}, $task->{tarfile}) ||
970 die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\n";
971
972 # determine size
973 $task->{size} = (-s $task->{tarfile}) || 0;
974 my $cs = format_size ($task->{size});
975 debugmsg ('info', "archive file size: $cs", $logfd);
976
977 # purge older backup
978
979 my $maxfiles = $opts->{maxfiles};
980
981 if ($maxfiles) {
982 my @bklist = ();
983 my $dir = $opts->{dumpdir};
984 foreach my $fn (<$dir/${bkname}-*>) {
985 next if $fn eq $task->{tarfile};
986 if ($fn =~ m!/${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|tar)$!) {
987 my $t = timelocal ($6, $5, $4, $3, $2 - 1, $1 - 1900);
988 push @bklist, [$fn, $t];
989 }
990 }
991
992 @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
993
994 my $ind = scalar (@bklist);
995
996 while (scalar (@bklist) >= $maxfiles) {
997 my $d = pop @bklist;
998 debugmsg ('info', "delete old backup '$d->[0]'", $logfd);
999 unlink $d->[0];
1000 my $logfn = $d->[0];
1001 $logfn =~ s/\.(tgz|tar)$/\.log/;
1002 unlink $logfn;
1003 }
1004 }
1005
1006 $self->run_hook_script ('backup-end', $task, $logfd);
1007 };
1008 my $err = $@;
1009
1010 if ($plugin) {
1011 # clean-up
1012
1013 if ($cleanup->{unlock}) {
1014 eval { $plugin->unlock_vm ($vmid); };
1015 warn $@ if $@;
1016 }
1017
1018 eval { $plugin->cleanup ($task, $vmid) };
1019 warn $@ if $@;
1020
1021 eval { $plugin->set_logfd (undef); };
1022 warn $@ if $@;
1023
1024 if ($cleanup->{resume} || $cleanup->{restart}) {
1025 eval {
1026 $self->run_hook_script ('pre-restart', $task, $logfd);
1027 if ($cleanup->{resume}) {
1028 debugmsg ('info', "resume vm", $logfd);
1029 $plugin->resume_vm ($task, $vmid);
1030 } else {
1031 debugmsg ('info', "restarting vm", $logfd);
1032 $plugin->start_vm ($task, $vmid);
1033 }
1034 };
1035 my $err = $@;
1036 if ($err) {
1037 warn $err;
1038 } else {
1039 my $delay = time () - $vmstoptime;
1040 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
1041 }
1042 }
1043 }
1044
1045 eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; };
1046 warn $@ if $@;
1047
1048 eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; };
1049 warn $@ if $@;
1050
1051 my $delay = $task->{backuptime} = time () - $vmstarttime;
1052
1053 if ($err) {
1054 $task->{state} = 'err';
1055 $task->{msg} = $err;
1056 debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1);
1057
1058 eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
1059
1060 } else {
1061 $task->{state} = 'ok';
1062 my $tstr = format_time ($delay);
1063 debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
1064 }
1065
1066 close ($logfd) if $logfd;
1067
1068 if ($task->{tmplog} && $task->{logfile}) {
1069 system ("cp '$task->{tmplog}' '$task->{logfile}'");
1070 }
1071
1072 eval { $self->run_hook_script ('log-end', $task); };
1073
1074 die $err if $err && $err =~ m/^interrupted by signal$/;
1075}
1076
1077sub exec_backup {
1078 my ($self) = @_;
1079
1080 my $opts = $self->{opts};
1081
1082 debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1);
1083
1084 my $tasklist = [];
1085
1086 if ($opts->{all}) {
1087 foreach my $plugin (@{$self->{plugins}}) {
1088 my $vmlist = $plugin->vmlist();
1089 foreach my $vmid (sort @$vmlist) {
1090 next if grep { $_ eq $vmid } @{$opts->{exclude}};
1091 push @$tasklist, { vmid => $vmid, state => 'todo', plugin => $plugin };
1092 }
1093 }
1094 } else {
1095 foreach my $vmid (sort @{$opts->{vmids}}) {
1096 my $plugin;
1097 foreach my $pg (@{$self->{plugins}}) {
1098 my $vmlist = $pg->vmlist();
1099 if (grep { $_ eq $vmid } @$vmlist) {
1100 $plugin = $pg;
1101 last;
1102 }
1103 }
1104 push @$tasklist, { vmid => $vmid, state => 'todo', plugin => $plugin };
1105 }
1106 }
1107
1108 my $starttime = time();
1109 my $errcount = 0;
1110 eval {
1111
1112 $self->run_hook_script ('job-start');
1113
1114 foreach my $task (@$tasklist) {
1115 $self->exec_backup_task ($task);
1116 $errcount += 1 if $task->{state} ne 'ok';
1117 }
1118
1119 $self->run_hook_script ('job-end');
1120 };
1121 my $err = $@;
1122
1123 $self->run_hook_script ('job-abort') if $err;
1124
1125 if ($err) {
1126 debugmsg ('err', "Backup job failed - $err", undef, 1);
1127 } else {
1128 if ($errcount) {
1129 debugmsg ('info', "Backup job finished with errors", undef, 1);
1130 } else {
1131 debugmsg ('info', "Backup job finished successfuly", undef, 1);
1132 }
1133 }
1134
1135 my $totaltime = time() - $starttime;
1136
1137 eval { $self->$sendmail ($tasklist, $totaltime); };
1138 debugmsg ('err', $@) if $@;
1139}
1140
11411;