]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/VZDump.pm
vzdump: included_guest: return empty hash if no guests selected
[pve-manager.git] / PVE / VZDump.pm
index 647eece3859563479e38665c96228ebc1d5de3e6..601cd56e64cd607091d1c2d4daea64008b68c27f 100644 (file)
@@ -2,67 +2,60 @@ package PVE::VZDump;
 
 use strict;
 use warnings;
+
 use Fcntl ':flock';
-use PVE::Exception qw(raise_param_exc);
-use PVE::SafeSyslog;
+use File::Path;
 use IO::File;
 use IO::Select;
 use IPC::Open3;
 use POSIX qw(strftime);
-use File::Path;
-use PVE::RPCEnvironment;
-use PVE::Storage;
-use PVE::Cluster qw(cfs_read_file);
-use PVE::VZDump::OpenVZ;
-use Time::localtime;
 use Time::Local;
+
+use PVE::Cluster qw(cfs_read_file);
+use PVE::DataCenterConfig;
+use PVE::Exception qw(raise_param_exc);
+use PVE::HA::Config;
+use PVE::HA::Env::PVE2;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::RPCEnvironment;
+use PVE::Storage;
+use PVE::VZDump::Common;
+use PVE::VZDump::Plugin;
+use PVE::Tools qw(extract_param split_list);
+use PVE::API2Tools;
 
 my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
 
 my $lockfile = '/var/run/vzdump.lock';
-
+my $pidfile = '/var/run/vzdump.pid';
 my $logdir = '/var/log/vzdump';
 
-my @plugins = qw (PVE::VZDump::OpenVZ);
+my @plugins = qw();
+
+my $confdesc = PVE::VZDump::Common::get_confdesc();
 
 # Load available plugins
-my $pveplug = "/usr/share/perl5/PVE/VZDump/QemuServer.pm";
-if (-f $pveplug) {
-    eval { require $pveplug; };
-    if (!$@) {
-       PVE::VZDump::QemuServer->import ();
-         push @plugins, "PVE::VZDump::QemuServer";
-      } else {
-         warn $@;
-      }
+my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
+foreach my $plug (@pve_vzdump_classes) {
+    my $filename = "/usr/share/perl5/$plug.pm";
+    $filename =~ s!::!/!g;
+    if (-f $filename) {
+       eval { require $filename; };
+       if (!$@) {
+           $plug->import ();
+           push @plugins, $plug;
+       } else {
+           die $@;
+       }
+    }
 }
 
 # helper functions
 
-my $debugstattxt = {
-    err =>  'ERROR:',
-    info => 'INFO:',
-    warn => 'WARN:',
-};
-
 sub debugmsg {
     my ($mtype, $msg, $logfd, $syslog) = @_;
 
-    chomp $msg;
-
-    return if !$msg;
-
-    my $pre = $debugstattxt->{$mtype} || $debugstattxt->{'err'};
-
-    my $timestr = strftime ("%b %d %H:%M:%S", CORE::localtime);
-
-    syslog ($mtype eq 'info' ? 'info' : 'err', "$pre $msg") if $syslog;
-
-    foreach my $line (split (/\n/, $msg)) {
-       print STDERR "$pre $line\n";
-       print $logfd "$timestr $pre $line\n" if $logfd;
-    }
+    PVE::VZDump::Plugin::debugmsg(@_);
 }
 
 sub run_command {
@@ -79,21 +72,30 @@ sub run_command {
 sub storage_info {
     my $storage = shift;
 
-    my $cfg = cfs_read_file('storage.cfg');
+    my $cfg = PVE::Storage::config();
     my $scfg = PVE::Storage::storage_config($cfg, $storage);
     my $type = $scfg->{type};
-    die "can't use storage type '$type' for backup\n" 
-       if (!($type eq 'dir' || $type eq 'nfs' || $type eq 'glusterfs'));
-    die "can't use storage for backups - wrong content type\n" 
+
+    die "can't use storage type '$type' for backup\n"
+       if (!($type eq 'dir' || $type eq 'nfs' || $type eq 'glusterfs'
+             || $type eq 'cifs' || $type eq 'cephfs' || $type eq 'pbs'));
+    die "can't use storage '$storage' for backups - wrong content type\n"
        if (!$scfg->{content}->{backup});
 
     PVE::Storage::activate_storage($cfg, $storage);
 
-    return {
-       dumpdir => PVE::Storage::get_backup_dir($cfg, $storage),
+    my $info = {
+       scfg => $scfg,
        maxfiles => $scfg->{maxfiles},
     };
+
+    if ($type eq 'pbs') {
+       $info->{pbs} = 1;
+    } else {
+       $info->{dumpdir} = PVE::Storage::get_backup_dir($cfg, $storage);
+    }
+
+    return $info;
 }
 
 sub format_size {
@@ -106,13 +108,15 @@ sub format_size {
     }
 
     my $mb = $size / (1024*1024);
-
     if ($mb < 1024) {
        return int ($mb) . "MB";
-    } else {
-       my $gb = $mb / 1024;
+    }
+    my $gb = $mb / 1024;
+    if ($gb < 1024) {
        return sprintf ("%.2fGB", $gb);
-    } 
+    }
+    my $tb = $gb / 1024;
+    return sprintf ("%.2fTB", $tb);
 }
 
 sub format_time {
@@ -161,7 +165,7 @@ sub check_vmids {
     my (@vmids) = @_;
 
     my $res = [];
-    foreach my $vmid (@vmids) {
+    for my $vmid (sort {$a <=> $b} @vmids) {
        die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/;
        $vmid = int ($vmid); # remove leading zeros
        next if !$vmid;
@@ -176,75 +180,37 @@ sub read_vzdump_defaults {
 
     my $fn = "/etc/vzdump.conf";
 
-    my $res = {
-       bwlimit => 0,
-       ionice => 7,
-       size => 1024,
-       lockwait => 3*60, # 3 hours
-       stopwait => 10, # 10 minutes
-       mode => 'snapshot',
-       maxfiles => 1, 
+    my $defaults = {
+       map {
+           my $default = $confdesc->{$_}->{default};
+            defined($default) ? ($_ => $default) : ()
+       } keys %$confdesc
     };
 
-    my $fh = IO::File->new ("<$fn");
-    return $res if !$fh;
-    
-    my $line;
-    while (defined ($line = <$fh>)) {
-       next if $line =~ m/^\s*$/;
-       next if $line =~ m/^\#/;
-
-       if ($line =~ m/tmpdir:\s*(.*\S)\s*$/) {
-           $res->{tmpdir} = $1;
-       } elsif ($line =~ m/dumpdir:\s*(.*\S)\s*$/) {
-           $res->{dumpdir} = $1;
-       } elsif ($line =~ m/storage:\s*(\S+)\s*$/) {
-           $res->{storage} = $1;
-       } elsif ($line =~ m/script:\s*(.*\S)\s*$/) {
-           $res->{script} = $1;
-       } elsif ($line =~ m/bwlimit:\s*(\d+)\s*$/) {
-           $res->{bwlimit} = int($1);
-       } elsif ($line =~ m/ionice:\s*([0-8])\s*$/) {
-           $res->{ionice} = int($1);
-       } elsif ($line =~ m/lockwait:\s*(\d+)\s*$/) {
-           $res->{lockwait} = int($1);
-       } elsif ($line =~ m/stopwait:\s*(\d+)\s*$/) {
-           $res->{stopwait} = int($1);
-       } elsif ($line =~ m/size:\s*(\d+)\s*$/) {
-           $res->{size} = int($1);
-       } elsif ($line =~ m/maxfiles:\s*(\d+)\s*$/) {
-           $res->{maxfiles} = int($1);
-       } elsif ($line =~ m/exclude-path:\s*(.*)\s*$/) {
-           $res->{'exclude-path'} = PVE::Tools::split_args($1); 
-       } elsif ($line =~ m/mode:\s*(stop|snapshot|suspend)\s*$/) {
-           $res->{mode} = $1;
-       } else {
-           debugmsg ('warn', "unable to parse configuration file '$fn' - error at line " . $., undef, 1);
-       }
+    my $raw;
+    eval { $raw = PVE::Tools::file_get_contents($fn); };
+    return $defaults if $@;
 
+    my $conf_schema = { type => 'object', properties => $confdesc, };
+    my $res = PVE::JSONSchema::parse_config($conf_schema, $fn, $raw);
+    if (my $excludes = $res->{'exclude-path'}) {
+       $res->{'exclude-path'} = PVE::Tools::split_args($excludes);
     }
-    close ($fh);
-
-    return $res;
-}
-
-
-sub find_add_exclude {
-    my ($self, $excltype, $value) = @_;
-
-    if (($excltype eq '-regex') || ($excltype eq '-files')) {
-       $value = "\.$value";
+    if (defined($res->{mailto})) {
+       my @mailto = split_list($res->{mailto});
+       $res->{mailto} = [ @mailto ];
     }
 
-    if ($excltype eq '-files') {
-       push @{$self->{findexcl}}, "'('", '-not', '-type', 'd', '-regex' , "'$value'", "')'", '-o';
-    } else {
-       push @{$self->{findexcl}}, "'('", $excltype , "'$value'", '-prune', "')'", '-o';
+    foreach my $key (keys %$defaults) {
+       $res->{$key} = $defaults->{$key} if !defined($res->{$key});
     }
+
+    return $res;
 }
 
+use constant MAX_MAIL_SIZE => 1024*1024;
 sub sendmail {
-    my ($self, $tasklist, $totaltime, $err) = @_;
+    my ($self, $tasklist, $totaltime, $err, $detail_pre, $detail_post) = @_;
 
     my $opts = $self->{opts};
 
@@ -260,7 +226,7 @@ sub sendmail {
        chomp $task->{msg} if $task->{msg};
        $task->{backuptime} = 0 if !$task->{backuptime};
        $task->{size} = 0 if !$task->{size};
-       $task->{tarfile} = 'unknown' if !$task->{tarfile};
+       $task->{target} = 'unknown' if !$task->{target};
        $task->{hostname} = "VM $task->{vmid}" if !$task->{hostname};
 
        if ($task->{state} eq 'todo') {
@@ -268,99 +234,72 @@ sub sendmail {
        }
     }
 
-    return if (!$ecount && !$err && $opts->{mailnotification} eq 'failure');
+    my $notify = $opts->{mailnotification} || 'always';
+    return if (!$ecount && !$err && ($notify eq 'failure'));
 
     my $stat = ($ecount || $err) ? 'backup failed' : 'backup successful';
-    $stat .= ": $err" if $err;
+    if ($err) {
+       if ($err =~ /\n/) {
+           $stat .= ": multiple problems";
+       } else {
+           $stat .= ": $err";
+           $err = undef;
+       }
+    }
 
     my $hostname = `hostname -f` || PVE::INotify::nodename();
     chomp $hostname;
 
-    my $boundary = "----_=_NextPart_001_".int(time).$$;
-
-    my $rcvrarg = '';
-    foreach my $r (@$mailto) {
-       $rcvrarg .= " '$r'";
-    }
-    my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
-    my $mailfrom = $dcconf->{email_from} || "root";
-
-    open (MAIL,"|sendmail -B 8BITMIME -f $mailfrom $rcvrarg") || 
-       die "unable to open 'sendmail' - $!";
-
-    my $rcvrtxt = join (', ', @$mailto);
-
-    print MAIL "Content-Type: multipart/alternative;\n";
-    print MAIL "\tboundary=\"$boundary\"\n";
-    print MAIL "MIME-Version: 1.0\n";
-
-    print MAIL "FROM: vzdump backup tool <$mailfrom>\n";
-    print MAIL "TO: $rcvrtxt\n";
-    print MAIL "SUBJECT: vzdump backup status ($hostname) : $stat\n";
-    print MAIL "\n";
-    print MAIL "This is a multi-part message in MIME format.\n\n";
-    print MAIL "--$boundary\n";
-
-    print MAIL "Content-Type: text/plain;\n";
-    print MAIL "\tcharset=\"UTF8\"\n";
-    print MAIL "Content-Transfer-Encoding: 8bit\n";
-    print MAIL "\n";
-
     # text part
-
-    my $fill = '  '; # Avoid The Remove Extra Line Breaks Issue (MS Outlook)
-
-    print MAIL sprintf ("${fill}%-10s %-6s %10s %10s  %s\n", qw(VMID STATUS TIME SIZE FILENAME));
+    my $text = $err ? "$err\n\n" : '';
+    $text .= sprintf ("%-10s %-6s %10s %10s  %s\n", qw(VMID STATUS TIME SIZE FILENAME));
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        if  ($task->{state} eq 'ok') {
 
-           print MAIL sprintf ("${fill}%-10s %-6s %10s %10s  %s\n", $vmid, 
-                               $task->{state}, 
+           $text .= sprintf ("%-10s %-6s %10s %10s  %s\n", $vmid,
+                               $task->{state},
                                format_time($task->{backuptime}),
                                format_size ($task->{size}),
-                               $task->{tarfile});
+                               $task->{target});
        } else {
-           print MAIL sprintf ("${fill}%-10s %-6s %10s %8.2fMB  %s\n", $vmid, 
-                               $task->{state}, 
+           $text .= sprintf ("%-10s %-6s %10s %8.2fMB  %s\n", $vmid,
+                               $task->{state},
                                format_time($task->{backuptime}),
                                0, '-');
        }
     }
-    print MAIL "${fill}\n";
-    print MAIL "${fill}Detailed backup logs:\n";
-    print MAIL "${fill}\n";
-    print MAIL "$fill$cmdline\n";
-    print MAIL "${fill}\n";
 
+    my $text_log_part;
+    $text_log_part .= "\nDetailed backup logs:\n\n";
+    $text_log_part .= "$cmdline\n\n";
+
+    $text_log_part .= $detail_pre . "\n" if defined($detail_pre);
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        my $log = $task->{tmplog};
        if (!$log) {
-           print MAIL "${fill}$vmid: no log available\n\n";
+           $text_log_part .= "$vmid: no log available\n\n";
            next;
        }
-       open (TMP, "$log");
-       while (my $line = <TMP>) { print MAIL encode8bit ("${fill}$vmid: $line"); }
+       if (open (TMP, "$log")) {
+           while (my $line = <TMP>) {
+               next if $line =~ /^status: \d+/; # not useful in mails
+               $text_log_part .= encode8bit ("$vmid: $line");
+           }
+       } else {
+           $text_log_part .= "$vmid: Could not open log file\n\n";
+       }
        close (TMP);
-       print MAIL "${fill}\n";
+       $text_log_part .= "\n";
     }
-
-    # end text part
-    print MAIL "\n--$boundary\n";
-
-    print MAIL "Content-Type: text/html;\n";
-    print MAIL "\tcharset=\"UTF8\"\n";
-    print MAIL "Content-Transfer-Encoding: 8bit\n";
-    print MAIL "\n";
+    $text_log_part .= $detail_post if defined($detail_post);
 
     # html part
-
-    print MAIL "<html><body>\n";
-
-    print MAIL "<table border=1 cellpadding=3>\n";
-
-    print MAIL "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
+    my $html = "<html><body>\n";
+    $html .= "<p>" . (escape_html($err) =~ s/\n/<br>/gr) . "</p>\n" if $err;
+    $html .= "<table border=1 cellpadding=3>\n";
+    $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
 
     my $ssize = 0;
 
@@ -372,56 +311,75 @@ sub sendmail {
 
            $ssize += $task->{size};
 
-           print MAIL sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n", 
+           $html .= sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
                                $vmid, $name,
                                format_time($task->{backuptime}),
                                format_size ($task->{size}),
-                               escape_html ($task->{tarfile}));
+                               escape_html ($task->{target}));
        } else {
-           print MAIL sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
-                               $vmid, $name, format_time($task->{backuptime}), 
+           $html .= sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
+                               $vmid, $name, format_time($task->{backuptime}),
                                escape_html ($task->{msg}));
        }
     }
 
-    print MAIL sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
+    $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
  format_time ($totaltime), format_size ($ssize));
 
-    print MAIL "</table><br><br>\n";
-    print MAIL "Detailed backup logs:<br>\n";
-    print MAIL "<br>\n";
-    print MAIL "<pre>\n";
-    print MAIL escape_html($cmdline) . "\n";
-    print MAIL "\n";
+    $html .= "\n</table><br><br>\n";
+    my $html_log_part;
+    $html_log_part .= "Detailed backup logs:<br /><br />\n";
+    $html_log_part .= "<pre>\n";
+    $html_log_part .= escape_html($cmdline) . "\n\n";
 
+    $html_log_part .= escape_html($detail_pre) . "\n" if defined($detail_pre);
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        my $log = $task->{tmplog};
        if (!$log) {
-           print MAIL "$vmid: no log available\n\n";
+           $html_log_part .= "$vmid: no log available\n\n";
            next;
        }
-       open (TMP, "$log");
-       while (my $line = <TMP>) {
-           if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
-               print MAIL encode8bit ("$vmid: <font color=red>". 
-                                      escape_html ($line) . "</font>"); 
-           } else {
-               print MAIL encode8bit ("$vmid: " . escape_html ($line)); 
+       if (open (TMP, "$log")) {
+           while (my $line = <TMP>) {
+               next if $line =~ /^status: \d+/; # not useful in mails
+               if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
+                   $html_log_part .= encode8bit ("$vmid: <font color=red>".
+                       escape_html ($line) . "</font>");
+               } else {
+                   $html_log_part .= encode8bit ("$vmid: " . escape_html ($line));
+               }
            }
+       } else {
+           $html_log_part .= "$vmid: Could not open log file\n\n";
        }
        close (TMP);
-       print MAIL "\n";
+       $html_log_part .= "\n";
+    }
+    $html_log_part .= escape_html($detail_post) if defined($detail_post);
+    $html_log_part .= "</pre>";
+    my $html_end .= "\n</body></html>\n";
+    # end html part
+
+    if (length($text) + length($text_log_part) +
+       length($html) + length($html_log_part) < MAX_MAIL_SIZE)
+    {
+       $html .= $html_log_part;
+       $text .= $text_log_part;
+    } else {
+       my $msg = "Log output was too long to be sent by mail. ".
+           "See Task History for details!\n";
+       $text .= $msg;
+       $html .= "<p>$msg</p>";
+       $html .= $html_end;
     }
-    print MAIL "</pre>\n";
 
-    print MAIL "</body></html>\n";
+    my $subject = "vzdump backup status ($hostname) : $stat";
 
-    # end html part
-    print MAIL "\n--$boundary--\n";
+    my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
+    my $mailfrom = $dcconf->{email_from} || "root";
 
-    close(MAIL);
+    PVE::Tools::sendmail($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool");
 };
 
 sub new {
@@ -447,11 +405,10 @@ sub new {
 
     my $defaults = read_vzdump_defaults();
 
-    my $maxfiles = $opts->{maxfiles}; # save here, because we overwrite with default
-
     $opts->{remove} = 1 if !defined($opts->{remove});
 
     foreach my $k (keys %$defaults) {
+       next if $k eq 'exclude-path' || $k eq 'maxfiles'; # dealt with separately
        if ($k eq 'dumpdir' || $k eq 'storage') {
            $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
                !defined ($opts->{storage});
@@ -466,28 +423,19 @@ sub new {
     $skiplist = [] if !$skiplist;
     my $self = bless { cmdline => $cmdline, opts => $opts, skiplist => $skiplist };
 
-    #always skip '.'
-    push @{$self->{findexcl}}, "'('", '-regex' , "'^\\.\$'", "')'", '-o';
-
-    $self->find_add_exclude ('-type', 's'); # skip sockets
-
+    my $findexcl = $self->{findexcl} = [];
     if ($defaults->{'exclude-path'}) {
-       foreach my $path (@{$defaults->{'exclude-path'}}) {
-           $self->find_add_exclude ('-regex', $path);
-       }
+       push @$findexcl, @{$defaults->{'exclude-path'}};
     }
 
     if ($opts->{'exclude-path'}) {
-       foreach my $path (@{$opts->{'exclude-path'}}) {
-           $self->find_add_exclude ('-regex', $path);
-       }
+       push @$findexcl, @{$opts->{'exclude-path'}};
     }
 
     if ($opts->{stdexcludes}) {
-       $self->find_add_exclude ('-files', '/var/log/.+');
-       $self->find_add_exclude ('-regex', '/tmp/.+');
-       $self->find_add_exclude ('-regex', '/var/tmp/.+');
-       $self->find_add_exclude ('-regex', '/var/run/.+pid');
+       push @$findexcl, '/tmp/?*',
+                        '/var/tmp/?*',
+                        '/var/run/?*.pid';
     }
 
     foreach my $p (@plugins) {
@@ -497,56 +445,47 @@ sub new {
        push @{$self->{plugins}}, $pd;
     }
 
+    if (defined($opts->{storage}) && $opts->{stdout}) {
+       die "cannot use options 'storage' and 'stdout' at the same time\n";
+    } elsif (defined($opts->{storage}) && defined($opts->{dumpdir})) {
+       die "cannot use options 'storage' and 'dumpdir' at the same time\n";
+    }
+
     if (!$opts->{dumpdir} && !$opts->{storage}) {
        $opts->{storage} = 'local';
     }
 
+    my $errors = '';
+
     if ($opts->{storage}) {
-       my $info = storage_info ($opts->{storage});
+       my $info = eval { storage_info ($opts->{storage}) };
+       $errors .= "could not get storage information for '$opts->{storage}': $@"
+           if ($@);
        $opts->{dumpdir} = $info->{dumpdir};
-       $maxfiles = $info->{maxfiles} if !defined($maxfiles) && defined($info->{maxfiles});
+       $opts->{scfg} = $info->{scfg};
+       $opts->{pbs} = $info->{pbs};
+       $opts->{maxfiles} //= $info->{maxfiles};
     } elsif ($opts->{dumpdir}) {
-       die "dumpdir '$opts->{dumpdir}' does not exist\n"
+       $errors .= "dumpdir '$opts->{dumpdir}' does not exist"
            if ! -d $opts->{dumpdir};
     } else {
-       die "internal error"; 
+       die "internal error";
     }
 
+    $opts->{maxfiles} //= $defaults->{maxfiles};
+
     if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
-       die "tmpdir '$opts->{tmpdir}' does not exist\n";
+       $errors .= "\n" if $errors;
+       $errors .= "tmpdir '$opts->{tmpdir}' does not exist";
     }
 
-    $opts->{maxfiles} = $maxfiles if defined($maxfiles);
+    if ($errors) {
+       eval { $self->sendmail([], 0, $errors); };
+       debugmsg ('err', $@) if $@;
+       die "$errors\n";
+    }
 
     return $self;
-
-}
-
-sub get_lvm_mapping {
-
-    my $devmapper;
-
-    my $cmd = ['lvs', '--units', 'm', '--separator', ':', '--noheadings',
-              '-o', 'vg_name,lv_name,lv_size' ];
-
-    my $parser = sub {
-       my $line = shift;
-       if ($line =~ m|^\s*(\S+):(\S+):(\d+(\.\d+))[Mm]$|) {
-           my $vg = $1;
-           my $lv = $2;
-           $devmapper->{"/dev/$vg/$lv"} = [$vg, $lv];
-           my $qlv = $lv;
-           $qlv =~ s/-/--/g;
-           my $qvg = $vg;
-           $qvg =~ s/-/--/g;
-           $devmapper->{"/dev/mapper/$qvg-$qlv"} = [$vg, $lv];
-       }                       
-    };
-
-    eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); };
-    warn $@ if $@;
-
-    return $devmapper;
 }
 
 sub get_mount_info {
@@ -576,66 +515,55 @@ sub get_mount_info {
     return $res;
 }
 
-sub get_lvm_device {
-    my ($dir, $mapping) = @_;
-
-    my $info = get_mount_info($dir);
-
-    return undef if !$info;
-   
-    my $dev = $info->{device};
-
-    my ($vg, $lv);
+sub getlock {
+    my ($self, $upid) = @_;
 
-    ($vg, $lv) = @{$mapping->{$dev}} if defined $mapping->{$dev};
+    my $fh;
 
-    return wantarray ? ($dev, $info->{mountpoint}, $vg, $lv, $info->{fstype}) : $dev;
-}
+    my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
 
-sub getlock {
-    my ($self) = @_;
+    die "missimg UPID" if !$upid; # should not happen
 
-    my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
     if (!open (SERVER_FLCK, ">>$lockfile")) {
        debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1);
        die "can't open lock on file '$lockfile' - $!";
     }
 
-    if (flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
-       return;
-    }
+    if (!flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
 
-    if (!$maxwait) {
-       debugmsg ('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
-       die "can't aquire lock '$lockfile' (wait = 0)";
-    }
+       if (!$maxwait) {
+           debugmsg ('err', "can't acquire lock '$lockfile' (wait = 0)", undef, 1);
+           die "can't acquire lock '$lockfile' (wait = 0)";
+       }
 
-    debugmsg('info', "trying to get global lock - waiting...", undef, 1);
+       debugmsg('info', "trying to get global lock - waiting...", undef, 1);
 
-    eval {
-       alarm ($maxwait * 60);
-       
-       local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
+       eval {
+           alarm ($maxwait * 60);
 
-       if (!flock (SERVER_FLCK, LOCK_EX)) {
-           my $err = $!;
-           close (SERVER_FLCK);
+           local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
+
+           if (!flock (SERVER_FLCK, LOCK_EX)) {
+               my $err = $!;
+               close (SERVER_FLCK);
+               alarm (0);
+               die "$err\n";
+           }
            alarm (0);
-           die "$err\n";
-       }
+       };
        alarm (0);
-    };
-    alarm (0);
-    
-    my $err = $@;
 
-    if ($err) {
-       debugmsg ('err', "can't aquire lock '$lockfile' - $err", undef, 1);
-       die "can't aquire lock '$lockfile' - $err";
+       my $err = $@;
+
+       if ($err) {
+           debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1);
+           die "can't acquire lock '$lockfile' - $err";
+       }
+
+       debugmsg('info', "got global lock", undef, 1);
     }
 
-    debugmsg('info', "got global lock", undef, 1);
+    PVE::Tools::file_set_contents($pidfile, $upid);
 }
 
 sub run_hook_script {
@@ -644,20 +572,22 @@ sub run_hook_script {
     my $opts = $self->{opts};
 
     my $script = $opts->{script};
-
     return if !$script;
 
+    if (!-x $script) {
+       die "The hook script '$script' is not executable.\n";
+    }
+
     my $cmd = "$script $phase";
 
     $cmd .= " $task->{mode} $task->{vmid}" if ($task);
 
     local %ENV;
-
     # set immutable opts directly (so they are available in all phases)
     $ENV{STOREID} = $opts->{storage} if $opts->{storage};
     $ENV{DUMPDIR} = $opts->{dumpdir} if $opts->{dumpdir};
 
-    foreach my $ek (qw(vmtype hostname tarfile logfile)) {
+    foreach my $ek (qw(vmtype hostname target logfile)) {
        $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
     }
 
@@ -665,14 +595,31 @@ sub run_hook_script {
 }
 
 sub compressor_info {
-    my ($opt_compress) = @_;
+    my ($opts) = @_;
+    my $opt_compress = $opts->{compress};
 
     if (!$opt_compress || $opt_compress eq '0') {
        return undef;
     } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
        return ('lzop', 'lzo');
     } elsif ($opt_compress eq 'gzip') {
-       return ('gzip', 'gz');
+       if ($opts->{pigz} > 0) {
+           my $pigz_threads = $opts->{pigz};
+           if ($pigz_threads == 1) {
+               my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
+               $pigz_threads = int(($cpuinfo->{cpus} + 1)/2);
+           }
+           return ("pigz -p ${pigz_threads} --rsyncable", 'gz');
+       } else {
+           return ('gzip --rsyncable', 'gz');
+       }
+    } elsif ($opt_compress eq 'zstd') {
+       my $zstd_threads = $opts->{zstd} // 1;
+       if ($zstd_threads == 0) {
+           my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
+           $zstd_threads = int(($cpuinfo->{cpus} + 1)/2);
+       }
+       return ("zstd --rsyncable --threads=${zstd_threads}", 'zst');
     } else {
        die "internal error - unknown compression option '$opt_compress'";
     }
@@ -684,79 +631,122 @@ sub get_backup_file_list {
     my $bklist = [];
     foreach my $fn (<$dir/${bkname}-*>) {
        next if $exclude_fn && $fn eq $exclude_fn;
-       if ($fn =~ m!/(${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?)))$!) {
+       if ($fn =~ m!/(${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) {
            $fn = "$dir/$1"; # untaint
-           my $t = timelocal ($7, $6, $5, $4, $3 - 1, $2 - 1900);
+           my $t = timelocal ($7, $6, $5, $4, $3 - 1, $2);
            push @$bklist, [$fn, $t];
        }
     }
 
     return $bklist;
 }
+
 sub exec_backup_task {
     my ($self, $task) = @_;
-        
+
     my $opts = $self->{opts};
 
     my $vmid = $task->{vmid};
     my $plugin = $task->{plugin};
+    my $vmtype = $plugin->type();
+
+    $task->{backup_time} = time();
+
+    my $pbs_group_name;
+    my $pbs_snapshot_name;
+
+    if ($self->{opts}->{pbs}) {
+       if ($vmtype eq 'lxc') {
+           $pbs_group_name = "ct/$vmid";
+       } elsif  ($vmtype eq 'qemu') {
+           $pbs_group_name = "vm/$vmid";
+       } else {
+           die "pbs backup not implemented for plugin type '$vmtype'\n";
+       }
+       my $btime = strftime("%FT%TZ", gmtime($task->{backup_time}));
+       $pbs_snapshot_name = "$pbs_group_name/$btime";
+    }
 
     my $vmstarttime = time ();
-    
+
     my $logfd;
 
     my $cleanup = {};
 
-    my $vmstoptime = 0;
+    my $log_vm_online_again = sub {
+       return if !defined($task->{vmstoptime});
+       $task->{vmconttime} //= time();
+       my $delay = $task->{vmconttime} - $task->{vmstoptime};
+       debugmsg ('info', "guest is online again after $delay seconds", $logfd);
+    };
 
     eval {
        die "unable to find VM '$vmid'\n" if !$plugin;
 
-       my $vmtype = $plugin->type();
+       # for now we deny backups of a running ha managed service in *stop* mode
+       # as it interferes with the HA stack (started services should not stop).
+       if ($opts->{mode} eq 'stop' &&
+           PVE::HA::Config::vm_is_ha_managed($vmid, 'started'))
+       {
+           die "Cannot execute a backup with stop mode on a HA managed and".
+               " enabled Service. Use snapshot mode or disable the Service.\n";
+       }
 
        my $tmplog = "$logdir/$vmtype-$vmid.log";
 
-       my $lt = localtime();
-
        my $bkname = "vzdump-$vmtype-$vmid";
-       my $basename = sprintf "${bkname}-%04d_%02d_%02d-%02d_%02d_%02d", 
-       $lt->year + 1900, $lt->mon + 1, $lt->mday, 
-       $lt->hour, $lt->min, $lt->sec;
+       my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time}));
 
        my $maxfiles = $opts->{maxfiles};
 
        if ($maxfiles && !$opts->{remove}) {
-           my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname);
-           die "only $maxfiles backup(s) allowed - please consider to remove old backup files.\n" 
-               if scalar(@$bklist) >= $maxfiles;
+           my $count;
+           if ($self->{opts}->{pbs}) {
+               my $res = PVE::Storage::PBSPlugin::run_client_cmd($opts->{scfg}, $opts->{storage}, 'snapshots', $pbs_group_name);
+               $count = scalar(@$res);
+           } else {
+               my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname);
+               $count = scalar(@$bklist);
+           }
+           die "There is a max backup limit of ($maxfiles) enforced by the".
+               " target storage or the vzdump parameters.".
+               " Either increase the limit or delete old backup(s).\n"
+               if $count >= $maxfiles;
        }
 
-       my $logfile = $task->{logfile} = "$opts->{dumpdir}/$basename.log";
+       if (!$self->{opts}->{pbs}) {
+           $task->{logfile} = "$opts->{dumpdir}/$basename.log";
+       }
 
        my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar';
-       my ($comp, $comp_ext) = compressor_info($opts->{compress});
+       my ($comp, $comp_ext) = compressor_info($opts);
        if ($comp && $comp_ext) {
            $ext .= ".${comp_ext}";
        }
 
-       if ($opts->{stdout}) {
-           $task->{tarfile} = '-';
+       if ($self->{opts}->{pbs}) {
+           die "unable to pipe backup to stdout\n" if $opts->{stdout};
+           $task->{target} = $pbs_snapshot_name;
        } else {
-           my $tarfile = $task->{tarfile} = "$opts->{dumpdir}/$basename$ext";
-           $task->{tmptar} = $task->{tarfile};
-           $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
-           unlink $task->{tmptar};
+           if ($opts->{stdout}) {
+               $task->{target} = '-';
+           } else {
+               $task->{target} = $task->{tmptar} = "$opts->{dumpdir}/$basename$ext";
+               $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
+               unlink $task->{tmptar};
+           }
        }
 
        $task->{vmtype} = $vmtype;
 
-       if ($opts->{tmpdir}) {
-           $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$"; 
+       if ($self->{opts}->{pbs}) {
+           $task->{tmpdir} = "/var/tmp/vzdumptmp$$"; #fixme
+       } elsif ($opts->{tmpdir}) {
+           $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$";
        } else {
            # dumpdir is posix? then use it as temporary dir
            my $info = get_mount_info($opts->{dumpdir});
-           if ($vmtype eq 'qemu' || 
+           if ($vmtype eq 'qemu' ||
                grep ($_ eq $info->{fstype}, @posix_filesystems)) {
                $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp";
            } else {
@@ -776,11 +766,13 @@ sub exec_backup_task {
 
        $task->{dumpdir} = $opts->{dumpdir};
        $task->{storeid} = $opts->{storage};
+       $task->{scfg} = $opts->{scfg};
        $task->{tmplog} = $tmplog;
 
-       unlink $logfile;
+       unlink $task->{logfile} if defined($task->{logfile});
 
-       debugmsg ('info',  "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
+       debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
+       debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime()));
 
        $plugin->set_logfd ($logfd);
 
@@ -796,7 +788,7 @@ sub exec_backup_task {
 
        # prepare
 
-       my $mode = $running ? $opts->{mode} : 'stop';
+       my $mode = $running ? $task->{mode} : 'stop';
 
        if ($mode eq 'snapshot') {
            my %saved_task = %$task;
@@ -806,10 +798,12 @@ sub exec_backup_task {
                debugmsg ('info',  $err, $logfd);
                debugmsg ('info',  "trying 'suspend' mode instead", $logfd);
                $mode = 'suspend'; # so prepare is called again below
-               %$task = %saved_task; 
+               %$task = %saved_task;
            }
        }
 
+       $cleanup->{prepared} = 1;
+
        $task->{mode} = $mode;
 
        debugmsg ('info', "backup mode: $mode", $logfd);
@@ -827,12 +821,12 @@ sub exec_backup_task {
 
            if ($running) {
                debugmsg ('info', "stopping vm", $logfd);
-               $vmstoptime = time ();
+               $task->{vmstoptime} = time();
                $self->run_hook_script ('pre-stop', $task, $logfd);
                $plugin->stop_vm ($task, $vmid);
                $cleanup->{restart} = 1;
            }
+
 
        } elsif ($mode eq 'suspend') {
 
@@ -840,27 +834,27 @@ sub exec_backup_task {
 
            $self->run_hook_script ('backup-start', $task, $logfd);
 
-           if ($vmtype eq 'openvz') {
+           if ($vmtype eq 'lxc') {
                # pre-suspend rsync
-               $plugin->copy_data_phase1 ($task, $vmid);
+               $plugin->copy_data_phase1($task, $vmid);
            }
 
            debugmsg ('info', "suspend vm", $logfd);
-           $vmstoptime = time ();
+           $task->{vmstoptime} = time ();
            $self->run_hook_script ('pre-stop', $task, $logfd);
            $plugin->suspend_vm ($task, $vmid);
            $cleanup->{resume} = 1;
 
-           if ($vmtype eq 'openvz') {
+           if ($vmtype eq 'lxc') {
                # post-suspend rsync
-               $plugin->copy_data_phase2 ($task, $vmid);
+               $plugin->copy_data_phase2($task, $vmid);
 
                debugmsg ('info', "resume vm", $logfd);
                $cleanup->{resume} = 0;
-               $self->run_hook_script ('pre-restart', $task, $logfd);
-               $plugin->resume_vm ($task, $vmid);
-               my $delay = time () - $vmstoptime;
-               debugmsg ('info', "vm is online again after $delay seconds", $logfd);
+               $self->run_hook_script('pre-restart', $task, $logfd);
+               $plugin->resume_vm($task, $vmid);
+               $self->run_hook_script('post-restart', $task, $logfd);
+               $log_vm_online_again->();
            }
 
        } elsif ($mode eq 'snapshot') {
@@ -873,7 +867,7 @@ sub exec_backup_task {
 
            if ($snapshot_count > 1) {
                debugmsg ('info', "suspend vm to make snapshot", $logfd);
-               $vmstoptime = time ();
+               $task->{vmstoptime} = time ();
                $plugin->suspend_vm ($task, $vmid);
                $cleanup->{resume} = 1;
            }
@@ -886,18 +880,19 @@ sub exec_backup_task {
                debugmsg ('info', "resume vm", $logfd);
                $cleanup->{resume} = 0;
                $plugin->resume_vm ($task, $vmid);
-               my $delay = time () - $vmstoptime;
-               debugmsg ('info', "vm is online again after $delay seconds", $logfd);
+               $log_vm_online_again->();
            }
 
+           $self->run_hook_script ('post-restart', $task, $logfd);
+
        } else {
            die "internal error - unknown mode '$mode'\n";
        }
 
        # assemble archive image
        $plugin->assemble ($task, $vmid);
-       
-       # produce archive 
+
+       # produce archive
 
        if ($opts->{stdout}) {
            debugmsg ('info', "sending archive to stdout", $logfd);
@@ -906,30 +901,42 @@ sub exec_backup_task {
            return;
        }
 
-       debugmsg ('info', "creating archive '$task->{tarfile}'", $logfd);
+       my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump';
+       debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd);
        $plugin->archive($task, $vmid, $task->{tmptar}, $comp);
 
-       rename ($task->{tmptar}, $task->{tarfile}) ||
-           die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\n";
+       if ($self->{opts}->{pbs}) {
+           # size is added to task struct in guest vzdump plugins
+       } else {
+           rename ($task->{tmptar}, $task->{target}) ||
+               die "unable to rename '$task->{tmptar}' to '$task->{target}'\n";
 
-       # determine size
-       $task->{size} = (-s $task->{tarfile}) || 0;
-       my $cs = format_size ($task->{size}); 
-       debugmsg ('info', "archive file size: $cs", $logfd);
+           # determine size
+           $task->{size} = (-s $task->{target}) || 0;
+           my $cs = format_size ($task->{size});
+           debugmsg ('info', "archive file size: $cs", $logfd);
+       }
 
        # purge older backup
-
        if ($maxfiles && $opts->{remove}) {
-           my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{tarfile});
-           $bklist = [ sort { $b->[1] <=> $a->[1] } @$bklist ];
-
-           while (scalar (@$bklist) >= $maxfiles) {
-               my $d = pop @$bklist;
-               debugmsg ('info', "delete old backup '$d->[0]'", $logfd);
-               unlink $d->[0];
-               my $logfn = $d->[0];
-               $logfn =~ s/\.(tgz|((tar|vma)(\.(gz|lzo))?))$/\.log/;
-               unlink $logfn;
+
+           if ($self->{opts}->{pbs}) {
+               my $args = [$pbs_group_name, '--quiet', '1', '--keep-last', $maxfiles];
+               my $logfunc = sub { my $line = shift; debugmsg ('info', $line, $logfd); };
+               PVE::Storage::PBSPlugin::run_raw_client_cmd(
+                   $opts->{scfg}, $opts->{storage}, 'prune', $args, logfunc => $logfunc);
+           } else {
+               my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{target});
+               $bklist = [ sort { $b->[1] <=> $a->[1] } @$bklist ];
+
+               while (scalar (@$bklist) >= $maxfiles) {
+                   my $d = pop @$bklist;
+                   debugmsg ('info', "delete old backup '$d->[0]'", $logfd);
+                   unlink $d->[0];
+                   my $logfn = $d->[0];
+                   $logfn =~ s/\.(tgz|((tar|vma)(\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?))$/\.log/;
+                   unlink $logfn;
+               }
            }
        }
 
@@ -945,14 +952,17 @@ sub exec_backup_task {
            warn $@ if $@;
        }
 
-       eval { $plugin->cleanup ($task, $vmid) };
-       warn $@ if $@;
+       if ($cleanup->{prepared}) {
+           # only call cleanup when necessary (when prepare was executed)
+           eval { $plugin->cleanup ($task, $vmid) };
+           warn $@ if $@;
+       }
 
        eval { $plugin->set_logfd (undef); };
        warn $@ if $@;
 
-       if ($cleanup->{resume} || $cleanup->{restart}) {        
-           eval { 
+       if ($cleanup->{resume} || $cleanup->{restart}) {
+           eval {
                $self->run_hook_script ('pre-restart', $task, $logfd);
                if ($cleanup->{resume}) {
                    debugmsg ('info', "resume vm", $logfd);
@@ -963,14 +973,14 @@ sub exec_backup_task {
                        debugmsg ('info', "restarting vm", $logfd);
                        $plugin->start_vm ($task, $vmid);
                    }
-               } 
+               }
+               $self->run_hook_script ('post-restart', $task, $logfd);
            };
            my $err = $@;
            if ($err) {
                warn $err;
            } else {
-               my $delay = time () - $vmstoptime;
-               debugmsg ('info', "vm is online again after $delay seconds", $logfd);
+               $log_vm_online_again->();
            }
        }
     }
@@ -987,6 +997,7 @@ sub exec_backup_task {
        $task->{state} = 'err';
        $task->{msg} = $err;
        debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1);
+       debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime()));
 
        eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
 
@@ -994,12 +1005,21 @@ sub exec_backup_task {
        $task->{state} = 'ok';
        my $tstr = format_time ($delay);
        debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
+       debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime()));
     }
 
     close ($logfd) if $logfd;
-    
-    if ($task->{tmplog} && $task->{logfile}) {
-       system ("cp '$task->{tmplog}' '$task->{logfile}'");
+
+    if ($task->{tmplog}) {
+       if ($self->{opts}->{pbs}) {
+           if ($task->{state} eq 'ok') {
+               my $param = [$pbs_snapshot_name, $task->{tmplog}];
+               PVE::Storage::PBSPlugin::run_raw_client_cmd(
+                   $opts->{scfg}, $opts->{storage}, 'upload-log', $param, errmsg => "upload log failed");
+           }
+       } elsif ($task->{logfile}) {
+           system {'cp'} 'cp', $task->{tmplog}, $task->{logfile};
+       }
     }
 
     eval { $self->run_hook_script ('log-end', $task); };
@@ -1015,49 +1035,50 @@ sub exec_backup {
     debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1);
     debugmsg ('info', "skip external VMs: " . join(', ', @{$self->{skiplist}}))
        if scalar(@{$self->{skiplist}});
+
     my $tasklist = [];
+    my $vzdump_plugins =  {};
+    foreach my $plugin (@{$self->{plugins}}) {
+       my $type = $plugin->type();
+       next if exists $vzdump_plugins->{$type};
+       $vzdump_plugins->{$type} = $plugin;
+    }
 
-    if ($opts->{all}) {
-       foreach my $plugin (@{$self->{plugins}}) {
-           my $vmlist = $plugin->vmlist();
-           foreach my $vmid (sort @$vmlist) {
-               next if grep { $_ eq  $vmid } @{$opts->{exclude}};
-               next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], 1);
-               push @$tasklist, { vmid => $vmid,  state => 'todo', plugin => $plugin };
-           }
-       }
-    } else {
-       foreach my $vmid (sort @{$opts->{vmids}}) {
-           my $plugin;
-           foreach my $pg (@{$self->{plugins}}) {
-               my $vmlist = $pg->vmlist();
-               if (grep { $_ eq  $vmid } @$vmlist) {
-                   $plugin = $pg;
-                   last;
-               }
-           }
-           $rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ]);
-           push @$tasklist, { vmid => $vmid,  state => 'todo', plugin => $plugin };
-       }
+    my $vmlist = PVE::Cluster::get_vmlist();
+    foreach my $vmid (sort @{$opts->{vmids}}) {
+       my $guest_type = $vmlist->{ids}->{$vmid}->{type};
+       my $plugin = $vzdump_plugins->{$guest_type};
+       next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all});
+       push @$tasklist, {
+           mode => $opts->{mode},
+           plugin => $plugin,
+           state => 'todo',
+           vmid => $vmid,
+       };
     }
 
+    # Use in-memory files for the outer hook logs to pass them to sendmail.
+    my $job_start_log = '';
+    my $job_end_log = '';
+    open my $job_start_fd, '>', \$job_start_log;
+    open my $job_end_fd, '>', \$job_end_log;
+
     my $starttime = time();
     my $errcount = 0;
     eval {
 
-       $self->run_hook_script ('job-start');
+       $self->run_hook_script ('job-start', undef, $job_start_fd);
 
        foreach my $task (@$tasklist) {
            $self->exec_backup_task ($task);
            $errcount += 1 if $task->{state} ne 'ok';
        }
 
-       $self->run_hook_script ('job-end');    
+       $self->run_hook_script ('job-end', undef, $job_end_fd);
     };
     my $err = $@;
 
-    $self->run_hook_script ('job-abort') if $err;    
+    $self->run_hook_script ('job-abort', undef, $job_end_fd) if $err;
 
     if ($err) {
        debugmsg ('err', "Backup job failed - $err", undef, 1);
@@ -1069,160 +1090,27 @@ sub exec_backup {
        }
     }
 
+    close $job_start_fd;
+    close $job_end_fd;
+
     my $totaltime = time() - $starttime;
 
-    eval { $self->sendmail ($tasklist, $totaltime); };
+    eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); };
     debugmsg ('err', $@) if $@;
 
     die $err if $err;
 
-    die "job errors\n" if $errcount; 
+    die "job errors\n" if $errcount;
+
+    unlink $pidfile;
 }
 
-my $confdesc = {
-    vmid => {
-       type => 'string', format => 'pve-vmid-list',            
-       description => "The ID of the VM you want to backup.",
-       optional => 1,
-    },
-    node => get_standard_option('pve-node', { 
-       description => "Only run if executed on this node.",
-       optional => 1,
-    }),
-    all => {
-       type => 'boolean',
-       description => "Backup all known VMs on this host.",
-       optional => 1,
-       default => 0,
-    },
-    stdexcludes => {
-       type => 'boolean',
-       description => "Exclude temorary files and logs.",
-       optional => 1,
-       default => 1,
-    },
-    compress => {
-       type => 'string',
-       description => "Compress dump file.",
-       optional => 1,
-       enum => ['0', '1', 'gzip', 'lzo'],
-       default => 'lzo',
-    },
-    quiet => {
-       type => 'boolean',
-       description => "Be quiet.",
-       optional => 1,
-       default => 0,
-    },
-    mode => {
-       type => 'string',
-       description => "Backup mode.",
-       optional => 1,
-       default => 'stop',
-       enum => [ 'snapshot', 'suspend', 'stop' ],
-    },
-    exclude => {
-       type => 'string', format => 'pve-vmid-list',
-       description => "exclude specified VMs (assumes --all)",
-       optional => 1,
-    },
-    'exclude-path' => {
-       type => 'string', format => 'string-alist',
-       description => "exclude certain files/directories (regex).",
-       optional => 1,
-    },
-    mailto => {
-       type => 'string', format => 'string-list',
-       description => "",
-       optional => 1,
-    },
-    mailnotification => {
-        type => 'string',
-        description => "Specify when to send an email",
-        optional => 1,
-        enum => [ 'always', 'failure' ],
-        default => 'always',
-    },
-    tmpdir => {
-       type => 'string',
-       description => "Store temporary files to specified directory.",
-       optional => 1,
-    },
-    dumpdir => {
-       type => 'string',
-       description => "Store resulting files to specified directory.",
-       optional => 1,
-    },
-    script => {
-       type => 'string',
-       description => "Use specified hook script.",
-       optional => 1,
-    },
-    storage => get_standard_option('pve-storage-id', {
-       description => "Store resulting file to this storage.",
-       optional => 1,
-    }),
-    size => {
-       type => 'integer',
-       description => "LVM snapshot size im MB.",
-       optional => 1,
-       minimum => 500,
-    },
-    bwlimit => {
-       type => 'integer',
-       description => "Limit I/O bandwidth (KBytes per second).",
-       optional => 1,
-       minimum => 0,
-    },
-    ionice => {
-       type => 'integer',
-       description => "Set CFQ ionice priority.",
-       optional => 1,
-       minimum => 0,
-       maximum => 8,
-    },
-    lockwait => {
-       type => 'integer',
-       description => "Maximal time to wait for the global lock (minutes).",
-       optional => 1,
-       minimum => 0,
-    },
-    stopwait => {
-       type => 'integer',
-       description => "Maximal time to wait until a VM is stopped (minutes).",
-       optional => 1,
-       minimum => 0,
-    },
-    maxfiles => {
-       type => 'integer',
-       description => "Maximal number of backup files per VM.",
-       optional => 1,
-       minimum => 1,
-    },
-    remove => {
-       type => 'boolean',
-       description => "Remove old backup files if there are more than 'maxfiles' backup files.",
-       optional => 1,
-       default => 1,
-    },
-};
 
 sub option_exists {
     my $key = shift;
     return defined($confdesc->{$key});
 }
 
-# add JSON properties for create and set function
-sub json_config_properties {
-    my $prop = shift;
-
-    foreach my $opt (keys %$confdesc) {
-       $prop->{$opt} = $confdesc->{$opt};
-    }
-
-    return $prop;
-}
-
 sub verify_vzdump_parameters {
     my ($param, $check_missing) = @_;
 
@@ -1232,38 +1120,78 @@ sub verify_vzdump_parameters {
     raise_param_exc({ exclude => "option conflicts with option 'vmid'"})
        if $param->{exclude} && $param->{vmid};
 
-    $param->{all} = 1 if defined($param->{exclude});
+    raise_param_exc({ pool => "option conflicts with option 'vmid'"})
+       if $param->{pool} && $param->{vmid};
+
+    $param->{all} = 1 if (defined($param->{exclude}) && !$param->{pool});
+
+    warn "option 'size' is deprecated and will be removed in a future " .
+        "release, please update your script/configuration!\n"
+       if defined($param->{size});
 
     return if !$check_missing;
 
     raise_param_exc({ vmid => "property is missing"})
-       if !$param->{all} && !$param->{vmid};
+       if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid};
 
 }
 
-sub command_line {
-    my ($param) = @_;
+sub stop_running_backups {
+    my($self) = @_;
+
+    my $upid = PVE::Tools::file_read_firstline($pidfile);
+    return if !$upid;
 
-    my $cmd = "vzdump";
+    my $task = PVE::Tools::upid_decode($upid);
 
-    if ($param->{vmid}) {
-       $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
+    if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) &&
+       PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) {
+       kill(15, $task->{pid});
+       # wait max 15 seconds to shut down (else, do nothing for now)
+       my $i;
+       for ($i = 15; $i > 0; $i--) {
+           last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart}));
+           sleep (1);
+       }
+       die "stopping backup process $task->{pid} failed\n" if $i == 0;
     }
+}
 
-    foreach my $p (keys %$param) {
-       next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' || $p eq 'dow' || $p eq 'stdout';
-       my $v = $param->{$p};
-       my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
-       if ($p eq 'exclude-path') {
-           foreach my $path (split(/\0/, $v || '')) {
-               $cmd .= " --$p " . PVE::Tools::shellquote($path);
-           }
-       } else {
-           $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
+sub get_included_guests {
+    my ($job) = @_;
+
+    my $nodename = PVE::INotify::nodename();
+    my $vmids = [];
+    my $vmids_per_node = {};
+
+    my $vmlist = PVE::Cluster::get_vmlist();
+
+    if ($job->{pool}) {
+       $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool});
+    } elsif ($job->{vmid}) {
+       $vmids = [ split_list($job->{vmid}) ];
+    } elsif ($job->{all}) {
+       # all or exclude
+       my $exclude = check_vmids(split_list($job->{exclude}));
+       my $excludehash = { map { $_ => 1 } @$exclude };
+
+       for my $id (keys %{$vmlist->{ids}}) {
+           next if $excludehash->{$id};
+           push @$vmids, $id;
        }
+    } else {
+       return $vmids_per_node;
+    }
+    $vmids = check_vmids(@$vmids);
+
+    for my $vmid (@$vmids) {
+       my $node = $vmlist->{ids}->{$vmid}->{node};
+       next if (defined $job->{node} && $job->{node} ne $node);
+
+       push @{$vmids_per_node->{$node}}, $vmid;
     }
 
-    return $cmd;
+    return $vmids_per_node;
 }
 
 1;