]> git.proxmox.com Git - pve-manager.git/blob - PVE/VZDump.pm
339bdbd5f12daf63294e78b1c49b7964750cdda2
[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'));
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 "FROM: vzdump backup tool <root>\n";
291 print MAIL "TO: $rcvrtxt\n";
292 print MAIL "SUBJECT: vzdump backup status ($hostname) : $stat\n";
293 print MAIL "\n";
294 print MAIL "This is a multi-part message in MIME format.\n\n";
295 print MAIL "--$boundary\n";
296
297 print MAIL "Content-Type: text/plain;\n";
298 print MAIL "\tcharset=\"UTF8\"\n";
299 print MAIL "Content-Transfer-Encoding: 8bit\n";
300 print MAIL "\n";
301
302 # text part
303
304 my $fill = ' '; # Avoid The Remove Extra Line Breaks Issue (MS Outlook)
305
306 print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
307 foreach my $task (@$tasklist) {
308 my $vmid = $task->{vmid};
309 if ($task->{state} eq 'ok') {
310
311 print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", $vmid,
312 $task->{state},
313 format_time($task->{backuptime}),
314 format_size ($task->{size}),
315 $task->{tarfile});
316 } else {
317 print MAIL sprintf ("${fill}%-10s %-6s %10s %8.2fMB %s\n", $vmid,
318 $task->{state},
319 format_time($task->{backuptime}),
320 0, '-');
321 }
322 }
323 print MAIL "${fill}\n";
324 print MAIL "${fill}Detailed backup logs:\n";
325 print MAIL "${fill}\n";
326 print MAIL "$fill$cmdline\n";
327 print MAIL "${fill}\n";
328
329 foreach my $task (@$tasklist) {
330 my $vmid = $task->{vmid};
331 my $log = $task->{tmplog};
332 if (!$log) {
333 print MAIL "${fill}$vmid: no log available\n\n";
334 next;
335 }
336 open (TMP, "$log");
337 while (my $line = <TMP>) { print MAIL encode8bit ("${fill}$vmid: $line"); }
338 close (TMP);
339 print MAIL "${fill}\n";
340 }
341
342 # end text part
343 print MAIL "\n--$boundary\n";
344
345 print MAIL "Content-Type: text/html;\n";
346 print MAIL "\tcharset=\"UTF8\"\n";
347 print MAIL "Content-Transfer-Encoding: 8bit\n";
348 print MAIL "\n";
349
350 # html part
351
352 print MAIL "<html><body>\n";
353
354 print MAIL "<table border=1 cellpadding=3>\n";
355
356 print MAIL "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
357
358 my $ssize = 0;
359
360 foreach my $task (@$tasklist) {
361 my $vmid = $task->{vmid};
362 my $name = $task->{hostname};
363
364 if ($task->{state} eq 'ok') {
365
366 $ssize += $task->{size};
367
368 print MAIL sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
369 $vmid, $name,
370 format_time($task->{backuptime}),
371 format_size ($task->{size}),
372 escape_html ($task->{tarfile}));
373 } else {
374 print MAIL sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
375
376 $vmid, $name, format_time($task->{backuptime}),
377 escape_html ($task->{msg}));
378 }
379 }
380
381 print MAIL sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
382 format_time ($totaltime), format_size ($ssize));
383
384 print MAIL "</table><br><br>\n";
385 print MAIL "Detailed backup logs:<br>\n";
386 print MAIL "<br>\n";
387 print MAIL "<pre>\n";
388 print MAIL escape_html($cmdline) . "\n";
389 print MAIL "\n";
390
391 foreach my $task (@$tasklist) {
392 my $vmid = $task->{vmid};
393 my $log = $task->{tmplog};
394 if (!$log) {
395 print MAIL "$vmid: no log available\n\n";
396 next;
397 }
398 open (TMP, "$log");
399 while (my $line = <TMP>) {
400 if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
401 print MAIL encode8bit ("$vmid: <font color=red>".
402 escape_html ($line) . "</font>");
403 } else {
404 print MAIL encode8bit ("$vmid: " . escape_html ($line));
405 }
406 }
407 close (TMP);
408 print MAIL "\n";
409 }
410 print MAIL "</pre>\n";
411
412 print MAIL "</body></html>\n";
413
414 # end html part
415 print MAIL "\n--$boundary--\n";
416
417 close(MAIL);
418 };
419
420 sub new {
421 my ($class, $cmdline, $opts, $skiplist) = @_;
422
423 mkpath $logdir;
424
425 check_bin ('cp');
426 check_bin ('df');
427 check_bin ('sendmail');
428 check_bin ('rsync');
429 check_bin ('tar');
430 check_bin ('mount');
431 check_bin ('umount');
432 check_bin ('cstream');
433 check_bin ('ionice');
434
435 if ($opts->{mode} && $opts->{mode} eq 'snapshot') {
436 check_bin ('lvcreate');
437 check_bin ('lvs');
438 check_bin ('lvremove');
439 }
440
441 my $defaults = read_vzdump_defaults();
442
443 my $maxfiles = $opts->{maxfiles}; # save here, because we overwrite with default
444
445 $opts->{remove} = 1 if !defined($opts->{remove});
446
447 foreach my $k (keys %$defaults) {
448 if ($k eq 'dumpdir' || $k eq 'storage') {
449 $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
450 !defined ($opts->{storage});
451 } else {
452 $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k});
453 }
454 }
455
456 $opts->{dumpdir} =~ s|/+$|| if ($opts->{dumpdir});
457 $opts->{tmpdir} =~ s|/+$|| if ($opts->{tmpdir});
458
459 $skiplist = [] if !$skiplist;
460 my $self = bless { cmdline => $cmdline, opts => $opts, skiplist => $skiplist };
461
462 #always skip '.'
463 push @{$self->{findexcl}}, "'('", '-regex' , "'^\\.\$'", "')'", '-o';
464
465 $self->find_add_exclude ('-type', 's'); # skip sockets
466
467 if ($defaults->{'exclude-path'}) {
468 foreach my $path (@{$defaults->{'exclude-path'}}) {
469 $self->find_add_exclude ('-regex', $path);
470 }
471 }
472
473 if ($opts->{'exclude-path'}) {
474 foreach my $path (@{$opts->{'exclude-path'}}) {
475 $self->find_add_exclude ('-regex', $path);
476 }
477 }
478
479 if ($opts->{stdexcludes}) {
480 $self->find_add_exclude ('-files', '/var/log/.+');
481 $self->find_add_exclude ('-regex', '/tmp/.+');
482 $self->find_add_exclude ('-regex', '/var/tmp/.+');
483 $self->find_add_exclude ('-regex', '/var/run/.+pid');
484 }
485
486 foreach my $p (@plugins) {
487
488 my $pd = $p->new ($self);
489
490 push @{$self->{plugins}}, $pd;
491 }
492
493 if (!$opts->{dumpdir} && !$opts->{storage}) {
494 $opts->{storage} = 'local';
495 }
496
497 if ($opts->{storage}) {
498 my $info = storage_info ($opts->{storage});
499 $opts->{dumpdir} = $info->{dumpdir};
500 $maxfiles = $info->{maxfiles} if !defined($maxfiles) && defined($info->{maxfiles});
501 } elsif ($opts->{dumpdir}) {
502 die "dumpdir '$opts->{dumpdir}' does not exist\n"
503 if ! -d $opts->{dumpdir};
504 } else {
505 die "internal error";
506 }
507
508 if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
509 die "tmpdir '$opts->{tmpdir}' does not exist\n";
510 }
511
512 $opts->{maxfiles} = $maxfiles if defined($maxfiles);
513
514 return $self;
515
516 }
517
518 sub get_lvm_mapping {
519
520 my $devmapper;
521
522 my $cmd = ['lvs', '--units', 'm', '--separator', ':', '--noheadings',
523 '-o', 'vg_name,lv_name,lv_size' ];
524
525 my $parser = sub {
526 my $line = shift;
527 if ($line =~ m|^\s*(\S+):(\S+):(\d+(\.\d+))[Mm]$|) {
528 my $vg = $1;
529 my $lv = $2;
530 $devmapper->{"/dev/$vg/$lv"} = [$vg, $lv];
531 my $qlv = $lv;
532 $qlv =~ s/-/--/g;
533 my $qvg = $vg;
534 $qvg =~ s/-/--/g;
535 $devmapper->{"/dev/mapper/$qvg-$qlv"} = [$vg, $lv];
536 }
537 };
538
539 eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); };
540 warn $@ if $@;
541
542 return $devmapper;
543 }
544
545 sub get_mount_info {
546 my ($dir) = @_;
547
548 # Note: df 'available' can be negative, and percentage set to '-'
549
550 my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir];
551
552 my $res;
553
554 my $parser = sub {
555 my $line = shift;
556 if (my ($fsid, $fstype, undef, $mp) = $line =~
557 m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) {
558 $res = {
559 device => $fsid,
560 fstype => $fstype,
561 mountpoint => $mp,
562 };
563 }
564 };
565
566 eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); };
567 warn $@ if $@;
568
569 return $res;
570 }
571
572 sub get_lvm_device {
573 my ($dir, $mapping) = @_;
574
575 my $info = get_mount_info($dir);
576
577 return undef if !$info;
578
579 my $dev = $info->{device};
580
581 my ($vg, $lv);
582
583 ($vg, $lv) = @{$mapping->{$dev}} if defined $mapping->{$dev};
584
585 return wantarray ? ($dev, $info->{mountpoint}, $vg, $lv, $info->{fstype}) : $dev;
586 }
587
588 sub getlock {
589 my ($self) = @_;
590
591 my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
592
593 if (!open (SERVER_FLCK, ">>$lockfile")) {
594 debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1);
595 exit (-1);
596 }
597
598 if (flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
599 return;
600 }
601
602 if (!$maxwait) {
603 debugmsg ('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
604 exit (-1);
605 }
606
607 debugmsg('info', "trying to get global lock - waiting...", undef, 1);
608
609 eval {
610 alarm ($maxwait * 60);
611
612 local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
613
614 if (!flock (SERVER_FLCK, LOCK_EX)) {
615 my $err = $!;
616 close (SERVER_FLCK);
617 alarm (0);
618 die "$err\n";
619 }
620 alarm (0);
621 };
622 alarm (0);
623
624 my $err = $@;
625
626 if ($err) {
627 debugmsg ('err', "can't aquire lock '$lockfile' - $err", undef, 1);
628 exit (-1);
629 }
630
631 debugmsg('info', "got global lock", undef, 1);
632 }
633
634 sub run_hook_script {
635 my ($self, $phase, $task, $logfd) = @_;
636
637 my $opts = $self->{opts};
638
639 my $script = $opts->{script};
640
641 return if !$script;
642
643 my $cmd = "$script $phase";
644
645 $cmd .= " $task->{mode} $task->{vmid}" if ($task);
646
647 local %ENV;
648
649 # set immutable opts directly (so they are available in all phases)
650 $ENV{STOREID} = $opts->{storage} if $opts->{storage};
651 $ENV{DUMPDIR} = $opts->{dumpdir} if $opts->{dumpdir};
652
653 foreach my $ek (qw(vmtype hostname tarfile logfile)) {
654 $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
655 }
656
657 run_command ($logfd, $cmd);
658 }
659
660 sub compressor_info {
661 my ($opt_compress) = @_;
662
663 if (!$opt_compress || $opt_compress eq '0') {
664 return undef;
665 } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
666 return ('lzop', 'lzo');
667 } elsif ($opt_compress eq 'gzip') {
668 return ('gzip', 'gz');
669 } else {
670 die "internal error - unknown compression option '$opt_compress'";
671 }
672 }
673
674 sub get_backup_file_list {
675 my ($dir, $bkname, $exclude_fn) = @_;
676
677 my $bklist = [];
678 foreach my $fn (<$dir/${bkname}-*>) {
679 next if $exclude_fn && $fn eq $exclude_fn;
680 if ($fn =~ m!/(${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?)))$!) {
681 $fn = "$dir/$1"; # untaint
682 my $t = timelocal ($7, $6, $5, $4, $3 - 1, $2 - 1900);
683 push @$bklist, [$fn, $t];
684 }
685 }
686
687 return $bklist;
688 }
689
690 sub exec_backup_task {
691 my ($self, $task) = @_;
692
693 my $opts = $self->{opts};
694
695 my $vmid = $task->{vmid};
696 my $plugin = $task->{plugin};
697
698 my $vmstarttime = time ();
699
700 my $logfd;
701
702 my $cleanup = {};
703
704 my $vmstoptime = 0;
705
706 eval {
707 die "unable to find VM '$vmid'\n" if !$plugin;
708
709 my $vmtype = $plugin->type();
710
711 my $tmplog = "$logdir/$vmtype-$vmid.log";
712
713 my $lt = localtime();
714
715 my $bkname = "vzdump-$vmtype-$vmid";
716 my $basename = sprintf "${bkname}-%04d_%02d_%02d-%02d_%02d_%02d",
717 $lt->year + 1900, $lt->mon + 1, $lt->mday,
718 $lt->hour, $lt->min, $lt->sec;
719
720 my $maxfiles = $opts->{maxfiles};
721
722 if ($maxfiles && !$opts->{remove}) {
723 my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname);
724 die "only $maxfiles backup(s) allowed - please consider to remove old backup files.\n"
725 if scalar(@$bklist) >= $maxfiles;
726 }
727
728 my $logfile = $task->{logfile} = "$opts->{dumpdir}/$basename.log";
729
730 my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar';
731 my ($comp, $comp_ext) = compressor_info($opts->{compress});
732 if ($comp && $comp_ext) {
733 $ext .= ".${comp_ext}";
734 }
735
736 if ($opts->{stdout}) {
737 $task->{tarfile} = '-';
738 } else {
739 my $tarfile = $task->{tarfile} = "$opts->{dumpdir}/$basename$ext";
740 $task->{tmptar} = $task->{tarfile};
741 $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
742 unlink $task->{tmptar};
743 }
744
745 $task->{vmtype} = $vmtype;
746
747 if ($opts->{tmpdir}) {
748 $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$";
749 } else {
750 # dumpdir is posix? then use it as temporary dir
751 my $info = get_mount_info($opts->{dumpdir});
752 if ($vmtype eq 'qemu' ||
753 grep ($_ eq $info->{fstype}, @posix_filesystems)) {
754 $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp";
755 } else {
756 $task->{tmpdir} = "/var/tmp/vzdumptmp$$";
757 debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" .
758 "using $task->{tmpdir} for temporary files", $logfd);
759 }
760 }
761
762 rmtree $task->{tmpdir};
763 mkdir $task->{tmpdir};
764 -d $task->{tmpdir} ||
765 die "unable to create temporary directory '$task->{tmpdir}'";
766
767 $logfd = IO::File->new (">$tmplog") ||
768 die "unable to create log file '$tmplog'";
769
770 $task->{dumpdir} = $opts->{dumpdir};
771 $task->{storeid} = $opts->{storage};
772 $task->{tmplog} = $tmplog;
773
774 unlink $logfile;
775
776 debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
777
778 $plugin->set_logfd ($logfd);
779
780 # test is VM is running
781 my ($running, $status_text) = $plugin->vm_status ($vmid);
782
783 debugmsg ('info', "status = ${status_text}", $logfd);
784
785 # lock VM (prevent config changes)
786 $plugin->lock_vm ($vmid);
787
788 $cleanup->{unlock} = 1;
789
790 # prepare
791
792 my $mode = $running ? $opts->{mode} : 'stop';
793
794 if ($mode eq 'snapshot') {
795 my %saved_task = %$task;
796 eval { $plugin->prepare ($task, $vmid, $mode); };
797 if (my $err = $@) {
798 die $err if $err !~ m/^mode failure/;
799 debugmsg ('info', $err, $logfd);
800 debugmsg ('info', "trying 'suspend' mode instead", $logfd);
801 $mode = 'suspend'; # so prepare is called again below
802 %$task = %saved_task;
803 }
804 }
805
806 $task->{mode} = $mode;
807
808 debugmsg ('info', "backup mode: $mode", $logfd);
809
810 debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd)
811 if $opts->{bwlimit};
812
813 debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd);
814
815 if ($mode eq 'stop') {
816
817 $plugin->prepare ($task, $vmid, $mode);
818
819 $self->run_hook_script ('backup-start', $task, $logfd);
820
821 if ($running) {
822 debugmsg ('info', "stopping vm", $logfd);
823 $vmstoptime = time ();
824 $self->run_hook_script ('pre-stop', $task, $logfd);
825 $plugin->stop_vm ($task, $vmid);
826 $cleanup->{restart} = 1;
827 }
828
829
830 } elsif ($mode eq 'suspend') {
831
832 $plugin->prepare ($task, $vmid, $mode);
833
834 $self->run_hook_script ('backup-start', $task, $logfd);
835
836 if ($vmtype eq 'openvz') {
837 # pre-suspend rsync
838 $plugin->copy_data_phase1 ($task, $vmid);
839 }
840
841 debugmsg ('info', "suspend vm", $logfd);
842 $vmstoptime = time ();
843 $self->run_hook_script ('pre-stop', $task, $logfd);
844 $plugin->suspend_vm ($task, $vmid);
845 $cleanup->{resume} = 1;
846
847 if ($vmtype eq 'openvz') {
848 # post-suspend rsync
849 $plugin->copy_data_phase2 ($task, $vmid);
850
851 debugmsg ('info', "resume vm", $logfd);
852 $cleanup->{resume} = 0;
853 $self->run_hook_script ('pre-restart', $task, $logfd);
854 $plugin->resume_vm ($task, $vmid);
855 my $delay = time () - $vmstoptime;
856 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
857 }
858
859 } elsif ($mode eq 'snapshot') {
860
861 $self->run_hook_script ('backup-start', $task, $logfd);
862
863 my $snapshot_count = $task->{snapshot_count} || 0;
864
865 $self->run_hook_script ('pre-stop', $task, $logfd);
866
867 if ($snapshot_count > 1) {
868 debugmsg ('info', "suspend vm to make snapshot", $logfd);
869 $vmstoptime = time ();
870 $plugin->suspend_vm ($task, $vmid);
871 $cleanup->{resume} = 1;
872 }
873
874 $plugin->snapshot ($task, $vmid);
875
876 $self->run_hook_script ('pre-restart', $task, $logfd);
877
878 if ($snapshot_count > 1) {
879 debugmsg ('info', "resume vm", $logfd);
880 $cleanup->{resume} = 0;
881 $plugin->resume_vm ($task, $vmid);
882 my $delay = time () - $vmstoptime;
883 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
884 }
885
886 } else {
887 die "internal error - unknown mode '$mode'\n";
888 }
889
890 # assemble archive image
891 $plugin->assemble ($task, $vmid);
892
893 # produce archive
894
895 if ($opts->{stdout}) {
896 debugmsg ('info', "sending archive to stdout", $logfd);
897 $plugin->archive($task, $vmid, $task->{tmptar}, $comp);
898 $self->run_hook_script ('backup-end', $task, $logfd);
899 return;
900 }
901
902 debugmsg ('info', "creating archive '$task->{tarfile}'", $logfd);
903 $plugin->archive($task, $vmid, $task->{tmptar}, $comp);
904
905 rename ($task->{tmptar}, $task->{tarfile}) ||
906 die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\n";
907
908 # determine size
909 $task->{size} = (-s $task->{tarfile}) || 0;
910 my $cs = format_size ($task->{size});
911 debugmsg ('info', "archive file size: $cs", $logfd);
912
913 # purge older backup
914
915 if ($maxfiles && $opts->{remove}) {
916 my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{tarfile});
917 $bklist = [ sort { $b->[1] <=> $a->[1] } @$bklist ];
918
919 while (scalar (@$bklist) >= $maxfiles) {
920 my $d = pop @$bklist;
921 debugmsg ('info', "delete old backup '$d->[0]'", $logfd);
922 unlink $d->[0];
923 my $logfn = $d->[0];
924 $logfn =~ s/\.(tgz|((tar|vma)(\.(gz|lzo))?))$/\.log/;
925 unlink $logfn;
926 }
927 }
928
929 $self->run_hook_script ('backup-end', $task, $logfd);
930 };
931 my $err = $@;
932
933 if ($plugin) {
934 # clean-up
935
936 if ($cleanup->{unlock}) {
937 eval { $plugin->unlock_vm ($vmid); };
938 warn $@ if $@;
939 }
940
941 eval { $plugin->cleanup ($task, $vmid) };
942 warn $@ if $@;
943
944 eval { $plugin->set_logfd (undef); };
945 warn $@ if $@;
946
947 if ($cleanup->{resume} || $cleanup->{restart}) {
948 eval {
949 $self->run_hook_script ('pre-restart', $task, $logfd);
950 if ($cleanup->{resume}) {
951 debugmsg ('info', "resume vm", $logfd);
952 $plugin->resume_vm ($task, $vmid);
953 } else {
954 my $running = $plugin->vm_status($vmid);
955 if (!$running) {
956 debugmsg ('info', "restarting vm", $logfd);
957 $plugin->start_vm ($task, $vmid);
958 }
959 }
960 };
961 my $err = $@;
962 if ($err) {
963 warn $err;
964 } else {
965 my $delay = time () - $vmstoptime;
966 debugmsg ('info', "vm is online again after $delay seconds", $logfd);
967 }
968 }
969 }
970
971 eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; };
972 warn $@ if $@;
973
974 eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; };
975 warn $@ if $@;
976
977 my $delay = $task->{backuptime} = time () - $vmstarttime;
978
979 if ($err) {
980 $task->{state} = 'err';
981 $task->{msg} = $err;
982 debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1);
983
984 eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
985
986 } else {
987 $task->{state} = 'ok';
988 my $tstr = format_time ($delay);
989 debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
990 }
991
992 close ($logfd) if $logfd;
993
994 if ($task->{tmplog} && $task->{logfile}) {
995 system ("cp '$task->{tmplog}' '$task->{logfile}'");
996 }
997
998 eval { $self->run_hook_script ('log-end', $task); };
999
1000 die $err if $err && $err =~ m/^interrupted by signal$/;
1001 }
1002
1003 sub exec_backup {
1004 my ($self, $rpcenv, $authuser) = @_;
1005
1006 my $opts = $self->{opts};
1007
1008 debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1);
1009 debugmsg ('info', "skip external VMs: " . join(', ', @{$self->{skiplist}}))
1010 if scalar(@{$self->{skiplist}});
1011
1012 my $tasklist = [];
1013
1014 if ($opts->{all}) {
1015 foreach my $plugin (@{$self->{plugins}}) {
1016 my $vmlist = $plugin->vmlist();
1017 foreach my $vmid (sort @$vmlist) {
1018 next if grep { $_ eq $vmid } @{$opts->{exclude}};
1019 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], 1);
1020 push @$tasklist, { vmid => $vmid, state => 'todo', plugin => $plugin };
1021 }
1022 }
1023 } else {
1024 foreach my $vmid (sort @{$opts->{vmids}}) {
1025 my $plugin;
1026 foreach my $pg (@{$self->{plugins}}) {
1027 my $vmlist = $pg->vmlist();
1028 if (grep { $_ eq $vmid } @$vmlist) {
1029 $plugin = $pg;
1030 last;
1031 }
1032 }
1033 $rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ]);
1034 push @$tasklist, { vmid => $vmid, state => 'todo', plugin => $plugin };
1035 }
1036 }
1037
1038 my $starttime = time();
1039 my $errcount = 0;
1040 eval {
1041
1042 $self->run_hook_script ('job-start');
1043
1044 foreach my $task (@$tasklist) {
1045 $self->exec_backup_task ($task);
1046 $errcount += 1 if $task->{state} ne 'ok';
1047 }
1048
1049 $self->run_hook_script ('job-end');
1050 };
1051 my $err = $@;
1052
1053 $self->run_hook_script ('job-abort') if $err;
1054
1055 if ($err) {
1056 debugmsg ('err', "Backup job failed - $err", undef, 1);
1057 } else {
1058 if ($errcount) {
1059 debugmsg ('info', "Backup job finished with errors", undef, 1);
1060 } else {
1061 debugmsg ('info', "Backup job finished successfully", undef, 1);
1062 }
1063 }
1064
1065 my $totaltime = time() - $starttime;
1066
1067 eval { $self->$sendmail ($tasklist, $totaltime); };
1068 debugmsg ('err', $@) if $@;
1069
1070 die $err if $err;
1071
1072 die "job errors\n" if $errcount;
1073 }
1074
1075 my $confdesc = {
1076 vmid => {
1077 type => 'string', format => 'pve-vmid-list',
1078 description => "The ID of the VM you want to backup.",
1079 optional => 1,
1080 },
1081 node => get_standard_option('pve-node', {
1082 description => "Only run if executed on this node.",
1083 optional => 1,
1084 }),
1085 all => {
1086 type => 'boolean',
1087 description => "Backup all known VMs on this host.",
1088 optional => 1,
1089 default => 0,
1090 },
1091 stdexcludes => {
1092 type => 'boolean',
1093 description => "Exclude temorary files and logs.",
1094 optional => 1,
1095 default => 1,
1096 },
1097 compress => {
1098 type => 'string',
1099 description => "Compress dump file.",
1100 optional => 1,
1101 enum => ['0', '1', 'gzip', 'lzo'],
1102 default => 'lzo',
1103 },
1104 quiet => {
1105 type => 'boolean',
1106 description => "Be quiet.",
1107 optional => 1,
1108 default => 0,
1109 },
1110 mode => {
1111 type => 'string',
1112 description => "Backup mode.",
1113 optional => 1,
1114 default => 'stop',
1115 enum => [ 'snapshot', 'suspend', 'stop' ],
1116 },
1117 exclude => {
1118 type => 'string', format => 'pve-vmid-list',
1119 description => "exclude specified VMs (assumes --all)",
1120 optional => 1,
1121 },
1122 'exclude-path' => {
1123 type => 'string', format => 'string-alist',
1124 description => "exclude certain files/directories (regex).",
1125 optional => 1,
1126 },
1127 mailto => {
1128 type => 'string', format => 'string-list',
1129 description => "",
1130 optional => 1,
1131 },
1132 tmpdir => {
1133 type => 'string',
1134 description => "Store temporary files to specified directory.",
1135 optional => 1,
1136 },
1137 dumpdir => {
1138 type => 'string',
1139 description => "Store resulting files to specified directory.",
1140 optional => 1,
1141 },
1142 script => {
1143 type => 'string',
1144 description => "Use specified hook script.",
1145 optional => 1,
1146 },
1147 storage => get_standard_option('pve-storage-id', {
1148 description => "Store resulting file to this storage.",
1149 optional => 1,
1150 }),
1151 size => {
1152 type => 'integer',
1153 description => "LVM snapshot size im MB.",
1154 optional => 1,
1155 minimum => 500,
1156 },
1157 bwlimit => {
1158 type => 'integer',
1159 description => "Limit I/O bandwidth (KBytes per second).",
1160 optional => 1,
1161 minimum => 0,
1162 },
1163 ionice => {
1164 type => 'integer',
1165 description => "Set CFQ ionice priority.",
1166 optional => 1,
1167 minimum => 0,
1168 maximum => 8,
1169 },
1170 lockwait => {
1171 type => 'integer',
1172 description => "Maximal time to wait for the global lock (minutes).",
1173 optional => 1,
1174 minimum => 0,
1175 },
1176 stopwait => {
1177 type => 'integer',
1178 description => "Maximal time to wait until a VM is stopped (minutes).",
1179 optional => 1,
1180 minimum => 0,
1181 },
1182 maxfiles => {
1183 type => 'integer',
1184 description => "Maximal number of backup files per VM.",
1185 optional => 1,
1186 minimum => 1,
1187 },
1188 remove => {
1189 type => 'boolean',
1190 description => "Remove old backup files if there are more than 'maxfiles' backup files.",
1191 optional => 1,
1192 default => 1,
1193 },
1194 };
1195
1196 sub option_exists {
1197 my $key = shift;
1198 return defined($confdesc->{$key});
1199 }
1200
1201 # add JSON properties for create and set function
1202 sub json_config_properties {
1203 my $prop = shift;
1204
1205 foreach my $opt (keys %$confdesc) {
1206 $prop->{$opt} = $confdesc->{$opt};
1207 }
1208
1209 return $prop;
1210 }
1211
1212 sub verify_vzdump_parameters {
1213 my ($param, $check_missing) = @_;
1214
1215 raise_param_exc({ all => "option conflicts with option 'vmid'"})
1216 if $param->{all} && $param->{vmid};
1217
1218 raise_param_exc({ exclude => "option conflicts with option 'vmid'"})
1219 if $param->{exclude} && $param->{vmid};
1220
1221 $param->{all} = 1 if defined($param->{exclude});
1222
1223 return if !$check_missing;
1224
1225 raise_param_exc({ vmid => "property is missing"})
1226 if !$param->{all} && !$param->{vmid};
1227
1228 }
1229
1230 sub command_line {
1231 my ($param) = @_;
1232
1233 my $cmd = "vzdump";
1234
1235 if ($param->{vmid}) {
1236 $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
1237 }
1238
1239 foreach my $p (keys %$param) {
1240 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' || $p eq 'dow' || $p eq 'stdout';
1241 my $v = $param->{$p};
1242 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
1243 if ($p eq 'exclude-path') {
1244 foreach my $path (split(/\0/, $v || '')) {
1245 $cmd .= " --$p " . PVE::Tools::shellquote($path);
1246 }
1247 } else {
1248 $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
1249 }
1250 }
1251
1252 return $cmd;
1253 }
1254
1255 1;