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