]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/VZDump.pm
fix #949: add post-restart hook to vzdump
[pve-manager.git] / PVE / VZDump.pm
index 937d896cf119a8896111e720487a91f4d3896f1b..3101b6a25e6896fada26d86556b8ce2b743d49ea 100644 (file)
@@ -32,7 +32,7 @@ my @plugins = qw();
 my $confdesc = {
     vmid => {
        type => 'string', format => 'pve-vmid-list',
-       description => "The ID of the VM you want to backup.",
+       description => "The ID of the guest system you want to backup.",
        completion => \&PVE::Cluster::complete_local_vmid,
        optional => 1,
     },
@@ -43,13 +43,13 @@ my $confdesc = {
     }),
     all => {
        type => 'boolean',
-       description => "Backup all known VMs on this host.",
+       description => "Backup all known guest systems on this host.",
        optional => 1,
        default => 0,
     },
     stdexcludes => {
        type => 'boolean',
-       description => "Exclude temorary files and logs.",
+       description => "Exclude temporary files and logs.",
        optional => 1,
        default => 1,
     },
@@ -58,11 +58,11 @@ my $confdesc = {
        description => "Compress dump file.",
        optional => 1,
        enum => ['0', '1', 'gzip', 'lzo'],
-       default => 'lzo',
+       default => '0',
     },
     pigz=> {
        type => "integer",
-       description => "Uses pigz instead of gzip when N>0.".
+       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,
@@ -77,22 +77,23 @@ my $confdesc = {
        type => 'string',
        description => "Backup mode.",
        optional => 1,
-       default => 'stop',
+       default => 'snapshot',
        enum => [ 'snapshot', 'suspend', 'stop' ],
     },
     exclude => {
        type => 'string', format => 'pve-vmid-list',
-       description => "exclude specified VMs (assumes --all)",
+       description => "Exclude specified guest systems (assumes --all)",
        optional => 1,
     },
     'exclude-path' => {
        type => 'string', format => 'string-alist',
-       description => "exclude certain files/directories (shell globs).",
+       description => "Exclude certain files/directories (shell globs).",
        optional => 1,
     },
     mailto => {
        type => 'string', format => 'string-list',
-       description => "",
+       description => "Comma-separated list of email addresses that should" .
+           " receive email notifications.",
        optional => 1,
     },
     mailnotification => {
@@ -130,15 +131,17 @@ my $confdesc = {
     },
     size => {
        type => 'integer',
-       description => "LVM snapshot size in MB.",
+       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',
@@ -146,24 +149,28 @@ my $confdesc = {
        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 VM is stopped (minutes).",
+       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 VM.",
+       description => "Maximal number of backup files per guest system.",
        optional => 1,
        minimum => 1,
+       default => 1,
     },
     remove => {
        type => 'boolean',
@@ -230,7 +237,7 @@ 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};
  
@@ -328,14 +335,10 @@ sub read_vzdump_defaults {
     my $fn = "/etc/vzdump.conf";
 
     my $defaults = {
-       bwlimit => 0,
-       ionice => 7,
-       size => 1024,
-       lockwait => 3*60, # 3 hours
-       stopwait => 10, # 10 minutes
-       mode => 'snapshot',
-       maxfiles => 1, 
-       pigz => 0,
+       map {
+           my $default = $confdesc->{$_}->{default};
+            defined($default) ? ($_ => $default) : ()
+       } keys %$confdesc
     };
 
     my $raw;
@@ -347,9 +350,13 @@ sub read_vzdump_defaults {
     if (my $excludes = $res->{'exclude-path'}) {
        $res->{'exclude-path'} = PVE::Tools::split_args($excludes);
     }
+    if (defined($res->{mailto})) {
+       my @mailto = PVE::Tools::split_list($res->{mailto});
+       $res->{mailto} = [ @mailto ];
+    }
 
     foreach my $key (keys %$defaults) {
-       $res->{$key} = $defaults->{$key} if !$res->{$key};
+       $res->{$key} = $defaults->{$key} if !defined($res->{$key});
     }
 
     return $res;
@@ -389,91 +396,45 @@ sub sendmail {
     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 = 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});
        } 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";
+
+    $text .= "Detailed backup logs:\n\n";
+    $text .= "$cmdline\n\n";
 
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        my $log = $task->{tmplog};
        if (!$log) {
-           print MAIL "${fill}$vmid: no log available\n\n";
+           $text .= "$vmid: no log available\n\n";
            next;
        }
        open (TMP, "$log");
-       while (my $line = <TMP>) { print MAIL encode8bit ("${fill}$vmid: $line"); }
+       while (my $line = <TMP>) { $text .= encode8bit ("$vmid: $line"); }
        close (TMP);
-       print MAIL "${fill}\n";
+       $text .= "\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";
-
     # 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 .= "<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;
 
@@ -485,56 +446,54 @@ 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}));
        } 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 .= "</table><br><br>\n";
+    $html .= "Detailed backup logs:<br /><br />\n";
+    $html .= "<pre>\n";
+    $html .= escape_html($cmdline) . "\n\n";
 
     foreach my $task (@$tasklist) {
        my $vmid = $task->{vmid};
        my $log = $task->{tmplog};
        if (!$log) {
-           print MAIL "$vmid: no log available\n\n";
+           $html .= "$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>"); 
+               $html .= encode8bit ("$vmid: <font color=red>".
+                                      escape_html ($line) . "</font>");
            } else {
-               print MAIL encode8bit ("$vmid: " . escape_html ($line)); 
+               $html .= encode8bit ("$vmid: " . escape_html ($line));
            }
        }
        close (TMP);
-       print MAIL "\n";
+       $html .= "\n";
     }
-    print MAIL "</pre>\n";
+    $html .= "</pre></body></html>\n";
+    # end html part
 
-    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 {
@@ -565,6 +524,7 @@ sub new {
     $opts->{remove} = 1 if !defined($opts->{remove});
 
     foreach my $k (keys %$defaults) {
+       next if $k eq 'exclude-path'; # dealt with separately
        if ($k eq 'dumpdir' || $k eq 'storage') {
            $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
                !defined ($opts->{storage});
@@ -592,7 +552,7 @@ sub new {
        push @$findexcl, '/var/log/?*',
                         '/tmp/?*',
                         '/var/tmp/?*',
-                        '/var/run/?*';
+                        '/var/run/?*.pid';
     }
 
     foreach my $p (@plugins) {
@@ -627,33 +587,6 @@ sub new {
 
 }
 
-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 {
     my ($dir) = @_;
 
@@ -681,22 +614,6 @@ 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);
-
-    ($vg, $lv) = @{$mapping->{$dev}} if defined $mapping->{$dev};
-
-    return wantarray ? ($dev, $info->{mountpoint}, $vg, $lv, $info->{fstype}) : $dev;
-}
-
 sub getlock {
     my ($self, $upid) = @_;
 
@@ -986,6 +903,7 @@ sub exec_backup_task {
                $cleanup->{resume} = 0;
                $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);
            }
@@ -1017,6 +935,8 @@ sub exec_backup_task {
                debugmsg ('info', "vm is online again after $delay seconds", $logfd);
            }
 
+           $self->run_hook_script ('post-restart', $task, $logfd);
+
        } else {
            die "internal error - unknown mode '$mode'\n";
        }
@@ -1093,7 +1013,8 @@ 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) {
@@ -1239,6 +1160,10 @@ sub verify_vzdump_parameters {
 
     $param->{all} = 1 if defined($param->{exclude});
 
+    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"})