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