]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/VZDump.pm
vzdump: make guest include logic testable
[pve-manager.git] / PVE / VZDump.pm
index 7fc69f987b32db5a4d450468d58927858f1cab56..bdbf641e7308a01fd35972f6c97500015d728bd1 100644 (file)
@@ -2,182 +2,37 @@ package PVE::VZDump;
 
 use strict;
 use warnings;
+
 use Fcntl ':flock';
-use PVE::Exception qw(raise_param_exc);
+use File::Path;
 use IO::File;
 use IO::Select;
 use IPC::Open3;
-use File::Path;
-use PVE::RPCEnvironment;
-use PVE::Storage;
-use PVE::Cluster qw(cfs_read_file);
-use Time::localtime;
+use POSIX qw(strftime);
 use Time::Local;
-use PVE::JSONSchema qw(get_standard_option);
-use PVE::HA::Env::PVE2;
+
+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);
+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();
 
-my $confdesc = {
-    vmid => {
-       type => 'string', format => 'pve-vmid-list',
-       description => "The ID of the guest system you want to backup.",
-       completion => \&PVE::Cluster::complete_local_vmid,
-       optional => 1,
-    },
-    node => get_standard_option('pve-node', {
-       description => "Only run if executed on this node.",
-       completion => \&PVE::Cluster::get_nodelist,
-       optional => 1,
-    }),
-    all => {
-       type => 'boolean',
-       description => "Backup all known guest systems on this host.",
-       optional => 1,
-       default => 0,
-    },
-    stdexcludes => {
-       type => 'boolean',
-       description => "Exclude temporary files and logs.",
-       optional => 1,
-       default => 1,
-    },
-    compress => {
-       type => 'string',
-       description => "Compress dump file.",
-       optional => 1,
-       enum => ['0', '1', 'gzip', 'lzo'],
-       default => '0',
-    },
-    pigz=> {
-       type => "integer",
-       description => "Use pigz instead of gzip when N>0.".
-           " N=1 uses half of cores, N>1 uses N as thread count.",
-       optional => 1,
-       default => 0,
-    },
-    quiet => {
-       type => 'boolean',
-       description => "Be quiet.",
-       optional => 1,
-       default => 0,
-    },
-    mode => {
-       type => 'string',
-       description => "Backup mode.",
-       optional => 1,
-       default => 'snapshot',
-       enum => [ 'snapshot', 'suspend', 'stop' ],
-    },
-    exclude => {
-       type => 'string', format => 'pve-vmid-list',
-       description => "Exclude specified guest systems (assumes --all)",
-       optional => 1,
-    },
-    'exclude-path' => {
-       type => 'string', format => 'string-alist',
-       description => "Exclude certain files/directories (shell globs).",
-       optional => 1,
-    },
-    mailto => {
-       type => 'string', format => 'string-list',
-       description => "Comma-separated list of email addresses that should" .
-           " receive email notifications.",
-       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.",
-       completion => \&complete_backup_storage,
-       optional => 1,
-    }),
-    stop => {
-       type => 'boolean',
-       description => "Stop runnig backup jobs on this host.",
-       optional => 1,
-       default => 0,
-    },
-    size => {
-       type => 'integer',
-       description => "Unused, will be removed in a future release.",
-       optional => 1,
-       minimum => 500,
-       default => 1024,
-    },
-    bwlimit => {
-       type => 'integer',
-       description => "Limit I/O bandwidth (KBytes per second).",
-       optional => 1,
-       minimum => 0,
-       default => 0,
-    },
-    ionice => {
-       type => 'integer',
-       description => "Set CFQ ionice priority.",
-       optional => 1,
-       minimum => 0,
-       maximum => 8,
-       default => 7,
-    },
-    lockwait => {
-       type => 'integer',
-       description => "Maximal time to wait for the global lock (minutes).",
-       optional => 1,
-       minimum => 0,
-       default => 3*60, # 3 hours
-    },
-    stopwait => {
-       type => 'integer',
-       description => "Maximal time to wait until a guest system is stopped (minutes).",
-       optional => 1,
-       minimum => 0,
-       default => 10, # 10 minutes
-    },
-    maxfiles => {
-       type => 'integer',
-       description => "Maximal number of backup files per guest system.",
-       optional => 1,
-       minimum => 1,
-       default => 1,
-    },
-    remove => {
-       type => 'boolean',
-       description => "Remove old backup files if there are more than 'maxfiles' backup files.",
-       optional => 1,
-       default => 1,
-    },
-};
+my $confdesc = PVE::VZDump::Common::get_confdesc();
 
 # Load available plugins
 my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
@@ -220,19 +75,28 @@ sub storage_info {
     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" 
+
+    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'));
-    die "can't use storage '$storage' for backups - wrong content type\n" 
+             || $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),
-       maxfiles => $scfg->{maxfiles},
-    };
+    if ($type eq 'pbs') {
+       return {
+           scfg => $scfg,
+           maxfiles => $scfg->{maxfiles},
+           pbs => 1,
+       };
+    } else {
+       return {
+           scfg => $scfg,
+           dumpdir => PVE::Storage::get_backup_dir($cfg, $storage),
+           maxfiles => $scfg->{maxfiles},
+       };
+    }
 }
 
 sub format_size {
@@ -245,13 +109,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 {
@@ -343,6 +209,7 @@ sub read_vzdump_defaults {
     return $res;
 }
 
+use constant MAX_MAIL_SIZE => 1024*1024;
 sub sendmail {
     my ($self, $tasklist, $totaltime, $err, $detail_pre, $detail_post) = @_;
 
@@ -404,23 +271,30 @@ sub sendmail {
        }
     }
 
-    $text .= "\nDetailed backup logs:\n\n";
-    $text .= "$cmdline\n\n";
+    my $text_log_part;
+    $text_log_part .= "\nDetailed backup logs:\n\n";
+    $text_log_part .= "$cmdline\n\n";
 
-    $text .= $detail_pre . "\n" if defined($detail_pre);
+    $text_log_part .= $detail_pre . "\n" if defined($detail_pre);
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        my $log = $task->{tmplog};
        if (!$log) {
-           $text .= "$vmid: no log available\n\n";
+           $text_log_part .= "$vmid: no log available\n\n";
            next;
        }
-       open (TMP, "$log");
-       while (my $line = <TMP>) { $text .= encode8bit ("$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);
-       $text .= "\n";
+       $text_log_part .= "\n";
     }
-    $text .= $detail_post if defined($detail_post);
+    $text_log_part .= $detail_post if defined($detail_post);
 
     # html part
     my $html = "<html><body>\n";
@@ -453,35 +327,54 @@ sub sendmail {
     $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
  format_time ($totaltime), format_size ($ssize));
 
-    $html .= "</table><br><br>\n";
-    $html .= "Detailed backup logs:<br /><br />\n";
-    $html .= "<pre>\n";
-    $html .= escape_html($cmdline) . "\n\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 .= escape_html($detail_pre) . "\n" if defined($detail_pre);
+    $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) {
-           $html .= "$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):/) {
-               $html .= encode8bit ("$vmid: <font color=red>".
-                                      escape_html ($line) . "</font>");
-           } else {
-               $html .= 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);
-       $html .= "\n";
+       $html_log_part .= "\n";
     }
-    $html .= escape_html($detail_post) if defined($detail_post);
-    $html .= "</pre></body></html>\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;
+    }
+
     my $subject = "vzdump backup status ($hostname) : $stat";
 
     my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
@@ -555,6 +448,10 @@ sub new {
        push @{$self->{plugins}}, $pd;
     }
 
+    if (defined($opts->{storage}) && $opts->{stdout}) {
+       die "unable to use option 'storage' with option 'stdout'\n";
+    }
+
     if (!$opts->{dumpdir} && !$opts->{storage}) {
        $opts->{storage} = 'local';
     }
@@ -562,19 +459,18 @@ sub new {
     my $errors = '';
 
     if ($opts->{storage}) {
-       my $info;
-       eval {
-           $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};
+       $maxfiles //= $info->{maxfiles};
     } elsif ($opts->{dumpdir}) {
        $errors .= "dumpdir '$opts->{dumpdir}' does not exist"
            if ! -d $opts->{dumpdir};
     } else {
-       die "internal error"; 
+       die "internal error";
     }
 
     if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
@@ -625,9 +521,9 @@ sub getlock {
     my ($self, $upid) = @_;
 
     my $fh;
-           
+
     my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
+
     die "missimg UPID" if !$upid; # should not happen
 
     if (!open (SERVER_FLCK, ">>$lockfile")) {
@@ -638,15 +534,15 @@ sub getlock {
     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)";
+           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);
 
        eval {
            alarm ($maxwait * 60);
-       
+
            local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
 
            if (!flock (SERVER_FLCK, LOCK_EX)) {
@@ -658,12 +554,12 @@ sub getlock {
            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";
+           debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1);
+           die "can't acquire lock '$lockfile' - $err";
        }
 
        debugmsg('info', "got global lock", undef, 1);
@@ -678,15 +574,17 @@ 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};
@@ -713,10 +611,17 @@ sub compressor_info {
                my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
                $pigz_threads = int(($cpuinfo->{cpus} + 1)/2);
            }
-           return ("pigz -p ${pigz_threads}", 'gz');
+           return ("pigz -p ${pigz_threads} --rsyncable", 'gz');
        } else {
-           return ('gzip', 'gz');
+           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'";
     }
@@ -728,7 +633,7 @@ 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);
            push @$bklist, [$fn, $t];
@@ -737,22 +642,45 @@ sub get_backup_file_list {
 
     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;
@@ -766,26 +694,31 @@ sub exec_backup_task {
                " enabled Service. Use snapshot mode or disable the Service.\n";
        }
 
-       my $vmtype = $plugin->type();
-
        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);
@@ -793,23 +726,29 @@ sub exec_backup_task {
            $ext .= ".${comp_ext}";
        }
 
-       if ($opts->{stdout}) {
-           $task->{tarfile} = '-';
+       if ($self->{opts}->{pbs}) {
+           die "unable to pipe backup to stdout\n" if $opts->{stdout};
        } else {
-           my $tarfile = $task->{tarfile} = "$opts->{dumpdir}/$basename$ext";
-           $task->{tmptar} = $task->{tarfile};
-           $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
-           unlink $task->{tmptar};
+           if ($opts->{stdout}) {
+               $task->{tarfile} = '-';
+           } else {
+               my $tarfile = $task->{tarfile} = "$opts->{dumpdir}/$basename$ext";
+               $task->{tmptar} = $task->{tarfile};
+               $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 {
@@ -829,11 +768,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);
 
@@ -859,7 +800,7 @@ 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;
            }
        }
 
@@ -882,12 +823,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') {
 
@@ -901,7 +842,7 @@ sub exec_backup_task {
            }
 
            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;
@@ -915,10 +856,9 @@ sub exec_backup_task {
                $self->run_hook_script('pre-restart', $task, $logfd);
                $plugin->resume_vm($task, $vmid);
                $self->run_hook_script('post-restart', $task, $logfd);
-               my $delay = time () - $vmstoptime;
-               debugmsg('info', "vm is online again after $delay seconds", $logfd);
+               $log_vm_online_again->();
            }
-           
+
        } elsif ($mode eq 'snapshot') {
 
            $self->run_hook_script ('backup-start', $task, $logfd);
@@ -929,7 +869,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;
            }
@@ -942,8 +882,7 @@ 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);
@@ -954,8 +893,8 @@ sub exec_backup_task {
 
        # assemble archive image
        $plugin->assemble ($task, $vmid);
-       
-       # produce archive 
+
+       # produce archive
 
        if ($opts->{stdout}) {
            debugmsg ('info', "sending archive to stdout", $logfd);
@@ -964,30 +903,47 @@ sub exec_backup_task {
            return;
        }
 
-       debugmsg ('info', "creating archive '$task->{tarfile}'", $logfd);
+       # fixme: ??
+       if ($self->{opts}->{pbs}) {
+           debugmsg ('info', "creating pbs archive on storage '$opts->{storage}'", $logfd);
+       } else {
+           debugmsg ('info', "creating archive '$task->{tarfile}'", $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}) {
+           # fixme: log size ?
+           debugmsg ('info', "pbs upload finished", $logfd);
+       } else {
+           rename ($task->{tmptar}, $task->{tarfile}) ||
+               die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\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->{tarfile}) || 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->{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)(\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?))$/\.log/;
+                   unlink $logfn;
+               }
            }
        }
 
@@ -1012,8 +968,8 @@ sub exec_backup_task {
        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);
@@ -1031,8 +987,7 @@ sub exec_backup_task {
            if ($err) {
                warn $err;
            } else {
-               my $delay = time () - $vmstoptime;
-               debugmsg ('info', "vm is online again after $delay seconds", $logfd);
+               $log_vm_online_again->();
            }
        }
     }
@@ -1049,6 +1004,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); };
 
@@ -1056,12 +1012,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'} '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); };
@@ -1077,7 +1042,7 @@ 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 = [];
 
     if ($opts->{all}) {
@@ -1147,7 +1112,7 @@ sub exec_backup {
 
     die $err if $err;
 
-    die "job errors\n" if $errcount; 
+    die "job errors\n" if $errcount;
 
     unlink $pidfile;
 }
@@ -1158,17 +1123,6 @@ sub option_exists {
     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) = @_;
 
@@ -1178,7 +1132,10 @@ 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"
@@ -1187,7 +1144,7 @@ sub verify_vzdump_parameters {
     return if !$check_missing;
 
     raise_param_exc({ vmid => "property is missing"})
-       if !($param->{all} || $param->{stop}) && !$param->{vmid};
+       if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid};
 
 }
 
@@ -1199,7 +1156,7 @@ sub stop_running_backups {
 
     my $task = PVE::Tools::upid_decode($upid);
 
-    if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) && 
+    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)
@@ -1208,53 +1165,43 @@ sub stop_running_backups {
            last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart}));
            sleep (1);
        }
-       die "stoping backup process $task->{pid} failed\n" if $i == 0;
+       die "stopping backup process $task->{pid} failed\n" if $i == 0;
     }
 }
 
-sub command_line {
-    my ($param) = @_;
+sub get_included_guests {
+    my ($job) = @_;
 
-    my $cmd = "vzdump";
+    my $nodename = PVE::INotify::nodename();
+    my $vmids = [];
 
-    if ($param->{vmid}) {
-       $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
+    # convert string lists to arrays
+    if ($job->{pool}) {
+       $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool});
+    } else {
+       $vmids = [ PVE::Tools::split_list(extract_param($job, 'vmid')) ];
     }
 
-    foreach my $p (keys %$param) {
-       next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
-               $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
-       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);
+    my $skiplist = [];
+    if (!$job->{all}) {
+       if (!$job->{node} || $job->{node} eq $nodename) {
+           my $vmlist = PVE::Cluster::get_vmlist();
+           my $localvmids = [];
+           foreach my $vmid (@{$vmids}) {
+               my $d = $vmlist->{ids}->{$vmid};
+               if ($d && ($d->{node} ne $nodename)) {
+                   push @{$skiplist}, $vmid;
+               } else {
+                   push @{$localvmids}, $vmid;
+               }
            }
-       } else {
-           $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
+           $vmids = $localvmids;
        }
-    }
-
-    return $cmd;
-}
-
-# bash completion helpers
-sub complete_backup_storage {
 
-    my $cfg = PVE::Storage::config();
-    my $ids = $cfg->{ids};
-
-    my $nodename = PVE::INotify::nodename();
-
-    my $res = [];
-    foreach my $sid (keys %$ids) {
-       my $scfg = $ids->{$sid};
-       next if !PVE::Storage::storage_check_enabled($cfg, $sid, $nodename, 1);
-       next if !$scfg->{content}->{backup};
-       push @$res, $sid;
+       $job->{vmids} = PVE::VZDump::check_vmids(@{$vmids})
     }
 
-    return $res;
+    return ($vmids, $skiplist);
 }
 
 1;