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