]> git.proxmox.com Git - pve-manager.git/blame - PVE/VZDump.pm
api: backup: require Datastore.Allocate on storage
[pve-manager.git] / PVE / VZDump.pm
CommitLineData
aaeeeebe
DM
1package PVE::VZDump;
2
aaeeeebe
DM
3use strict;
4use warnings;
df2d3636 5
3f488b61 6use Clone;
aaeeeebe 7use Fcntl ':flock';
bbd4cdd8 8use File::Basename;
df2d3636 9use File::Path;
aaeeeebe
DM
10use IO::File;
11use IO::Select;
12use IPC::Open3;
e2b5eb29 13use POSIX qw(strftime);
aaeeeebe 14use Time::Local;
df2d3636
TL
15
16use PVE::Cluster qw(cfs_read_file);
17use PVE::DataCenterConfig;
18use PVE::Exception qw(raise_param_exc);
2de3ee58 19use PVE::HA::Config;
df2d3636
TL
20use PVE::HA::Env::PVE2;
21use PVE::JSONSchema qw(get_standard_option);
22use PVE::RPCEnvironment;
23use PVE::Storage;
2424074e 24use PVE::VZDump::Common;
df2d3636 25use PVE::VZDump::Plugin;
05447e04 26use PVE::Tools qw(extract_param split_list);
5c4da4c3 27use PVE::API2Tools;
aaeeeebe
DM
28
29my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
30
31my $lockfile = '/var/run/vzdump.lock';
8682f6fc 32my $pidfile = '/var/run/vzdump.pid';
aaeeeebe
DM
33my $logdir = '/var/log/vzdump';
34
dafb6246 35my @plugins = qw();
aaeeeebe 36
2424074e 37my $confdesc = PVE::VZDump::Common::get_confdesc();
cc61ea36 38
3f488b61
FE
39my $confdesc_for_defaults = Clone::clone($confdesc);
40delete $confdesc_for_defaults->{$_}->{requires} for qw(notes-template protected);
41
aaeeeebe 42# Load available plugins
8187f6b0
DM
43my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
44foreach my $plug (@pve_vzdump_classes) {
45 my $filename = "/usr/share/perl5/$plug.pm";
46 $filename =~ s!::!/!g;
47 if (-f $filename) {
48 eval { require $filename; };
49 if (!$@) {
50 $plug->import ();
51 push @plugins, $plug;
52 } else {
c76d0106 53 die $@;
8187f6b0
DM
54 }
55 }
aaeeeebe
DM
56}
57
58# helper functions
59
aaeeeebe
DM
60sub debugmsg {
61 my ($mtype, $msg, $logfd, $syslog) = @_;
62
a5c94797 63 PVE::VZDump::Plugin::debugmsg(@_);
aaeeeebe
DM
64}
65
66sub run_command {
67 my ($logfd, $cmdstr, %param) = @_;
68
7f910306 69 my $logfunc = sub {
4a4051d8 70 my $line = shift;
4a4051d8 71 debugmsg ('info', $line, $logfd);
aaeeeebe
DM
72 };
73
7f910306 74 PVE::Tools::run_command($cmdstr, %param, logfunc => $logfunc);
aaeeeebe
DM
75}
76
facf65a6
FE
77my $verify_notes_template = sub {
78 my ($template) = @_;
79
80 die "contains a line feed\n" if $template =~ /\n/;
81
82 my @problematic = ();
83 while ($template =~ /\\(.)/g) {
84 my $char = $1;
85 push @problematic, "escape sequence '\\$char' at char " . (pos($template) - 2)
86 if $char !~ /^[n\\]$/;
87 }
88
89 while ($template =~ /\{\{([^\s{}]+)\}\}/g) {
90 my $var = $1;
91 push @problematic, "variable '$var' at char " . (pos($template) - length($var))
92 if $var !~ /^(cluster|guestname|node|vmid)$/;
93 }
94
95 die "found unknown: " . join(', ', @problematic) . "\n" if scalar(@problematic);
96};
97
e01438a7
FE
98my $generate_notes = sub {
99 my ($notes_template, $task) = @_;
100
facf65a6
FE
101 $verify_notes_template->($notes_template);
102
e01438a7 103 my $info = {
2078e359
FE
104 cluster => PVE::Cluster::get_clinfo()->{cluster}->{name} // 'standalone node',
105 guestname => $task->{hostname} // "VM $task->{vmid}", # is always set for CTs
e01438a7
FE
106 node => PVE::INotify::nodename(),
107 vmid => $task->{vmid},
108 };
109
31213d61
FE
110 my $unescape = sub {
111 my ($char) = @_;
112 return '\\' if $char eq '\\';
113 return "\n" if $char eq 'n';
114 die "unexpected escape character '$char'\n";
e01438a7
FE
115 };
116
31213d61 117 $notes_template =~ s/\\(.)/$unescape->($1)/eg;
e01438a7
FE
118
119 my $vars = join('|', keys $info->%*);
4ed6e1b1 120 $notes_template =~ s/\{\{($vars)\}\}/$info->{$1}/g;
e01438a7
FE
121
122 return $notes_template;
123};
124
93880785
FE
125my sub parse_performance {
126 my ($param) = @_;
127
128 if (defined(my $perf = $param->{performance})) {
129 return if ref($perf) eq 'HASH'; # already parsed
130 $param->{performance} = PVE::JSONSchema::parse_property_string('backup-performance', $perf);
131 }
132}
133
1c56bcf3
FE
134my $parse_prune_backups_maxfiles = sub {
135 my ($param, $kind) = @_;
136
137 my $maxfiles = delete $param->{maxfiles};
138 my $prune_backups = $param->{'prune-backups'};
139
fb6871fc 140 debugmsg('warn', "both 'maxfiles' and 'prune-backups' defined as ${kind} - ignoring 'maxfiles'")
1c56bcf3
FE
141 if defined($maxfiles) && defined($prune_backups);
142
143 if (defined($prune_backups)) {
ecc08c34 144 return if ref($prune_backups) eq 'HASH'; # already parsed
1c56bcf3
FE
145 $param->{'prune-backups'} = PVE::JSONSchema::parse_property_string(
146 'prune-backups',
147 $prune_backups
148 );
149 } elsif (defined($maxfiles)) {
150 if ($maxfiles) {
151 $param->{'prune-backups'} = { 'keep-last' => $maxfiles };
152 } else {
153 $param->{'prune-backups'} = { 'keep-all' => 1 };
154 }
155 }
156};
157
aaeeeebe
DM
158sub storage_info {
159 my $storage = shift;
160
bbcfdc08 161 my $cfg = PVE::Storage::config();
4a4051d8 162 my $scfg = PVE::Storage::storage_config($cfg, $storage);
aaeeeebe 163 my $type = $scfg->{type};
60e049c2 164
60e049c2 165 die "can't use storage '$storage' for backups - wrong content type\n"
aaeeeebe
DM
166 if (!$scfg->{content}->{backup});
167
3a805f8d
FE
168 my $info = {
169 scfg => $scfg,
3a805f8d
FE
170 };
171
e6946086
FE
172 $info->{'prune-backups'} = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'})
173 if defined($scfg->{'prune-backups'});
174
1a87db9e 175 if ($type eq 'pbs') {
3a805f8d 176 $info->{pbs} = 1;
1a87db9e 177 } else {
3a805f8d 178 $info->{dumpdir} = PVE::Storage::get_backup_dir($cfg, $storage);
1a87db9e 179 }
3a805f8d
FE
180
181 return $info;
aaeeeebe
DM
182}
183
184sub format_size {
185 my $size = shift;
186
187 my $kb = $size / 1024;
188
189 if ($kb < 1024) {
190 return int ($kb) . "KB";
191 }
192
193 my $mb = $size / (1024*1024);
aaeeeebe
DM
194 if ($mb < 1024) {
195 return int ($mb) . "MB";
9c3a51ee
TL
196 }
197 my $gb = $mb / 1024;
198 if ($gb < 1024) {
aaeeeebe 199 return sprintf ("%.2fGB", $gb);
60e049c2 200 }
9c3a51ee
TL
201 my $tb = $gb / 1024;
202 return sprintf ("%.2fTB", $tb);
aaeeeebe
DM
203}
204
205sub format_time {
206 my $seconds = shift;
207
208 my $hours = int ($seconds/3600);
209 $seconds = $seconds - $hours*3600;
210 my $min = int ($seconds/60);
211 $seconds = $seconds - $min*60;
212
213 return sprintf ("%02d:%02d:%02d", $hours, $min, $seconds);
214}
215
216sub encode8bit {
217 my ($str) = @_;
218
219 $str =~ s/^(.{990})/$1\n/mg; # reduce line length
220
221 return $str;
222}
223
224sub escape_html {
225 my ($str) = @_;
226
227 $str =~ s/&/&amp;/g;
228 $str =~ s/</&lt;/g;
229 $str =~ s/>/&gt;/g;
230
231 return $str;
232}
233
234sub check_bin {
235 my ($bin) = @_;
236
237 foreach my $p (split (/:/, $ENV{PATH})) {
238 my $fn = "$p/$bin";
239 if (-x $fn) {
240 return $fn;
241 }
242 }
243
244 die "unable to find command '$bin'\n";
245}
246
247sub check_vmids {
248 my (@vmids) = @_;
249
250 my $res = [];
50ba40ec 251 for my $vmid (sort {$a <=> $b} @vmids) {
aaeeeebe
DM
252 die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/;
253 $vmid = int ($vmid); # remove leading zeros
4a4051d8 254 next if !$vmid;
aaeeeebe
DM
255 push @$res, $vmid;
256 }
257
258 return $res;
259}
260
261
262sub read_vzdump_defaults {
263
264 my $fn = "/etc/vzdump.conf";
265
cc61ea36 266 my $defaults = {
1ab98f05
WB
267 map {
268 my $default = $confdesc->{$_}->{default};
269 defined($default) ? ($_ => $default) : ()
3f488b61 270 } keys %$confdesc_for_defaults
aaeeeebe 271 };
dd5d80f5 272 $parse_prune_backups_maxfiles->($defaults, "defaults in VZDump schema");
93880785 273 parse_performance($defaults);
aaeeeebe 274
cc61ea36
TL
275 my $raw;
276 eval { $raw = PVE::Tools::file_get_contents($fn); };
277 return $defaults if $@;
aaeeeebe 278
3f488b61 279 my $conf_schema = { type => 'object', properties => $confdesc_for_defaults };
cc61ea36 280 my $res = PVE::JSONSchema::parse_config($conf_schema, $fn, $raw);
6368bbd4 281 if (my $excludes = $res->{'exclude-path'}) {
d6d3e55b
DC
282 if (ref($excludes) eq 'ARRAY') {
283 my $list = [];
284 for my $path ($excludes->@*) {
6a75aa33
WB
285 # We still use `split_args` here to be compatible with old configs where one line
286 # still has multiple space separated entries.
d6d3e55b
DC
287 push $list->@*, PVE::Tools::split_args($path)->@*;
288 }
289 $res->{'exclude-path'} = $list;
290 } else {
291 $res->{'exclude-path'} = PVE::Tools::split_args($excludes);
292 }
6368bbd4 293 }
74b47fd8 294 if (defined($res->{mailto})) {
05447e04 295 my @mailto = split_list($res->{mailto});
74b47fd8
FG
296 $res->{mailto} = [ @mailto ];
297 }
dd5d80f5 298 $parse_prune_backups_maxfiles->($res, "options in '$fn'");
93880785 299 parse_performance($res);
cc61ea36
TL
300
301 foreach my $key (keys %$defaults) {
e94fa5cb 302 $res->{$key} = $defaults->{$key} if !defined($res->{$key});
aaeeeebe 303 }
aaeeeebe 304
f1dd7629
FE
305 if (defined($res->{storage}) && defined($res->{dumpdir})) {
306 debugmsg('warn', "both 'storage' and 'dumpdir' defined in '$fn' - ignoring 'dumpdir'");
307 delete $res->{dumpdir};
308 }
309
aaeeeebe
DM
310 return $res;
311}
312
ce84a950 313use constant MAX_MAIL_SIZE => 1024*1024;
6ec9de44 314sub sendmail {
44b21574 315 my ($self, $tasklist, $totaltime, $err, $detail_pre, $detail_post) = @_;
aaeeeebe
DM
316
317 my $opts = $self->{opts};
318
319 my $mailto = $opts->{mailto};
320
4a4051d8 321 return if !($mailto && scalar(@$mailto));
aaeeeebe
DM
322
323 my $cmdline = $self->{cmdline};
324
325 my $ecount = 0;
326 foreach my $task (@$tasklist) {
327 $ecount++ if $task->{state} ne 'ok';
328 chomp $task->{msg} if $task->{msg};
329 $task->{backuptime} = 0 if !$task->{backuptime};
330 $task->{size} = 0 if !$task->{size};
6cba1855 331 $task->{target} = 'unknown' if !$task->{target};
aaeeeebe
DM
332 $task->{hostname} = "VM $task->{vmid}" if !$task->{hostname};
333
334 if ($task->{state} eq 'todo') {
335 $task->{msg} = 'aborted';
336 }
337 }
338
02e59759
DM
339 my $notify = $opts->{mailnotification} || 'always';
340 return if (!$ecount && !$err && ($notify eq 'failure'));
8b4b4860 341
6ec9de44 342 my $stat = ($ecount || $err) ? 'backup failed' : 'backup successful';
403761c4
WB
343 if ($err) {
344 if ($err =~ /\n/) {
345 $stat .= ": multiple problems";
346 } else {
347 $stat .= ": $err";
348 $err = undef;
349 }
350 }
aaeeeebe 351
4a4051d8 352 my $hostname = `hostname -f` || PVE::INotify::nodename();
aaeeeebe
DM
353 chomp $hostname;
354
aaeeeebe 355 # text part
403761c4 356 my $text = $err ? "$err\n\n" : '';
2cc64d0e
DJ
357 my $namelength = 20;
358 $text .= sprintf (
359 "%-10s %-${namelength}s %-6s %10s %10s %s\n",
360 qw(VMID NAME STATUS TIME SIZE FILENAME)
361 );
aaeeeebe 362 foreach my $task (@$tasklist) {
2cc64d0e
DJ
363 my $name = substr($task->{hostname}, 0, $namelength);
364 my $successful = $task->{state} eq 'ok';
365 my $size = $successful ? format_size ($task->{size}) : 0;
366 my $filename = $successful ? $task->{target} : '-';
367 my $size_fmt = $successful ? "%10s": "%8.2fMB";
368 $text .= sprintf(
369 "%-10s %-${namelength}s %-6s %10s $size_fmt %s\n",
370 $task->{vmid},
371 $name,
372 $task->{state},
373 format_time($task->{backuptime}),
374 $size,
375 $filename,
376 );
aaeeeebe 377 }
7a0a8e67 378
ce84a950
DJ
379 my $text_log_part;
380 $text_log_part .= "\nDetailed backup logs:\n\n";
381 $text_log_part .= "$cmdline\n\n";
aaeeeebe 382
ce84a950 383 $text_log_part .= $detail_pre . "\n" if defined($detail_pre);
aaeeeebe
DM
384 foreach my $task (@$tasklist) {
385 my $vmid = $task->{vmid};
386 my $log = $task->{tmplog};
387 if (!$log) {
ce84a950 388 $text_log_part .= "$vmid: no log available\n\n";
aaeeeebe
DM
389 next;
390 }
5620e576
TL
391 if (open (my $TMP, '<', "$log")) {
392 while (my $line = <$TMP>) {
ce84a950
DJ
393 next if $line =~ /^status: \d+/; # not useful in mails
394 $text_log_part .= encode8bit ("$vmid: $line");
395 }
5620e576 396 close ($TMP);
ce84a950
DJ
397 } else {
398 $text_log_part .= "$vmid: Could not open log file\n\n";
399 }
ce84a950 400 $text_log_part .= "\n";
aaeeeebe 401 }
ce84a950 402 $text_log_part .= $detail_post if defined($detail_post);
aaeeeebe 403
aaeeeebe 404 # html part
7a0a8e67 405 my $html = "<html><body>\n";
403761c4 406 $html .= "<p>" . (escape_html($err) =~ s/\n/<br>/gr) . "</p>\n" if $err;
7a0a8e67
TL
407 $html .= "<table border=1 cellpadding=3>\n";
408 $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
aaeeeebe
DM
409
410 my $ssize = 0;
aaeeeebe
DM
411 foreach my $task (@$tasklist) {
412 my $vmid = $task->{vmid};
413 my $name = $task->{hostname};
414
415 if ($task->{state} eq 'ok') {
aaeeeebe
DM
416 $ssize += $task->{size};
417
1353489e
TL
418 $html .= sprintf (
419 "<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
420 $vmid,
421 $name,
422 format_time($task->{backuptime}),
423 format_size ($task->{size}),
424 escape_html ($task->{target}),
425 );
aaeeeebe 426 } else {
1353489e
TL
427 $html .= sprintf (
428 "<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
429 $vmid,
430 $name,
431 format_time($task->{backuptime}),
432 escape_html ($task->{msg}),
433 );
aaeeeebe
DM
434 }
435 }
436
7a0a8e67 437 $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
aaeeeebe
DM
438 format_time ($totaltime), format_size ($ssize));
439
ce84a950
DJ
440 $html .= "\n</table><br><br>\n";
441 my $html_log_part;
442 $html_log_part .= "Detailed backup logs:<br /><br />\n";
443 $html_log_part .= "<pre>\n";
444 $html_log_part .= escape_html($cmdline) . "\n\n";
aaeeeebe 445
ce84a950 446 $html_log_part .= escape_html($detail_pre) . "\n" if defined($detail_pre);
aaeeeebe
DM
447 foreach my $task (@$tasklist) {
448 my $vmid = $task->{vmid};
449 my $log = $task->{tmplog};
450 if (!$log) {
ce84a950 451 $html_log_part .= "$vmid: no log available\n\n";
aaeeeebe
DM
452 next;
453 }
5620e576
TL
454 if (open (my $TMP, '<', "$log")) {
455 while (my $line = <$TMP>) {
ce84a950
DJ
456 next if $line =~ /^status: \d+/; # not useful in mails
457 if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
458 $html_log_part .= encode8bit ("$vmid: <font color=red>".
459 escape_html ($line) . "</font>");
460 } else {
461 $html_log_part .= encode8bit ("$vmid: " . escape_html ($line));
462 }
aaeeeebe 463 }
5620e576 464 close ($TMP);
ce84a950
DJ
465 } else {
466 $html_log_part .= "$vmid: Could not open log file\n\n";
aaeeeebe 467 }
ce84a950 468 $html_log_part .= "\n";
aaeeeebe 469 }
ce84a950
DJ
470 $html_log_part .= escape_html($detail_post) if defined($detail_post);
471 $html_log_part .= "</pre>";
213e0eb2 472 my $html_end = "\n</body></html>\n";
7a0a8e67 473 # end html part
aaeeeebe 474
ce84a950 475 if (length($text) + length($text_log_part) +
213e0eb2
FE
476 length($html) + length($html_log_part) +
477 length($html_end) < MAX_MAIL_SIZE)
ce84a950
DJ
478 {
479 $html .= $html_log_part;
213e0eb2 480 $html .= $html_end;
ce84a950
DJ
481 $text .= $text_log_part;
482 } else {
483 my $msg = "Log output was too long to be sent by mail. ".
484 "See Task History for details!\n";
485 $text .= $msg;
486 $html .= "<p>$msg</p>";
487 $html .= $html_end;
488 }
489
7a0a8e67 490 my $subject = "vzdump backup status ($hostname) : $stat";
aaeeeebe 491
7a0a8e67
TL
492 my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
493 my $mailfrom = $dcconf->{email_from} || "root";
aaeeeebe 494
7a0a8e67 495 PVE::Tools::sendmail($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool");
aaeeeebe
DM
496};
497
498sub new {
a7e42354 499 my ($class, $cmdline, $opts, $skiplist) = @_;
aaeeeebe
DM
500
501 mkpath $logdir;
502
503 check_bin ('cp');
504 check_bin ('df');
505 check_bin ('sendmail');
506 check_bin ('rsync');
507 check_bin ('tar');
508 check_bin ('mount');
509 check_bin ('umount');
510 check_bin ('cstream');
511 check_bin ('ionice');
512
47664cbe 513 if ($opts->{mode} && $opts->{mode} eq 'snapshot') {
aaeeeebe
DM
514 check_bin ('lvcreate');
515 check_bin ('lvs');
516 check_bin ('lvremove');
517 }
518
519 my $defaults = read_vzdump_defaults();
520
521 foreach my $k (keys %$defaults) {
acc963c3 522 next if $k eq 'exclude-path' || $k eq 'prune-backups'; # dealt with separately
aaeeeebe
DM
523 if ($k eq 'dumpdir' || $k eq 'storage') {
524 $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
525 !defined ($opts->{storage});
526 } else {
527 $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k});
528 }
529 }
530
aaeeeebe
DM
531 $opts->{dumpdir} =~ s|/+$|| if ($opts->{dumpdir});
532 $opts->{tmpdir} =~ s|/+$|| if ($opts->{tmpdir});
533
a7e42354 534 $skiplist = [] if !$skiplist;
94009776
TL
535 my $self = bless {
536 cmdline => $cmdline,
537 opts => $opts,
538 skiplist => $skiplist,
539 }, $class;
aaeeeebe 540
45f16a06 541 my $findexcl = $self->{findexcl} = [];
f4a8bab4 542 if ($defaults->{'exclude-path'}) {
45f16a06 543 push @$findexcl, @{$defaults->{'exclude-path'}};
f4a8bab4
DM
544 }
545
aaeeeebe 546 if ($opts->{'exclude-path'}) {
45f16a06 547 push @$findexcl, @{$opts->{'exclude-path'}};
aaeeeebe
DM
548 }
549
550 if ($opts->{stdexcludes}) {
1353489e
TL
551 push @$findexcl,
552 '/tmp/?*',
553 '/var/tmp/?*',
554 '/var/run/?*.pid',
555 ;
aaeeeebe
DM
556 }
557
558 foreach my $p (@plugins) {
1353489e 559 my $pd = $p->new($self);
aaeeeebe
DM
560
561 push @{$self->{plugins}}, $pd;
aaeeeebe
DM
562 }
563
1a87db9e 564 if (defined($opts->{storage}) && $opts->{stdout}) {
a6fcd7d9
FE
565 die "cannot use options 'storage' and 'stdout' at the same time\n";
566 } elsif (defined($opts->{storage}) && defined($opts->{dumpdir})) {
567 die "cannot use options 'storage' and 'dumpdir' at the same time\n";
1a87db9e
DM
568 }
569
aaeeeebe 570 if (!$opts->{dumpdir} && !$opts->{storage}) {
19d5c0f2 571 $opts->{storage} = 'local';
aaeeeebe
DM
572 }
573
3f488b61
FE
574 # Enforced by the API too, but these options might come in via defaults. Drop them if necessary.
575 if (!$opts->{storage}) {
576 delete $opts->{$_} for qw(notes-template protected);
577 }
578
403761c4 579 my $errors = '';
3c5a7616
FE
580 my $add_error = sub {
581 my ($error) = @_;
582 $errors .= "\n" if $errors;
583 chomp($error);
584 $errors .= $error;
585 };
403761c4 586
c527d28f
FE
587 eval {
588 $self->{job_init_log} = '';
589 open my $job_init_fd, '>', \$self->{job_init_log};
590 $self->run_hook_script('job-init', undef, $job_init_fd);
591 close $job_init_fd;
592
593 PVE::Cluster::cfs_update(); # Pick up possible changes made by the hook script.
594 };
595 $add_error->($@) if $@;
596
aaeeeebe 597 if ($opts->{storage}) {
164651da
FE
598 my $storage_cfg = PVE::Storage::config();
599 eval { PVE::Storage::activate_storage($storage_cfg, $opts->{storage}) };
3c5a7616 600 $add_error->("could not activate storage '$opts->{storage}': $@") if $@;
164651da 601
1e4583d5 602 my $info = eval { storage_info ($opts->{storage}) };
e6946086 603 if (my $err = $@) {
3c5a7616 604 $add_error->("could not get storage information for '$opts->{storage}': $err");
e6946086
FE
605 } else {
606 $opts->{dumpdir} = $info->{dumpdir};
607 $opts->{scfg} = $info->{scfg};
608 $opts->{pbs} = $info->{pbs};
acc963c3 609 $opts->{'prune-backups'} //= $info->{'prune-backups'};
e6946086 610 }
aaeeeebe 611 } elsif ($opts->{dumpdir}) {
3c5a7616 612 $add_error->("dumpdir '$opts->{dumpdir}' does not exist")
aaeeeebe
DM
613 if ! -d $opts->{dumpdir};
614 } else {
60e049c2 615 die "internal error";
aaeeeebe
DM
616 }
617
acc963c3 618 $opts->{'prune-backups'} //= $defaults->{'prune-backups'};
5b6b72e6 619
1db8529e
FE
620 # avoid triggering any remove code path if keep-all is set
621 $opts->{remove} = 0 if $opts->{'prune-backups'}->{'keep-all'};
622
aaeeeebe 623 if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
3c5a7616 624 $add_error->("tmpdir '$opts->{tmpdir}' does not exist");
403761c4
WB
625 }
626
627 if ($errors) {
628 eval { $self->sendmail([], 0, $errors); };
629 debugmsg ('err', $@) if $@;
630 die "$errors\n";
aaeeeebe
DM
631 }
632
633 return $self;
aaeeeebe
DM
634}
635
aaeeeebe
DM
636sub get_mount_info {
637 my ($dir) = @_;
638
8572d646
DM
639 # Note: df 'available' can be negative, and percentage set to '-'
640
d93d0459 641 my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir];
aaeeeebe 642
d93d0459 643 my $res;
aaeeeebe 644
d93d0459
DM
645 my $parser = sub {
646 my $line = shift;
8572d646
DM
647 if (my ($fsid, $fstype, undef, $mp) = $line =~
648 m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) {
d93d0459
DM
649 $res = {
650 device => $fsid,
651 fstype => $fstype,
652 mountpoint => $mp,
653 };
654 }
aaeeeebe 655 };
d93d0459
DM
656
657 eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); };
658 warn $@ if $@;
659
660 return $res;
aaeeeebe
DM
661}
662
aaeeeebe 663sub getlock {
eab837c4 664 my ($self, $upid) = @_;
aaeeeebe 665
8682f6fc 666 my $fh;
60e049c2 667
aaeeeebe 668 my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
60e049c2 669
dc6e0a52 670 die "missing UPID" if !$upid; # should not happen
eab837c4 671
5620e576
TL
672 my $SERVER_FLCK;
673 if (!open ($SERVER_FLCK, '>>', "$lockfile")) {
aaeeeebe 674 debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1);
6ec9de44 675 die "can't open lock on file '$lockfile' - $!";
aaeeeebe
DM
676 }
677
5620e576 678 if (!flock ($SERVER_FLCK, LOCK_EX|LOCK_NB)) {
eab837c4 679 if (!$maxwait) {
76189130
TL
680 debugmsg ('err', "can't acquire lock '$lockfile' (wait = 0)", undef, 1);
681 die "can't acquire lock '$lockfile' (wait = 0)";
eab837c4 682 }
aaeeeebe 683
eab837c4 684 debugmsg('info', "trying to get global lock - waiting...", undef, 1);
eab837c4
DM
685 eval {
686 alarm ($maxwait * 60);
60e049c2 687
eab837c4 688 local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
aaeeeebe 689
5620e576 690 if (!flock ($SERVER_FLCK, LOCK_EX)) {
eab837c4 691 my $err = $!;
5620e576 692 close ($SERVER_FLCK);
eab837c4
DM
693 alarm (0);
694 die "$err\n";
695 }
aaeeeebe 696 alarm (0);
eab837c4 697 };
aaeeeebe 698 alarm (0);
60e049c2 699
eab837c4 700 my $err = $@;
60e049c2 701
eab837c4 702 if ($err) {
76189130
TL
703 debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1);
704 die "can't acquire lock '$lockfile' - $err";
eab837c4 705 }
aaeeeebe 706
eab837c4 707 debugmsg('info', "got global lock", undef, 1);
aaeeeebe
DM
708 }
709
eab837c4 710 PVE::Tools::file_set_contents($pidfile, $upid);
875c2e5a
FE
711
712 return $SERVER_FLCK;
aaeeeebe
DM
713}
714
715sub run_hook_script {
716 my ($self, $phase, $task, $logfd) = @_;
717
718 my $opts = $self->{opts};
719
720 my $script = $opts->{script};
aaeeeebe
DM
721 return if !$script;
722
0ac20586
TL
723 die "Error: The hook script '$script' does not exist.\n" if ! -f $script;
724 die "Error: The hook script '$script' is not executable.\n" if ! -x $script;
59798893 725
58f763e1 726 my $cmd = [$script, $phase];
aaeeeebe 727
58f763e1
FG
728 if ($task) {
729 push @$cmd, $task->{mode};
730 push @$cmd, $task->{vmid};
731 }
aaeeeebe
DM
732
733 local %ENV;
28272ca2
DM
734 # set immutable opts directly (so they are available in all phases)
735 $ENV{STOREID} = $opts->{storage} if $opts->{storage};
736 $ENV{DUMPDIR} = $opts->{dumpdir} if $opts->{dumpdir};
737
6cba1855 738 foreach my $ek (qw(vmtype hostname target logfile)) {
aaeeeebe
DM
739 $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
740 }
741
742 run_command ($logfd, $cmd);
743}
744
d7550e09 745sub compressor_info {
778d5b6d
TL
746 my ($opts) = @_;
747 my $opt_compress = $opts->{compress};
d7550e09
DM
748
749 if (!$opt_compress || $opt_compress eq '0') {
750 return undef;
751 } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
752 return ('lzop', 'lzo');
753 } elsif ($opt_compress eq 'gzip') {
778d5b6d 754 if ($opts->{pigz} > 0) {
8555b100
TL
755 my $pigz_threads = $opts->{pigz};
756 if ($pigz_threads == 1) {
757 my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
758 $pigz_threads = int(($cpuinfo->{cpus} + 1)/2);
759 }
819e4ff5 760 return ("pigz -p ${pigz_threads} --rsyncable", 'gz');
778d5b6d 761 } else {
e953f92a 762 return ('gzip --rsyncable', 'gz');
778d5b6d 763 }
3e2c5105
AA
764 } elsif ($opt_compress eq 'zstd') {
765 my $zstd_threads = $opts->{zstd} // 1;
766 if ($zstd_threads == 0) {
767 my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
768 $zstd_threads = int(($cpuinfo->{cpus} + 1)/2);
769 }
9a023d55 770 return ("zstd --threads=${zstd_threads}", 'zst');
d7550e09
DM
771 } else {
772 die "internal error - unknown compression option '$opt_compress'";
773 }
774}
899b8373 775
725c0555 776sub get_backup_file_list {
0a9ca6ca 777 my ($dir, $bkname) = @_;
899b8373
DM
778
779 my $bklist = [];
780 foreach my $fn (<$dir/${bkname}-*>) {
de2d177e
FE
781 my $archive_info = eval { PVE::Storage::archive_info($fn) } // {};
782 if ($archive_info->{is_std_name}) {
7bffbd22 783 my $path = "$dir/$archive_info->{filename}";
de2d177e 784 my $backup = {
7bffbd22 785 'path' => $path,
de2d177e
FE
786 'ctime' => $archive_info->{ctime},
787 };
725c0555
FG
788 $backup->{mark} = "protected"
789 if -e PVE::Storage::protection_file_path($path);
de2d177e 790 push @{$bklist}, $backup;
899b8373
DM
791 }
792 }
793
794 return $bklist;
795}
60e049c2 796
aaeeeebe
DM
797sub exec_backup_task {
798 my ($self, $task) = @_;
60e049c2 799
aaeeeebe
DM
800 my $opts = $self->{opts};
801
e6946086 802 my $cfg = PVE::Storage::config();
aaeeeebe
DM
803 my $vmid = $task->{vmid};
804 my $plugin = $task->{plugin};
1a87db9e
DM
805
806 $task->{backup_time} = time();
807
808 my $pbs_group_name;
809 my $pbs_snapshot_name;
810
aaeeeebe 811 my $vmstarttime = time ();
60e049c2 812
aaeeeebe
DM
813 my $logfd;
814
815 my $cleanup = {};
816
4e0947c8
TL
817 my $log_vm_online_again = sub {
818 return if !defined($task->{vmstoptime});
819 $task->{vmconttime} //= time();
820 my $delay = $task->{vmconttime} - $task->{vmstoptime};
310cde8b 821 $delay = '<1' if $delay < 1;
4e0947c8
TL
822 debugmsg ('info', "guest is online again after $delay seconds", $logfd);
823 };
aaeeeebe
DM
824
825 eval {
826 die "unable to find VM '$vmid'\n" if !$plugin;
827
d51f5a1e
FE
828 my $vmtype = $plugin->type();
829
830 if ($self->{opts}->{pbs}) {
831 if ($vmtype eq 'lxc') {
832 $pbs_group_name = "ct/$vmid";
833 } elsif ($vmtype eq 'qemu') {
834 $pbs_group_name = "vm/$vmid";
835 } else {
836 die "pbs backup not implemented for plugin type '$vmtype'\n";
837 }
838 my $btime = strftime("%FT%TZ", gmtime($task->{backup_time}));
839 $pbs_snapshot_name = "$pbs_group_name/$btime";
840 }
841
2de3ee58 842 # for now we deny backups of a running ha managed service in *stop* mode
83ec8f81 843 # as it interferes with the HA stack (started services should not stop).
2de3ee58 844 if ($opts->{mode} eq 'stop' &&
83ec8f81 845 PVE::HA::Config::vm_is_ha_managed($vmid, 'started'))
2de3ee58
TL
846 {
847 die "Cannot execute a backup with stop mode on a HA managed and".
848 " enabled Service. Use snapshot mode or disable the Service.\n";
849 }
850
aaeeeebe
DM
851 my $tmplog = "$logdir/$vmtype-$vmid.log";
852
aaeeeebe 853 my $bkname = "vzdump-$vmtype-$vmid";
1a87db9e 854 my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time}));
aaeeeebe 855
e6946086
FE
856 my $prune_options = $opts->{'prune-backups'};
857
858 my $backup_limit = 0;
b63baa39 859 if (!$prune_options->{'keep-all'}) {
1db8529e
FE
860 foreach my $keep (values %{$prune_options}) {
861 $backup_limit += $keep;
862 }
e6946086 863 }
899b8373 864
bbd4cdd8 865 if (($backup_limit && !$opts->{remove}) || $opts->{protected}) {
1a87db9e 866 my $count;
bbd4cdd8 867 my $protected_count;
7bffbd22 868 if (my $storeid = $opts->{storage}) {
bbd4cdd8
FE
869 my @backups = grep {
870 !$_->{subtype} || $_->{subtype} eq $vmtype
871 } PVE::Storage::volume_list($cfg, $storeid, $vmid, 'backup')->@*;
872
873 $count = grep { !$_->{protected} } @backups;
874 $protected_count = scalar(@backups) - $count;
1a87db9e 875 } else {
725c0555 876 $count = grep { !$_->{mark} || $_->{mark} ne "protected" } get_backup_file_list($opts->{dumpdir}, $bkname)->@*;
1a87db9e 877 }
7bffbd22 878
bbd4cdd8
FE
879 if ($opts->{protected}) {
880 my $max_protected = PVE::Storage::get_max_protected_backups(
881 $opts->{scfg},
882 $opts->{storage},
883 );
884 if ($max_protected > -1 && $protected_count >= $max_protected) {
885 die "The number of protected backups per guest is limited to $max_protected ".
886 "on storage '$opts->{storage}'\n";
887 }
888 } elsif ($count >= $backup_limit) {
889 die "There is a max backup limit of $backup_limit enforced by the target storage ".
890 "or the vzdump parameters. Either increase the limit or delete old backups.\n";
891 }
899b8373
DM
892 }
893
7ddcb3af 894 if (!$self->{opts}->{pbs}) {
1a87db9e
DM
895 $task->{logfile} = "$opts->{dumpdir}/$basename.log";
896 }
aaeeeebe 897
757fd3d5 898 my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar';
778d5b6d 899 my ($comp, $comp_ext) = compressor_info($opts);
d7550e09
DM
900 if ($comp && $comp_ext) {
901 $ext .= ".${comp_ext}";
902 }
aaeeeebe 903
7ddcb3af 904 if ($self->{opts}->{pbs}) {
1a87db9e 905 die "unable to pipe backup to stdout\n" if $opts->{stdout};
b3c3304f 906 $task->{target} = $pbs_snapshot_name;
aaeeeebe 907 } else {
1a87db9e 908 if ($opts->{stdout}) {
6cba1855 909 $task->{target} = '-';
1a87db9e 910 } else {
6cba1855 911 $task->{target} = $task->{tmptar} = "$opts->{dumpdir}/$basename$ext";
1a87db9e
DM
912 $task->{tmptar} =~ s/\.[^\.]+$/\.dat/;
913 unlink $task->{tmptar};
914 }
aaeeeebe
DM
915 }
916
917 $task->{vmtype} = $vmtype;
918
dfb16115 919 my $pid = $$;
01fc3672 920 if ($opts->{tmpdir}) {
dfb16115 921 $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp${pid}_$vmid/";
01fc3672 922 } elsif ($self->{opts}->{pbs}) {
dfb16115 923 $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid";
aaeeeebe
DM
924 } else {
925 # dumpdir is posix? then use it as temporary dir
5dc86eb8 926 my $info = get_mount_info($opts->{dumpdir});
60e049c2 927 if ($vmtype eq 'qemu' ||
aaeeeebe
DM
928 grep ($_ eq $info->{fstype}, @posix_filesystems)) {
929 $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp";
930 } else {
dfb16115 931 $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid";
aaeeeebe
DM
932 debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" .
933 "using $task->{tmpdir} for temporary files", $logfd);
934 }
935 }
936
937 rmtree $task->{tmpdir};
938 mkdir $task->{tmpdir};
939 -d $task->{tmpdir} ||
940 die "unable to create temporary directory '$task->{tmpdir}'";
941
942 $logfd = IO::File->new (">$tmplog") ||
943 die "unable to create log file '$tmplog'";
944
945 $task->{dumpdir} = $opts->{dumpdir};
ff00abe6 946 $task->{storeid} = $opts->{storage};
1a87db9e 947 $task->{scfg} = $opts->{scfg};
aaeeeebe
DM
948 $task->{tmplog} = $tmplog;
949
1a87db9e 950 unlink $task->{logfile} if defined($task->{logfile});
aaeeeebe 951
29f26f6d
DJ
952 debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
953 debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime()));
aaeeeebe
DM
954
955 $plugin->set_logfd ($logfd);
956
957 # test is VM is running
958 my ($running, $status_text) = $plugin->vm_status ($vmid);
959
960 debugmsg ('info', "status = ${status_text}", $logfd);
961
962 # lock VM (prevent config changes)
963 $plugin->lock_vm ($vmid);
964
965 $cleanup->{unlock} = 1;
966
967 # prepare
968
6acb632a 969 my $mode = $running ? $task->{mode} : 'stop';
aaeeeebe
DM
970
971 if ($mode eq 'snapshot') {
972 my %saved_task = %$task;
973 eval { $plugin->prepare ($task, $vmid, $mode); };
974 if (my $err = $@) {
975 die $err if $err !~ m/^mode failure/;
976 debugmsg ('info', $err, $logfd);
977 debugmsg ('info', "trying 'suspend' mode instead", $logfd);
978 $mode = 'suspend'; # so prepare is called again below
60e049c2 979 %$task = %saved_task;
aaeeeebe
DM
980 }
981 }
982
6acb632a
WB
983 $cleanup->{prepared} = 1;
984
aaeeeebe
DM
985 $task->{mode} = $mode;
986
987 debugmsg ('info', "backup mode: $mode", $logfd);
ff119724 988 debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd) if $opts->{bwlimit};
aaeeeebe
DM
989 debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd);
990
991 if ($mode eq 'stop') {
aaeeeebe
DM
992 $plugin->prepare ($task, $vmid, $mode);
993
994 $self->run_hook_script ('backup-start', $task, $logfd);
995
996 if ($running) {
4922b7a9 997 debugmsg ('info', "stopping virtual guest", $logfd);
4e0947c8 998 $task->{vmstoptime} = time();
aaeeeebe
DM
999 $self->run_hook_script ('pre-stop', $task, $logfd);
1000 $plugin->stop_vm ($task, $vmid);
1001 $cleanup->{restart} = 1;
1002 }
60e049c2 1003
aaeeeebe
DM
1004
1005 } elsif ($mode eq 'suspend') {
aaeeeebe
DM
1006 $plugin->prepare ($task, $vmid, $mode);
1007
1008 $self->run_hook_script ('backup-start', $task, $logfd);
1009
8187f6b0
DM
1010 if ($vmtype eq 'lxc') {
1011 # pre-suspend rsync
1012 $plugin->copy_data_phase1($task, $vmid);
1013 }
1014
ef7d3fdd 1015 debugmsg ('info', "suspending guest", $logfd);
4e0947c8 1016 $task->{vmstoptime} = time ();
aaeeeebe
DM
1017 $self->run_hook_script ('pre-stop', $task, $logfd);
1018 $plugin->suspend_vm ($task, $vmid);
1019 $cleanup->{resume} = 1;
1020
8187f6b0
DM
1021 if ($vmtype eq 'lxc') {
1022 # post-suspend rsync
1023 $plugin->copy_data_phase2($task, $vmid);
1024
ef7d3fdd 1025 debugmsg ('info', "resuming guest", $logfd);
8187f6b0
DM
1026 $cleanup->{resume} = 0;
1027 $self->run_hook_script('pre-restart', $task, $logfd);
1028 $plugin->resume_vm($task, $vmid);
d5047243 1029 $self->run_hook_script('post-restart', $task, $logfd);
4e0947c8 1030 $log_vm_online_again->();
8187f6b0 1031 }
60e049c2 1032
aaeeeebe 1033 } elsif ($mode eq 'snapshot') {
c5be0f8c
DM
1034 $self->run_hook_script ('backup-start', $task, $logfd);
1035
aaeeeebe
DM
1036 my $snapshot_count = $task->{snapshot_count} || 0;
1037
1038 $self->run_hook_script ('pre-stop', $task, $logfd);
1039
1040 if ($snapshot_count > 1) {
1041 debugmsg ('info', "suspend vm to make snapshot", $logfd);
4e0947c8 1042 $task->{vmstoptime} = time ();
aaeeeebe
DM
1043 $plugin->suspend_vm ($task, $vmid);
1044 $cleanup->{resume} = 1;
1045 }
1046
1047 $plugin->snapshot ($task, $vmid);
1048
1049 $self->run_hook_script ('pre-restart', $task, $logfd);
1050
1051 if ($snapshot_count > 1) {
1052 debugmsg ('info', "resume vm", $logfd);
1053 $cleanup->{resume} = 0;
1054 $plugin->resume_vm ($task, $vmid);
4e0947c8 1055 $log_vm_online_again->();
aaeeeebe
DM
1056 }
1057
d5047243
FG
1058 $self->run_hook_script ('post-restart', $task, $logfd);
1059
aaeeeebe
DM
1060 } else {
1061 die "internal error - unknown mode '$mode'\n";
1062 }
1063
1064 # assemble archive image
1065 $plugin->assemble ($task, $vmid);
60e049c2
TM
1066
1067 # produce archive
aaeeeebe
DM
1068
1069 if ($opts->{stdout}) {
1070 debugmsg ('info', "sending archive to stdout", $logfd);
d7550e09 1071 $plugin->archive($task, $vmid, $task->{tmptar}, $comp);
aaeeeebe
DM
1072 $self->run_hook_script ('backup-end', $task, $logfd);
1073 return;
1074 }
1075
b3c3304f
TL
1076 my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump';
1077 debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd);
d7550e09 1078 $plugin->archive($task, $vmid, $task->{tmptar}, $comp);
aaeeeebe 1079
7ddcb3af 1080 if ($self->{opts}->{pbs}) {
e21a31a7 1081 # size is added to task struct in guest vzdump plugins
1a87db9e 1082 } else {
6cba1855
TL
1083 rename ($task->{tmptar}, $task->{target}) ||
1084 die "unable to rename '$task->{tmptar}' to '$task->{target}'\n";
aaeeeebe 1085
1a87db9e 1086 # determine size
6cba1855 1087 $task->{size} = (-s $task->{target}) || 0;
1a87db9e
DM
1088 my $cs = format_size ($task->{size});
1089 debugmsg ('info', "archive file size: $cs", $logfd);
1090 }
aaeeeebe 1091
bbd4cdd8
FE
1092 # Mark as protected before pruning.
1093 if (my $storeid = $opts->{storage}) {
1094 my $volname = $opts->{pbs} ? $task->{target} : basename($task->{target});
1095 my $volid = "${storeid}:backup/${volname}";
1096
e01438a7
FE
1097 if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') {
1098 debugmsg('info', "adding notes to backup", $logfd);
31213d61
FE
1099 my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); };
1100 if (my $err = $@) {
1101 debugmsg('warn', "unable to add notes - $err", $logfd);
1102 } else {
1103 eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'notes', $notes) };
1104 debugmsg('warn', "unable to add notes - $@", $logfd) if $@;
1105 }
e01438a7
FE
1106 }
1107
bbd4cdd8
FE
1108 if ($opts->{protected}) {
1109 debugmsg('info', "marking backup as protected", $logfd);
1110 eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'protected', 1) };
1111 die "unable to set protected flag - $@\n" if $@;
1112 }
1113 }
1114
e6946086 1115 if ($opts->{remove}) {
ca155141
TL
1116 my $keepstr = join(', ', map { "$_=$prune_options->{$_}" } sort keys %$prune_options);
1117 debugmsg ('info', "prune older backups with retention: $keepstr", $logfd);
b4546c7e 1118 my $pruned = 0;
f354fe47 1119 if (!defined($opts->{storage})) {
725c0555 1120 my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname);
2fc939a9 1121
f354fe47 1122 PVE::Storage::prune_mark_backup_group($bklist, $prune_options);
e6946086 1123
f354fe47
FE
1124 foreach my $prune_entry (@{$bklist}) {
1125 next if $prune_entry->{mark} ne 'remove';
b4546c7e 1126 $pruned++;
f354fe47
FE
1127 my $archive_path = $prune_entry->{path};
1128 debugmsg ('info', "delete old backup '$archive_path'", $logfd);
1129 PVE::Storage::archive_remove($archive_path);
1a87db9e 1130 }
f354fe47 1131 } else {
b4546c7e
TL
1132 my $pruned_list = PVE::Storage::prune_backups(
1133 $cfg,
1134 $opts->{storage},
1135 $prune_options,
1136 $vmid,
1137 $vmtype,
1138 0,
1139 sub { debugmsg($_[0], $_[1], $logfd) },
1140 );
1141 $pruned = scalar(grep { $_->{mark} eq 'remove' } $pruned_list->@*);
aaeeeebe 1142 }
b4546c7e
TL
1143 my $log_pruned_extra = $pruned > 0 ? " not covered by keep-retention policy" : "";
1144 debugmsg ('info', "pruned $pruned backup(s)${log_pruned_extra}", $logfd);
aaeeeebe
DM
1145 }
1146
1147 $self->run_hook_script ('backup-end', $task, $logfd);
1148 };
1149 my $err = $@;
1150
1151 if ($plugin) {
1152 # clean-up
1153
1154 if ($cleanup->{unlock}) {
1155 eval { $plugin->unlock_vm ($vmid); };
1156 warn $@ if $@;
1157 }
1158
6acb632a 1159 if ($cleanup->{prepared}) {
ca2605e7
DM
1160 # only call cleanup when necessary (when prepare was executed)
1161 eval { $plugin->cleanup ($task, $vmid) };
1162 warn $@ if $@;
1163 }
aaeeeebe
DM
1164
1165 eval { $plugin->set_logfd (undef); };
1166 warn $@ if $@;
1167
60e049c2
TM
1168 if ($cleanup->{resume} || $cleanup->{restart}) {
1169 eval {
aaeeeebe
DM
1170 $self->run_hook_script ('pre-restart', $task, $logfd);
1171 if ($cleanup->{resume}) {
1172 debugmsg ('info', "resume vm", $logfd);
1173 $plugin->resume_vm ($task, $vmid);
1174 } else {
757fd3d5
DM
1175 my $running = $plugin->vm_status($vmid);
1176 if (!$running) {
1177 debugmsg ('info', "restarting vm", $logfd);
1178 $plugin->start_vm ($task, $vmid);
1179 }
d5047243
FG
1180 }
1181 $self->run_hook_script ('post-restart', $task, $logfd);
aaeeeebe
DM
1182 };
1183 my $err = $@;
1184 if ($err) {
1185 warn $err;
1186 } else {
4e0947c8 1187 $log_vm_online_again->();
aaeeeebe
DM
1188 }
1189 }
1190 }
1191
1192 eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; };
1193 warn $@ if $@;
1194
fe6643b6 1195 eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; };
aaeeeebe
DM
1196 warn $@ if $@;
1197
1198 my $delay = $task->{backuptime} = time () - $vmstarttime;
1199
1200 if ($err) {
1201 $task->{state} = 'err';
1202 $task->{msg} = $err;
1203 debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1);
29f26f6d 1204 debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime()));
aaeeeebe
DM
1205
1206 eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
1207
1208 } else {
1209 $task->{state} = 'ok';
1210 my $tstr = format_time ($delay);
1211 debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
29f26f6d 1212 debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime()));
aaeeeebe
DM
1213 }
1214
1215 close ($logfd) if $logfd;
60e049c2 1216
1a87db9e 1217 if ($task->{tmplog}) {
7ddcb3af 1218 if ($self->{opts}->{pbs}) {
1a87db9e 1219 if ($task->{state} eq 'ok') {
e8d9e1ed
TL
1220 eval {
1221 PVE::Storage::PBSPlugin::run_raw_client_cmd(
1222 $opts->{scfg},
1223 $opts->{storage},
1224 'upload-log',
1225 [ $pbs_snapshot_name, $task->{tmplog} ],
1226 errmsg => "uploading backup task log failed",
f332a167 1227 outfunc => sub {},
e8d9e1ed
TL
1228 );
1229 };
1230 debugmsg('warn', "$@") if $@; # $@ contains already error prefix
1a87db9e
DM
1231 }
1232 } elsif ($task->{logfile}) {
1233 system {'cp'} 'cp', $task->{tmplog}, $task->{logfile};
1234 }
aaeeeebe
DM
1235 }
1236
1237 eval { $self->run_hook_script ('log-end', $task); };
1238
1239 die $err if $err && $err =~ m/^interrupted by signal$/;
1240}
1241
1242sub exec_backup {
d7550e09 1243 my ($self, $rpcenv, $authuser) = @_;
aaeeeebe
DM
1244
1245 my $opts = $self->{opts};
1246
1247 debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1);
f647d0fc
FE
1248
1249 if (scalar(@{$self->{skiplist}})) {
1250 my $skip_string = join(', ', sort { $a <=> $b } @{$self->{skiplist}});
1251 debugmsg ('info', "skip external VMs: $skip_string");
1252 }
60e049c2 1253
aaeeeebe 1254 my $tasklist = [];
df5875b4
AL
1255 my $vzdump_plugins = {};
1256 foreach my $plugin (@{$self->{plugins}}) {
1257 my $type = $plugin->type();
1258 next if exists $vzdump_plugins->{$type};
1259 $vzdump_plugins->{$type} = $plugin;
1260 }
aaeeeebe 1261
df5875b4 1262 my $vmlist = PVE::Cluster::get_vmlist();
4d73f0e2
FE
1263 my $vmids = [ sort { $a <=> $b } @{$opts->{vmids}} ];
1264 foreach my $vmid (@{$vmids}) {
7f874148
FE
1265 my $plugin;
1266 if (defined($vmlist->{ids}->{$vmid})) {
1267 my $guest_type = $vmlist->{ids}->{$vmid}->{type};
1268 $plugin = $vzdump_plugins->{$guest_type};
1269 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all});
1270 }
df5875b4 1271 push @$tasklist, {
f839f3d1 1272 mode => $opts->{mode},
df5875b4 1273 plugin => $plugin,
f839f3d1
TL
1274 state => 'todo',
1275 vmid => $vmid,
1276 };
aaeeeebe
DM
1277 }
1278
44b21574
WB
1279 # Use in-memory files for the outer hook logs to pass them to sendmail.
1280 my $job_start_log = '';
1281 my $job_end_log = '';
1282 open my $job_start_fd, '>', \$job_start_log;
1283 open my $job_end_fd, '>', \$job_end_log;
1284
aaeeeebe
DM
1285 my $starttime = time();
1286 my $errcount = 0;
1287 eval {
1288
44b21574 1289 $self->run_hook_script ('job-start', undef, $job_start_fd);
aaeeeebe
DM
1290
1291 foreach my $task (@$tasklist) {
1292 $self->exec_backup_task ($task);
1293 $errcount += 1 if $task->{state} ne 'ok';
1294 }
1295
44b21574 1296 $self->run_hook_script ('job-end', undef, $job_end_fd);
aaeeeebe
DM
1297 };
1298 my $err = $@;
1299
aaeeeebe 1300 if ($err) {
573e8320
FE
1301 eval { $self->run_hook_script ('job-abort', undef, $job_end_fd); };
1302 $err .= $@ if $@;
aaeeeebe
DM
1303 debugmsg ('err', "Backup job failed - $err", undef, 1);
1304 } else {
1305 if ($errcount) {
1306 debugmsg ('info', "Backup job finished with errors", undef, 1);
1307 } else {
61ca4432 1308 debugmsg ('info', "Backup job finished successfully", undef, 1);
aaeeeebe
DM
1309 }
1310 }
1311
44b21574
WB
1312 close $job_start_fd;
1313 close $job_end_fd;
1314
aaeeeebe
DM
1315 my $totaltime = time() - $starttime;
1316
b241deb7 1317 eval {
268c6428
FE
1318 # otherwise $self->sendmail() will interpret it as multiple problems
1319 my $chomped_err = $err;
1320 chomp($chomped_err) if $chomped_err;
1321
b241deb7
FE
1322 $self->sendmail(
1323 $tasklist,
1324 $totaltime,
268c6428 1325 $chomped_err,
b241deb7
FE
1326 $self->{job_init_log} . $job_start_log,
1327 $job_end_log,
1328 );
1329 };
aaeeeebe 1330 debugmsg ('err', $@) if $@;
4a4051d8
DM
1331
1332 die $err if $err;
1333
60e049c2 1334 die "job errors\n" if $errcount;
8682f6fc
WL
1335
1336 unlink $pidfile;
aaeeeebe
DM
1337}
1338
ac27b58d 1339
47664cbe
DM
1340sub option_exists {
1341 my $key = shift;
1342 return defined($confdesc->{$key});
1343}
1344
f8ed6af8
FE
1345# NOTE it might make sense to merge this and verify_vzdump_parameters(), but one
1346# needs to adapt command_line() in guest-common's PVE/VZDump/Common.pm and detect
1347# a second parsing attempt, because verify_vzdump_parameters() is called twice
1348# during the update_job API call.
1349sub parse_mailto_exclude_path {
1350 my ($param) = @_;
1351
d6d3e55b 1352 # exclude-path list need to be 0 separated or be an array
f8ed6af8 1353 if (defined($param->{'exclude-path'})) {
d6d3e55b
DC
1354 my $expaths;
1355 if (ref($param->{'exclude-path'}) eq 'ARRAY') {
1356 $expaths = $param->{'exclude-path'};
1357 } else {
1358 $expaths = [split(/\0/, $param->{'exclude-path'} || '')];
1359 }
1360 $param->{'exclude-path'} = $expaths;
f8ed6af8
FE
1361 }
1362
1363 if (defined($param->{mailto})) {
1364 my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto'));
1365 $param->{mailto} = [ @mailto ];
1366 }
1367
1368 return;
1369}
1370
31aef761
DM
1371sub verify_vzdump_parameters {
1372 my ($param, $check_missing) = @_;
1373
1374 raise_param_exc({ all => "option conflicts with option 'vmid'"})
eab837c4 1375 if $param->{all} && $param->{vmid};
31aef761
DM
1376
1377 raise_param_exc({ exclude => "option conflicts with option 'vmid'"})
1378 if $param->{exclude} && $param->{vmid};
1379
f3376261
TM
1380 raise_param_exc({ pool => "option conflicts with option 'vmid'"})
1381 if $param->{pool} && $param->{vmid};
1382
e6946086
FE
1383 raise_param_exc({ 'prune-backups' => "option conflicts with option 'maxfiles'"})
1384 if defined($param->{'prune-backups'}) && defined($param->{maxfiles});
1385
1c56bcf3 1386 $parse_prune_backups_maxfiles->($param, 'CLI parameters');
93880785 1387 parse_performance($param);
e6946086 1388
facf65a6
FE
1389 if (my $template = $param->{'notes-template'}) {
1390 eval { $verify_notes_template->($template); };
1391 raise_param_exc({'notes-template' => $@}) if $@;
1392 }
010ff16e 1393
f3376261 1394 $param->{all} = 1 if (defined($param->{exclude}) && !$param->{pool});
31aef761
DM
1395
1396 return if !$check_missing;
1397
1398 raise_param_exc({ vmid => "property is missing"})
f3376261 1399 if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid};
8682f6fc
WL
1400
1401}
1402
eab837c4 1403sub stop_running_backups {
8682f6fc
WL
1404 my($self) = @_;
1405
eab837c4
DM
1406 my $upid = PVE::Tools::file_read_firstline($pidfile);
1407 return if !$upid;
8682f6fc 1408
eab837c4 1409 my $task = PVE::Tools::upid_decode($upid);
8682f6fc 1410
60e049c2 1411 if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) &&
eab837c4
DM
1412 PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) {
1413 kill(15, $task->{pid});
1414 # wait max 15 seconds to shut down (else, do nothing for now)
1415 my $i;
1416 for ($i = 15; $i > 0; $i--) {
1417 last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart}));
1418 sleep (1);
1419 }
76189130 1420 die "stopping backup process $task->{pid} failed\n" if $i == 0;
8682f6fc 1421 }
31aef761
DM
1422}
1423
5c4da4c3
AL
1424sub get_included_guests {
1425 my ($job) = @_;
1426
5c4da4c3 1427 my $vmids = [];
df5875b4
AL
1428 my $vmids_per_node = {};
1429
1430 my $vmlist = PVE::Cluster::get_vmlist();
5c4da4c3 1431
5c4da4c3
AL
1432 if ($job->{pool}) {
1433 $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool});
df5875b4 1434 } elsif ($job->{vmid}) {
05447e04 1435 $vmids = [ split_list($job->{vmid}) ];
da7cb515 1436 } elsif ($job->{all}) {
df5875b4 1437 # all or exclude
05447e04
TL
1438 my $exclude = check_vmids(split_list($job->{exclude}));
1439 my $excludehash = { map { $_ => 1 } @$exclude };
df5875b4 1440
05447e04 1441 for my $id (keys %{$vmlist->{ids}}) {
df5875b4
AL
1442 next if $excludehash->{$id};
1443 push @$vmids, $id;
1444 }
da7cb515
AL
1445 } else {
1446 return $vmids_per_node;
5c4da4c3 1447 }
50ba40ec 1448 $vmids = check_vmids(@$vmids);
5c4da4c3 1449
50ba40ec 1450 for my $vmid (@$vmids) {
7f874148
FE
1451 if (defined($vmlist->{ids}->{$vmid})) {
1452 my $node = $vmlist->{ids}->{$vmid}->{node};
1453 next if (defined $job->{node} && $job->{node} ne $node);
50ba40ec 1454
7f874148
FE
1455 push @{$vmids_per_node->{$node}}, $vmid;
1456 } else {
1457 push @{$vmids_per_node->{''}}, $vmid;
1458 }
5c4da4c3
AL
1459 }
1460
df5875b4 1461 return $vmids_per_node;
5c4da4c3
AL
1462}
1463
aaeeeebe 14641;