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