]>
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 | use constant MAX_LOG_SIZE => 1024*1024; |
aaeeeebe | 481 | |
c4afde55 LW |
482 | sub send_notification { |
483 | my ($self, $tasklist, $total_time, $err, $detail_pre, $detail_post) = @_; | |
aaeeeebe | 484 | |
c4afde55 LW |
485 | my $opts = $self->{opts}; |
486 | my $mailto = $opts->{mailto}; | |
487 | my $cmdline = $self->{cmdline}; | |
38035bdf LW |
488 | my $policy = $opts->{mailnotification} // 'always'; |
489 | my $mode = $opts->{"notification-mode"} // 'auto'; | |
c4afde55 LW |
490 | |
491 | sanitize_task_list($tasklist); | |
878d9af6 | 492 | my ($error_count, $total_size) = aggregate_task_statistics($tasklist); |
c4afde55 LW |
493 | |
494 | my $failed = ($error_count || $err); | |
495 | ||
c4afde55 LW |
496 | my $status_text = $failed ? 'backup failed' : 'backup successful'; |
497 | ||
498 | if ($err) { | |
499 | if ($err =~ /\n/) { | |
500 | $status_text .= ": multiple problems"; | |
ce84a950 | 501 | } else { |
c4afde55 LW |
502 | $status_text .= ": $err"; |
503 | $err = undef; | |
aaeeeebe | 504 | } |
aaeeeebe | 505 | } |
c4afde55 LW |
506 | |
507 | my $text_log_part = "$cmdline\n\n"; | |
508 | $text_log_part .= $detail_pre . "\n" if defined($detail_pre); | |
509 | $text_log_part .= read_backup_task_logs($tasklist); | |
510 | $text_log_part .= $detail_post if defined($detail_post); | |
511 | ||
512 | if (length($text_log_part) > MAX_LOG_SIZE) | |
ce84a950 | 513 | { |
c4afde55 LW |
514 | # Let's limit the maximum length of included logs |
515 | $text_log_part = "Log output was too long to be sent. ". | |
ce84a950 | 516 | "See Task History for details!\n"; |
c4afde55 LW |
517 | }; |
518 | ||
e95a9a33 LW |
519 | my $hostname = get_hostname(); |
520 | ||
c4afde55 | 521 | my $notification_props = { |
878d9af6 | 522 | "hostname" => $hostname, |
c4afde55 | 523 | "error-message" => $err, |
878d9af6 LW |
524 | "guest-table" => build_guest_table($tasklist), |
525 | "logs" => $text_log_part, | |
526 | "status-text" => $status_text, | |
527 | "total-time" => $total_time, | |
528 | "total-size" => $total_size, | |
c4afde55 LW |
529 | }; |
530 | ||
e95a9a33 | 531 | my $fields = { |
38035bdf LW |
532 | # TODO: There is no straight-forward way yet to get the |
533 | # backup job id here... (I think pvescheduler would need | |
534 | # to pass that to the vzdump call?) | |
e95a9a33 LW |
535 | type => "vzdump", |
536 | hostname => $hostname, | |
537 | }; | |
538 | ||
38035bdf LW |
539 | my $severity = $failed ? "error" : "info"; |
540 | my $email_configured = $mailto && scalar(@$mailto); | |
541 | ||
542 | if (($mode eq 'auto' && $email_configured) || $mode eq 'legacy-sendmail') { | |
543 | if ($email_configured && ($policy eq "always" || ($policy eq "failure" && $failed))) { | |
544 | # Start out with an empty config. Might still contain | |
545 | # built-ins, so we need to disable/remove them. | |
546 | my $notification_config = Proxmox::RS::Notify->parse_config('', ''); | |
547 | ||
548 | # Remove built-in matchers, since we only want to send an | |
549 | # email to the specified recipients and nobody else. | |
550 | for my $matcher (@{$notification_config->get_matchers()}) { | |
551 | $notification_config->delete_matcher($matcher->{name}); | |
552 | } | |
c4afde55 | 553 | |
38035bdf LW |
554 | # <, >, @ are not allowed in endpoint names, but that is only |
555 | # verified once the config is serialized. That means that | |
556 | # we can rely on that fact that no other endpoint with this name exists. | |
557 | my $endpoint_name = "<" . join(",", @$mailto) . ">"; | |
558 | $notification_config->add_sendmail_endpoint( | |
559 | $endpoint_name, | |
560 | $mailto, | |
561 | undef, | |
562 | undef, | |
563 | "vzdump backup tool" | |
564 | ); | |
565 | ||
566 | my $endpoints = [$endpoint_name]; | |
567 | ||
568 | # Add a matcher that matches all notifications, set our | |
569 | # newly created target as a target. | |
570 | $notification_config->add_matcher( | |
571 | "<matcher-$endpoint_name>", | |
572 | $endpoints, | |
573 | ); | |
574 | ||
575 | PVE::Notify::notify( | |
576 | $severity, | |
fede7e87 | 577 | "vzdump", |
38035bdf LW |
578 | $notification_props, |
579 | $fields, | |
580 | $notification_config | |
581 | ); | |
582 | } | |
583 | } else { | |
584 | # We use the 'new' system, or we are set to 'auto' and | |
585 | # no email addresses were configured. | |
586 | PVE::Notify::notify( | |
587 | $severity, | |
fede7e87 | 588 | "vzdump", |
38035bdf LW |
589 | $notification_props, |
590 | $fields, | |
c4afde55 | 591 | ); |
ce84a950 | 592 | } |
aaeeeebe DM |
593 | }; |
594 | ||
595 | sub new { | |
a7e42354 | 596 | my ($class, $cmdline, $opts, $skiplist) = @_; |
aaeeeebe DM |
597 | |
598 | mkpath $logdir; | |
599 | ||
600 | check_bin ('cp'); | |
601 | check_bin ('df'); | |
602 | check_bin ('sendmail'); | |
603 | check_bin ('rsync'); | |
604 | check_bin ('tar'); | |
605 | check_bin ('mount'); | |
606 | check_bin ('umount'); | |
607 | check_bin ('cstream'); | |
608 | check_bin ('ionice'); | |
609 | ||
47664cbe | 610 | if ($opts->{mode} && $opts->{mode} eq 'snapshot') { |
aaeeeebe DM |
611 | check_bin ('lvcreate'); |
612 | check_bin ('lvs'); | |
613 | check_bin ('lvremove'); | |
614 | } | |
615 | ||
616 | my $defaults = read_vzdump_defaults(); | |
617 | ||
618 | foreach my $k (keys %$defaults) { | |
acc963c3 | 619 | next if $k eq 'exclude-path' || $k eq 'prune-backups'; # dealt with separately |
aaeeeebe DM |
620 | if ($k eq 'dumpdir' || $k eq 'storage') { |
621 | $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) && | |
622 | !defined ($opts->{storage}); | |
097fe045 FE |
623 | } elsif (!defined($opts->{$k})) { |
624 | $opts->{$k} = $defaults->{$k}; | |
625 | } elsif ($k eq 'performance') { | |
626 | $opts->{$k} = merge_performance($opts->{$k}, $defaults->{$k}); | |
aaeeeebe DM |
627 | } |
628 | } | |
629 | ||
aaeeeebe DM |
630 | $opts->{dumpdir} =~ s|/+$|| if ($opts->{dumpdir}); |
631 | $opts->{tmpdir} =~ s|/+$|| if ($opts->{tmpdir}); | |
632 | ||
a7e42354 | 633 | $skiplist = [] if !$skiplist; |
94009776 TL |
634 | my $self = bless { |
635 | cmdline => $cmdline, | |
636 | opts => $opts, | |
637 | skiplist => $skiplist, | |
638 | }, $class; | |
aaeeeebe | 639 | |
45f16a06 | 640 | my $findexcl = $self->{findexcl} = []; |
f4a8bab4 | 641 | if ($defaults->{'exclude-path'}) { |
45f16a06 | 642 | push @$findexcl, @{$defaults->{'exclude-path'}}; |
f4a8bab4 DM |
643 | } |
644 | ||
aaeeeebe | 645 | if ($opts->{'exclude-path'}) { |
45f16a06 | 646 | push @$findexcl, @{$opts->{'exclude-path'}}; |
aaeeeebe DM |
647 | } |
648 | ||
649 | if ($opts->{stdexcludes}) { | |
1353489e TL |
650 | push @$findexcl, |
651 | '/tmp/?*', | |
652 | '/var/tmp/?*', | |
653 | '/var/run/?*.pid', | |
654 | ; | |
aaeeeebe DM |
655 | } |
656 | ||
657 | foreach my $p (@plugins) { | |
1353489e | 658 | my $pd = $p->new($self); |
aaeeeebe DM |
659 | |
660 | push @{$self->{plugins}}, $pd; | |
aaeeeebe DM |
661 | } |
662 | ||
1a87db9e | 663 | if (defined($opts->{storage}) && $opts->{stdout}) { |
a6fcd7d9 FE |
664 | die "cannot use options 'storage' and 'stdout' at the same time\n"; |
665 | } elsif (defined($opts->{storage}) && defined($opts->{dumpdir})) { | |
666 | die "cannot use options 'storage' and 'dumpdir' at the same time\n"; | |
1a87db9e DM |
667 | } |
668 | ||
43f83ad9 FE |
669 | if (my $storage = get_storage_param($opts)) { |
670 | $opts->{storage} = $storage; | |
aaeeeebe DM |
671 | } |
672 | ||
3f488b61 FE |
673 | # Enforced by the API too, but these options might come in via defaults. Drop them if necessary. |
674 | if (!$opts->{storage}) { | |
675 | delete $opts->{$_} for qw(notes-template protected); | |
676 | } | |
677 | ||
403761c4 | 678 | my $errors = ''; |
3c5a7616 FE |
679 | my $add_error = sub { |
680 | my ($error) = @_; | |
681 | $errors .= "\n" if $errors; | |
682 | chomp($error); | |
683 | $errors .= $error; | |
684 | }; | |
403761c4 | 685 | |
c527d28f FE |
686 | eval { |
687 | $self->{job_init_log} = ''; | |
688 | open my $job_init_fd, '>', \$self->{job_init_log}; | |
689 | $self->run_hook_script('job-init', undef, $job_init_fd); | |
690 | close $job_init_fd; | |
691 | ||
692 | PVE::Cluster::cfs_update(); # Pick up possible changes made by the hook script. | |
693 | }; | |
694 | $add_error->($@) if $@; | |
695 | ||
aaeeeebe | 696 | if ($opts->{storage}) { |
164651da FE |
697 | my $storage_cfg = PVE::Storage::config(); |
698 | eval { PVE::Storage::activate_storage($storage_cfg, $opts->{storage}) }; | |
3c5a7616 | 699 | $add_error->("could not activate storage '$opts->{storage}': $@") if $@; |
164651da | 700 | |
1e4583d5 | 701 | my $info = eval { storage_info ($opts->{storage}) }; |
e6946086 | 702 | if (my $err = $@) { |
3c5a7616 | 703 | $add_error->("could not get storage information for '$opts->{storage}': $err"); |
e6946086 FE |
704 | } else { |
705 | $opts->{dumpdir} = $info->{dumpdir}; | |
706 | $opts->{scfg} = $info->{scfg}; | |
707 | $opts->{pbs} = $info->{pbs}; | |
acc963c3 | 708 | $opts->{'prune-backups'} //= $info->{'prune-backups'}; |
e6946086 | 709 | } |
aaeeeebe | 710 | } elsif ($opts->{dumpdir}) { |
3c5a7616 | 711 | $add_error->("dumpdir '$opts->{dumpdir}' does not exist") |
aaeeeebe DM |
712 | if ! -d $opts->{dumpdir}; |
713 | } else { | |
60e049c2 | 714 | die "internal error"; |
aaeeeebe DM |
715 | } |
716 | ||
acc963c3 | 717 | $opts->{'prune-backups'} //= $defaults->{'prune-backups'}; |
5b6b72e6 | 718 | |
1db8529e FE |
719 | # avoid triggering any remove code path if keep-all is set |
720 | $opts->{remove} = 0 if $opts->{'prune-backups'}->{'keep-all'}; | |
721 | ||
aaeeeebe | 722 | if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) { |
3c5a7616 | 723 | $add_error->("tmpdir '$opts->{tmpdir}' does not exist"); |
403761c4 WB |
724 | } |
725 | ||
726 | if ($errors) { | |
c4afde55 | 727 | eval { $self->send_notification([], 0, $errors); }; |
403761c4 WB |
728 | debugmsg ('err', $@) if $@; |
729 | die "$errors\n"; | |
aaeeeebe DM |
730 | } |
731 | ||
732 | return $self; | |
aaeeeebe DM |
733 | } |
734 | ||
aaeeeebe DM |
735 | sub get_mount_info { |
736 | my ($dir) = @_; | |
737 | ||
8572d646 DM |
738 | # Note: df 'available' can be negative, and percentage set to '-' |
739 | ||
d93d0459 | 740 | my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir]; |
aaeeeebe | 741 | |
d93d0459 | 742 | my $res; |
aaeeeebe | 743 | |
d93d0459 DM |
744 | my $parser = sub { |
745 | my $line = shift; | |
8572d646 DM |
746 | if (my ($fsid, $fstype, undef, $mp) = $line =~ |
747 | m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) { | |
d93d0459 DM |
748 | $res = { |
749 | device => $fsid, | |
750 | fstype => $fstype, | |
751 | mountpoint => $mp, | |
752 | }; | |
753 | } | |
aaeeeebe | 754 | }; |
d93d0459 DM |
755 | |
756 | eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); }; | |
757 | warn $@ if $@; | |
758 | ||
759 | return $res; | |
aaeeeebe DM |
760 | } |
761 | ||
aaeeeebe | 762 | sub getlock { |
eab837c4 | 763 | my ($self, $upid) = @_; |
aaeeeebe | 764 | |
8682f6fc | 765 | my $fh; |
60e049c2 | 766 | |
aaeeeebe | 767 | my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait}; |
60e049c2 | 768 | |
dc6e0a52 | 769 | die "missing UPID" if !$upid; # should not happen |
eab837c4 | 770 | |
5620e576 TL |
771 | my $SERVER_FLCK; |
772 | if (!open ($SERVER_FLCK, '>>', "$lockfile")) { | |
aaeeeebe | 773 | debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1); |
6ec9de44 | 774 | die "can't open lock on file '$lockfile' - $!"; |
aaeeeebe DM |
775 | } |
776 | ||
5620e576 | 777 | if (!flock ($SERVER_FLCK, LOCK_EX|LOCK_NB)) { |
eab837c4 | 778 | if (!$maxwait) { |
76189130 TL |
779 | debugmsg ('err', "can't acquire lock '$lockfile' (wait = 0)", undef, 1); |
780 | die "can't acquire lock '$lockfile' (wait = 0)"; | |
eab837c4 | 781 | } |
aaeeeebe | 782 | |
eab837c4 | 783 | debugmsg('info', "trying to get global lock - waiting...", undef, 1); |
eab837c4 DM |
784 | eval { |
785 | alarm ($maxwait * 60); | |
60e049c2 | 786 | |
eab837c4 | 787 | local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; }; |
aaeeeebe | 788 | |
5620e576 | 789 | if (!flock ($SERVER_FLCK, LOCK_EX)) { |
eab837c4 | 790 | my $err = $!; |
5620e576 | 791 | close ($SERVER_FLCK); |
eab837c4 DM |
792 | alarm (0); |
793 | die "$err\n"; | |
794 | } | |
aaeeeebe | 795 | alarm (0); |
eab837c4 | 796 | }; |
aaeeeebe | 797 | alarm (0); |
60e049c2 | 798 | |
eab837c4 | 799 | my $err = $@; |
60e049c2 | 800 | |
eab837c4 | 801 | if ($err) { |
76189130 TL |
802 | debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1); |
803 | die "can't acquire lock '$lockfile' - $err"; | |
eab837c4 | 804 | } |
aaeeeebe | 805 | |
eab837c4 | 806 | debugmsg('info', "got global lock", undef, 1); |
aaeeeebe DM |
807 | } |
808 | ||
eab837c4 | 809 | PVE::Tools::file_set_contents($pidfile, $upid); |
875c2e5a FE |
810 | |
811 | return $SERVER_FLCK; | |
aaeeeebe DM |
812 | } |
813 | ||
814 | sub run_hook_script { | |
815 | my ($self, $phase, $task, $logfd) = @_; | |
816 | ||
817 | my $opts = $self->{opts}; | |
818 | ||
819 | my $script = $opts->{script}; | |
aaeeeebe DM |
820 | return if !$script; |
821 | ||
0ac20586 TL |
822 | die "Error: The hook script '$script' does not exist.\n" if ! -f $script; |
823 | die "Error: The hook script '$script' is not executable.\n" if ! -x $script; | |
59798893 | 824 | |
58f763e1 | 825 | my $cmd = [$script, $phase]; |
aaeeeebe | 826 | |
58f763e1 FG |
827 | if ($task) { |
828 | push @$cmd, $task->{mode}; | |
829 | push @$cmd, $task->{vmid}; | |
830 | } | |
aaeeeebe DM |
831 | |
832 | local %ENV; | |
28272ca2 DM |
833 | # set immutable opts directly (so they are available in all phases) |
834 | $ENV{STOREID} = $opts->{storage} if $opts->{storage}; | |
835 | $ENV{DUMPDIR} = $opts->{dumpdir} if $opts->{dumpdir}; | |
836 | ||
6cba1855 | 837 | foreach my $ek (qw(vmtype hostname target logfile)) { |
aaeeeebe DM |
838 | $ENV{uc($ek)} = $task->{$ek} if $task->{$ek}; |
839 | } | |
840 | ||
841 | run_command ($logfd, $cmd); | |
842 | } | |
843 | ||
d7550e09 | 844 | sub compressor_info { |
778d5b6d TL |
845 | my ($opts) = @_; |
846 | my $opt_compress = $opts->{compress}; | |
d7550e09 DM |
847 | |
848 | if (!$opt_compress || $opt_compress eq '0') { | |
849 | return undef; | |
850 | } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') { | |
851 | return ('lzop', 'lzo'); | |
852 | } elsif ($opt_compress eq 'gzip') { | |
778d5b6d | 853 | if ($opts->{pigz} > 0) { |
8555b100 TL |
854 | my $pigz_threads = $opts->{pigz}; |
855 | if ($pigz_threads == 1) { | |
856 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
857 | $pigz_threads = int(($cpuinfo->{cpus} + 1)/2); | |
858 | } | |
819e4ff5 | 859 | return ("pigz -p ${pigz_threads} --rsyncable", 'gz'); |
778d5b6d | 860 | } else { |
e953f92a | 861 | return ('gzip --rsyncable', 'gz'); |
778d5b6d | 862 | } |
3e2c5105 AA |
863 | } elsif ($opt_compress eq 'zstd') { |
864 | my $zstd_threads = $opts->{zstd} // 1; | |
865 | if ($zstd_threads == 0) { | |
866 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
867 | $zstd_threads = int(($cpuinfo->{cpus} + 1)/2); | |
868 | } | |
9a023d55 | 869 | return ("zstd --threads=${zstd_threads}", 'zst'); |
d7550e09 DM |
870 | } else { |
871 | die "internal error - unknown compression option '$opt_compress'"; | |
872 | } | |
873 | } | |
899b8373 | 874 | |
725c0555 | 875 | sub get_backup_file_list { |
0a9ca6ca | 876 | my ($dir, $bkname) = @_; |
899b8373 DM |
877 | |
878 | my $bklist = []; | |
879 | foreach my $fn (<$dir/${bkname}-*>) { | |
de2d177e FE |
880 | my $archive_info = eval { PVE::Storage::archive_info($fn) } // {}; |
881 | if ($archive_info->{is_std_name}) { | |
7bffbd22 | 882 | my $path = "$dir/$archive_info->{filename}"; |
de2d177e | 883 | my $backup = { |
7bffbd22 | 884 | 'path' => $path, |
de2d177e FE |
885 | 'ctime' => $archive_info->{ctime}, |
886 | }; | |
725c0555 FG |
887 | $backup->{mark} = "protected" |
888 | if -e PVE::Storage::protection_file_path($path); | |
de2d177e | 889 | push @{$bklist}, $backup; |
899b8373 DM |
890 | } |
891 | } | |
892 | ||
893 | return $bklist; | |
894 | } | |
60e049c2 | 895 | |
aaeeeebe DM |
896 | sub exec_backup_task { |
897 | my ($self, $task) = @_; | |
60e049c2 | 898 | |
aaeeeebe DM |
899 | my $opts = $self->{opts}; |
900 | ||
e6946086 | 901 | my $cfg = PVE::Storage::config(); |
aaeeeebe DM |
902 | my $vmid = $task->{vmid}; |
903 | my $plugin = $task->{plugin}; | |
1a87db9e DM |
904 | |
905 | $task->{backup_time} = time(); | |
906 | ||
907 | my $pbs_group_name; | |
908 | my $pbs_snapshot_name; | |
909 | ||
aaeeeebe | 910 | my $vmstarttime = time (); |
60e049c2 | 911 | |
aaeeeebe DM |
912 | my $logfd; |
913 | ||
914 | my $cleanup = {}; | |
915 | ||
4e0947c8 TL |
916 | my $log_vm_online_again = sub { |
917 | return if !defined($task->{vmstoptime}); | |
918 | $task->{vmconttime} //= time(); | |
919 | my $delay = $task->{vmconttime} - $task->{vmstoptime}; | |
310cde8b | 920 | $delay = '<1' if $delay < 1; |
4e0947c8 TL |
921 | debugmsg ('info', "guest is online again after $delay seconds", $logfd); |
922 | }; | |
aaeeeebe DM |
923 | |
924 | eval { | |
925 | die "unable to find VM '$vmid'\n" if !$plugin; | |
926 | ||
d51f5a1e FE |
927 | my $vmtype = $plugin->type(); |
928 | ||
929 | if ($self->{opts}->{pbs}) { | |
930 | if ($vmtype eq 'lxc') { | |
931 | $pbs_group_name = "ct/$vmid"; | |
932 | } elsif ($vmtype eq 'qemu') { | |
933 | $pbs_group_name = "vm/$vmid"; | |
934 | } else { | |
935 | die "pbs backup not implemented for plugin type '$vmtype'\n"; | |
936 | } | |
937 | my $btime = strftime("%FT%TZ", gmtime($task->{backup_time})); | |
938 | $pbs_snapshot_name = "$pbs_group_name/$btime"; | |
939 | } | |
940 | ||
2de3ee58 | 941 | # for now we deny backups of a running ha managed service in *stop* mode |
83ec8f81 | 942 | # as it interferes with the HA stack (started services should not stop). |
2de3ee58 | 943 | if ($opts->{mode} eq 'stop' && |
83ec8f81 | 944 | PVE::HA::Config::vm_is_ha_managed($vmid, 'started')) |
2de3ee58 TL |
945 | { |
946 | die "Cannot execute a backup with stop mode on a HA managed and". | |
947 | " enabled Service. Use snapshot mode or disable the Service.\n"; | |
948 | } | |
949 | ||
aaeeeebe DM |
950 | my $tmplog = "$logdir/$vmtype-$vmid.log"; |
951 | ||
aaeeeebe | 952 | my $bkname = "vzdump-$vmtype-$vmid"; |
1a87db9e | 953 | my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time})); |
aaeeeebe | 954 | |
e6946086 FE |
955 | my $prune_options = $opts->{'prune-backups'}; |
956 | ||
957 | my $backup_limit = 0; | |
b63baa39 | 958 | if (!$prune_options->{'keep-all'}) { |
1db8529e FE |
959 | foreach my $keep (values %{$prune_options}) { |
960 | $backup_limit += $keep; | |
961 | } | |
e6946086 | 962 | } |
899b8373 | 963 | |
bbd4cdd8 | 964 | if (($backup_limit && !$opts->{remove}) || $opts->{protected}) { |
1a87db9e | 965 | my $count; |
bbd4cdd8 | 966 | my $protected_count; |
7bffbd22 | 967 | if (my $storeid = $opts->{storage}) { |
bbd4cdd8 FE |
968 | my @backups = grep { |
969 | !$_->{subtype} || $_->{subtype} eq $vmtype | |
970 | } PVE::Storage::volume_list($cfg, $storeid, $vmid, 'backup')->@*; | |
971 | ||
972 | $count = grep { !$_->{protected} } @backups; | |
973 | $protected_count = scalar(@backups) - $count; | |
1a87db9e | 974 | } else { |
725c0555 | 975 | $count = grep { !$_->{mark} || $_->{mark} ne "protected" } get_backup_file_list($opts->{dumpdir}, $bkname)->@*; |
1a87db9e | 976 | } |
7bffbd22 | 977 | |
bbd4cdd8 FE |
978 | if ($opts->{protected}) { |
979 | my $max_protected = PVE::Storage::get_max_protected_backups( | |
980 | $opts->{scfg}, | |
981 | $opts->{storage}, | |
982 | ); | |
983 | if ($max_protected > -1 && $protected_count >= $max_protected) { | |
984 | die "The number of protected backups per guest is limited to $max_protected ". | |
985 | "on storage '$opts->{storage}'\n"; | |
986 | } | |
987 | } elsif ($count >= $backup_limit) { | |
988 | die "There is a max backup limit of $backup_limit enforced by the target storage ". | |
989 | "or the vzdump parameters. Either increase the limit or delete old backups.\n"; | |
990 | } | |
899b8373 DM |
991 | } |
992 | ||
7ddcb3af | 993 | if (!$self->{opts}->{pbs}) { |
1a87db9e DM |
994 | $task->{logfile} = "$opts->{dumpdir}/$basename.log"; |
995 | } | |
aaeeeebe | 996 | |
757fd3d5 | 997 | my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar'; |
778d5b6d | 998 | my ($comp, $comp_ext) = compressor_info($opts); |
d7550e09 DM |
999 | if ($comp && $comp_ext) { |
1000 | $ext .= ".${comp_ext}"; | |
1001 | } | |
aaeeeebe | 1002 | |
7ddcb3af | 1003 | if ($self->{opts}->{pbs}) { |
1a87db9e | 1004 | die "unable to pipe backup to stdout\n" if $opts->{stdout}; |
b3c3304f | 1005 | $task->{target} = $pbs_snapshot_name; |
aaeeeebe | 1006 | } else { |
1a87db9e | 1007 | if ($opts->{stdout}) { |
6cba1855 | 1008 | $task->{target} = '-'; |
1a87db9e | 1009 | } else { |
6cba1855 | 1010 | $task->{target} = $task->{tmptar} = "$opts->{dumpdir}/$basename$ext"; |
1a87db9e DM |
1011 | $task->{tmptar} =~ s/\.[^\.]+$/\.dat/; |
1012 | unlink $task->{tmptar}; | |
1013 | } | |
aaeeeebe DM |
1014 | } |
1015 | ||
1016 | $task->{vmtype} = $vmtype; | |
1017 | ||
dfb16115 | 1018 | my $pid = $$; |
01fc3672 | 1019 | if ($opts->{tmpdir}) { |
dfb16115 | 1020 | $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp${pid}_$vmid/"; |
01fc3672 | 1021 | } elsif ($self->{opts}->{pbs}) { |
dfb16115 | 1022 | $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid"; |
aaeeeebe DM |
1023 | } else { |
1024 | # dumpdir is posix? then use it as temporary dir | |
5dc86eb8 | 1025 | my $info = get_mount_info($opts->{dumpdir}); |
60e049c2 | 1026 | if ($vmtype eq 'qemu' || |
aaeeeebe DM |
1027 | grep ($_ eq $info->{fstype}, @posix_filesystems)) { |
1028 | $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp"; | |
1029 | } else { | |
dfb16115 | 1030 | $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid"; |
aaeeeebe DM |
1031 | debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" . |
1032 | "using $task->{tmpdir} for temporary files", $logfd); | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | rmtree $task->{tmpdir}; | |
1037 | mkdir $task->{tmpdir}; | |
1038 | -d $task->{tmpdir} || | |
1039 | die "unable to create temporary directory '$task->{tmpdir}'"; | |
1040 | ||
1041 | $logfd = IO::File->new (">$tmplog") || | |
1042 | die "unable to create log file '$tmplog'"; | |
1043 | ||
1044 | $task->{dumpdir} = $opts->{dumpdir}; | |
ff00abe6 | 1045 | $task->{storeid} = $opts->{storage}; |
1a87db9e | 1046 | $task->{scfg} = $opts->{scfg}; |
aaeeeebe DM |
1047 | $task->{tmplog} = $tmplog; |
1048 | ||
1a87db9e | 1049 | unlink $task->{logfile} if defined($task->{logfile}); |
aaeeeebe | 1050 | |
29f26f6d DJ |
1051 | debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1); |
1052 | debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime())); | |
aaeeeebe DM |
1053 | |
1054 | $plugin->set_logfd ($logfd); | |
1055 | ||
1056 | # test is VM is running | |
1057 | my ($running, $status_text) = $plugin->vm_status ($vmid); | |
1058 | ||
1059 | debugmsg ('info', "status = ${status_text}", $logfd); | |
1060 | ||
1061 | # lock VM (prevent config changes) | |
1062 | $plugin->lock_vm ($vmid); | |
1063 | ||
1064 | $cleanup->{unlock} = 1; | |
1065 | ||
1066 | # prepare | |
1067 | ||
6acb632a | 1068 | my $mode = $running ? $task->{mode} : 'stop'; |
aaeeeebe DM |
1069 | |
1070 | if ($mode eq 'snapshot') { | |
1071 | my %saved_task = %$task; | |
1072 | eval { $plugin->prepare ($task, $vmid, $mode); }; | |
1073 | if (my $err = $@) { | |
1074 | die $err if $err !~ m/^mode failure/; | |
1075 | debugmsg ('info', $err, $logfd); | |
1076 | debugmsg ('info', "trying 'suspend' mode instead", $logfd); | |
1077 | $mode = 'suspend'; # so prepare is called again below | |
60e049c2 | 1078 | %$task = %saved_task; |
aaeeeebe DM |
1079 | } |
1080 | } | |
1081 | ||
6acb632a WB |
1082 | $cleanup->{prepared} = 1; |
1083 | ||
aaeeeebe DM |
1084 | $task->{mode} = $mode; |
1085 | ||
1086 | debugmsg ('info', "backup mode: $mode", $logfd); | |
ff119724 | 1087 | debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd) if $opts->{bwlimit}; |
aaeeeebe DM |
1088 | debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd); |
1089 | ||
1090 | if ($mode eq 'stop') { | |
aaeeeebe DM |
1091 | $plugin->prepare ($task, $vmid, $mode); |
1092 | ||
1093 | $self->run_hook_script ('backup-start', $task, $logfd); | |
1094 | ||
1095 | if ($running) { | |
4922b7a9 | 1096 | debugmsg ('info', "stopping virtual guest", $logfd); |
4e0947c8 | 1097 | $task->{vmstoptime} = time(); |
aaeeeebe DM |
1098 | $self->run_hook_script ('pre-stop', $task, $logfd); |
1099 | $plugin->stop_vm ($task, $vmid); | |
1100 | $cleanup->{restart} = 1; | |
1101 | } | |
60e049c2 | 1102 | |
aaeeeebe DM |
1103 | |
1104 | } elsif ($mode eq 'suspend') { | |
aaeeeebe DM |
1105 | $plugin->prepare ($task, $vmid, $mode); |
1106 | ||
1107 | $self->run_hook_script ('backup-start', $task, $logfd); | |
1108 | ||
8187f6b0 DM |
1109 | if ($vmtype eq 'lxc') { |
1110 | # pre-suspend rsync | |
1111 | $plugin->copy_data_phase1($task, $vmid); | |
1112 | } | |
1113 | ||
ef7d3fdd | 1114 | debugmsg ('info', "suspending guest", $logfd); |
4e0947c8 | 1115 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
1116 | $self->run_hook_script ('pre-stop', $task, $logfd); |
1117 | $plugin->suspend_vm ($task, $vmid); | |
1118 | $cleanup->{resume} = 1; | |
1119 | ||
8187f6b0 DM |
1120 | if ($vmtype eq 'lxc') { |
1121 | # post-suspend rsync | |
1122 | $plugin->copy_data_phase2($task, $vmid); | |
1123 | ||
ef7d3fdd | 1124 | debugmsg ('info', "resuming guest", $logfd); |
8187f6b0 DM |
1125 | $cleanup->{resume} = 0; |
1126 | $self->run_hook_script('pre-restart', $task, $logfd); | |
1127 | $plugin->resume_vm($task, $vmid); | |
d5047243 | 1128 | $self->run_hook_script('post-restart', $task, $logfd); |
4e0947c8 | 1129 | $log_vm_online_again->(); |
8187f6b0 | 1130 | } |
60e049c2 | 1131 | |
aaeeeebe | 1132 | } elsif ($mode eq 'snapshot') { |
c5be0f8c DM |
1133 | $self->run_hook_script ('backup-start', $task, $logfd); |
1134 | ||
aaeeeebe DM |
1135 | my $snapshot_count = $task->{snapshot_count} || 0; |
1136 | ||
1137 | $self->run_hook_script ('pre-stop', $task, $logfd); | |
1138 | ||
1139 | if ($snapshot_count > 1) { | |
1140 | debugmsg ('info', "suspend vm to make snapshot", $logfd); | |
4e0947c8 | 1141 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
1142 | $plugin->suspend_vm ($task, $vmid); |
1143 | $cleanup->{resume} = 1; | |
1144 | } | |
1145 | ||
1146 | $plugin->snapshot ($task, $vmid); | |
1147 | ||
1148 | $self->run_hook_script ('pre-restart', $task, $logfd); | |
1149 | ||
1150 | if ($snapshot_count > 1) { | |
1151 | debugmsg ('info', "resume vm", $logfd); | |
1152 | $cleanup->{resume} = 0; | |
1153 | $plugin->resume_vm ($task, $vmid); | |
4e0947c8 | 1154 | $log_vm_online_again->(); |
aaeeeebe DM |
1155 | } |
1156 | ||
d5047243 FG |
1157 | $self->run_hook_script ('post-restart', $task, $logfd); |
1158 | ||
aaeeeebe DM |
1159 | } else { |
1160 | die "internal error - unknown mode '$mode'\n"; | |
1161 | } | |
1162 | ||
1163 | # assemble archive image | |
1164 | $plugin->assemble ($task, $vmid); | |
60e049c2 TM |
1165 | |
1166 | # produce archive | |
aaeeeebe DM |
1167 | |
1168 | if ($opts->{stdout}) { | |
1169 | debugmsg ('info', "sending archive to stdout", $logfd); | |
d7550e09 | 1170 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe DM |
1171 | $self->run_hook_script ('backup-end', $task, $logfd); |
1172 | return; | |
1173 | } | |
1174 | ||
b3c3304f TL |
1175 | my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump'; |
1176 | debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd); | |
d7550e09 | 1177 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe | 1178 | |
7ddcb3af | 1179 | if ($self->{opts}->{pbs}) { |
e21a31a7 | 1180 | # size is added to task struct in guest vzdump plugins |
1a87db9e | 1181 | } else { |
6cba1855 TL |
1182 | rename ($task->{tmptar}, $task->{target}) || |
1183 | die "unable to rename '$task->{tmptar}' to '$task->{target}'\n"; | |
aaeeeebe | 1184 | |
1a87db9e | 1185 | # determine size |
6cba1855 | 1186 | $task->{size} = (-s $task->{target}) || 0; |
1a87db9e DM |
1187 | my $cs = format_size ($task->{size}); |
1188 | debugmsg ('info', "archive file size: $cs", $logfd); | |
1189 | } | |
aaeeeebe | 1190 | |
bbd4cdd8 FE |
1191 | # Mark as protected before pruning. |
1192 | if (my $storeid = $opts->{storage}) { | |
1193 | my $volname = $opts->{pbs} ? $task->{target} : basename($task->{target}); | |
1194 | my $volid = "${storeid}:backup/${volname}"; | |
1195 | ||
e01438a7 FE |
1196 | if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') { |
1197 | debugmsg('info', "adding notes to backup", $logfd); | |
31213d61 FE |
1198 | my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); }; |
1199 | if (my $err = $@) { | |
1200 | debugmsg('warn', "unable to add notes - $err", $logfd); | |
1201 | } else { | |
1202 | eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'notes', $notes) }; | |
1203 | debugmsg('warn', "unable to add notes - $@", $logfd) if $@; | |
1204 | } | |
e01438a7 FE |
1205 | } |
1206 | ||
bbd4cdd8 FE |
1207 | if ($opts->{protected}) { |
1208 | debugmsg('info', "marking backup as protected", $logfd); | |
1209 | eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'protected', 1) }; | |
1210 | die "unable to set protected flag - $@\n" if $@; | |
1211 | } | |
1212 | } | |
1213 | ||
e6946086 | 1214 | if ($opts->{remove}) { |
ca155141 TL |
1215 | my $keepstr = join(', ', map { "$_=$prune_options->{$_}" } sort keys %$prune_options); |
1216 | debugmsg ('info', "prune older backups with retention: $keepstr", $logfd); | |
b4546c7e | 1217 | my $pruned = 0; |
f354fe47 | 1218 | if (!defined($opts->{storage})) { |
725c0555 | 1219 | my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname); |
2fc939a9 | 1220 | |
f354fe47 | 1221 | PVE::Storage::prune_mark_backup_group($bklist, $prune_options); |
e6946086 | 1222 | |
f354fe47 FE |
1223 | foreach my $prune_entry (@{$bklist}) { |
1224 | next if $prune_entry->{mark} ne 'remove'; | |
b4546c7e | 1225 | $pruned++; |
f354fe47 FE |
1226 | my $archive_path = $prune_entry->{path}; |
1227 | debugmsg ('info', "delete old backup '$archive_path'", $logfd); | |
1228 | PVE::Storage::archive_remove($archive_path); | |
1a87db9e | 1229 | } |
f354fe47 | 1230 | } else { |
b4546c7e TL |
1231 | my $pruned_list = PVE::Storage::prune_backups( |
1232 | $cfg, | |
1233 | $opts->{storage}, | |
1234 | $prune_options, | |
1235 | $vmid, | |
1236 | $vmtype, | |
1237 | 0, | |
1238 | sub { debugmsg($_[0], $_[1], $logfd) }, | |
1239 | ); | |
1240 | $pruned = scalar(grep { $_->{mark} eq 'remove' } $pruned_list->@*); | |
aaeeeebe | 1241 | } |
b4546c7e TL |
1242 | my $log_pruned_extra = $pruned > 0 ? " not covered by keep-retention policy" : ""; |
1243 | debugmsg ('info', "pruned $pruned backup(s)${log_pruned_extra}", $logfd); | |
aaeeeebe DM |
1244 | } |
1245 | ||
1246 | $self->run_hook_script ('backup-end', $task, $logfd); | |
1247 | }; | |
1248 | my $err = $@; | |
1249 | ||
1250 | if ($plugin) { | |
1251 | # clean-up | |
1252 | ||
1253 | if ($cleanup->{unlock}) { | |
1254 | eval { $plugin->unlock_vm ($vmid); }; | |
1255 | warn $@ if $@; | |
1256 | } | |
1257 | ||
6acb632a | 1258 | if ($cleanup->{prepared}) { |
ca2605e7 DM |
1259 | # only call cleanup when necessary (when prepare was executed) |
1260 | eval { $plugin->cleanup ($task, $vmid) }; | |
1261 | warn $@ if $@; | |
1262 | } | |
aaeeeebe DM |
1263 | |
1264 | eval { $plugin->set_logfd (undef); }; | |
1265 | warn $@ if $@; | |
1266 | ||
60e049c2 TM |
1267 | if ($cleanup->{resume} || $cleanup->{restart}) { |
1268 | eval { | |
aaeeeebe DM |
1269 | $self->run_hook_script ('pre-restart', $task, $logfd); |
1270 | if ($cleanup->{resume}) { | |
1271 | debugmsg ('info', "resume vm", $logfd); | |
1272 | $plugin->resume_vm ($task, $vmid); | |
1273 | } else { | |
757fd3d5 DM |
1274 | my $running = $plugin->vm_status($vmid); |
1275 | if (!$running) { | |
1276 | debugmsg ('info', "restarting vm", $logfd); | |
1277 | $plugin->start_vm ($task, $vmid); | |
1278 | } | |
d5047243 FG |
1279 | } |
1280 | $self->run_hook_script ('post-restart', $task, $logfd); | |
aaeeeebe DM |
1281 | }; |
1282 | my $err = $@; | |
1283 | if ($err) { | |
1284 | warn $err; | |
1285 | } else { | |
4e0947c8 | 1286 | $log_vm_online_again->(); |
aaeeeebe DM |
1287 | } |
1288 | } | |
1289 | } | |
1290 | ||
1291 | eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; }; | |
1292 | warn $@ if $@; | |
1293 | ||
fe6643b6 | 1294 | eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; }; |
aaeeeebe DM |
1295 | warn $@ if $@; |
1296 | ||
1297 | my $delay = $task->{backuptime} = time () - $vmstarttime; | |
1298 | ||
1299 | if ($err) { | |
1300 | $task->{state} = 'err'; | |
1301 | $task->{msg} = $err; | |
1302 | debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1); | |
29f26f6d | 1303 | debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1304 | |
1305 | eval { $self->run_hook_script ('backup-abort', $task, $logfd); }; | |
bd41a16d | 1306 | debugmsg('warn', $@) if $@; # message already contains command with phase name |
aaeeeebe DM |
1307 | |
1308 | } else { | |
1309 | $task->{state} = 'ok'; | |
1310 | my $tstr = format_time ($delay); | |
1311 | debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1); | |
29f26f6d | 1312 | debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1313 | } |
1314 | ||
1315 | close ($logfd) if $logfd; | |
60e049c2 | 1316 | |
1a87db9e | 1317 | if ($task->{tmplog}) { |
7ddcb3af | 1318 | if ($self->{opts}->{pbs}) { |
1a87db9e | 1319 | if ($task->{state} eq 'ok') { |
e8d9e1ed TL |
1320 | eval { |
1321 | PVE::Storage::PBSPlugin::run_raw_client_cmd( | |
1322 | $opts->{scfg}, | |
1323 | $opts->{storage}, | |
1324 | 'upload-log', | |
1325 | [ $pbs_snapshot_name, $task->{tmplog} ], | |
1326 | errmsg => "uploading backup task log failed", | |
f332a167 | 1327 | outfunc => sub {}, |
e8d9e1ed TL |
1328 | ); |
1329 | }; | |
1330 | debugmsg('warn', "$@") if $@; # $@ contains already error prefix | |
1a87db9e DM |
1331 | } |
1332 | } elsif ($task->{logfile}) { | |
1333 | system {'cp'} 'cp', $task->{tmplog}, $task->{logfile}; | |
1334 | } | |
aaeeeebe DM |
1335 | } |
1336 | ||
1337 | eval { $self->run_hook_script ('log-end', $task); }; | |
bd41a16d | 1338 | debugmsg('warn', $@) if $@; # message already contains command with phase name |
aaeeeebe DM |
1339 | |
1340 | die $err if $err && $err =~ m/^interrupted by signal$/; | |
1341 | } | |
1342 | ||
1343 | sub exec_backup { | |
d7550e09 | 1344 | my ($self, $rpcenv, $authuser) = @_; |
aaeeeebe DM |
1345 | |
1346 | my $opts = $self->{opts}; | |
1347 | ||
1348 | debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1); | |
f647d0fc FE |
1349 | |
1350 | if (scalar(@{$self->{skiplist}})) { | |
1351 | my $skip_string = join(', ', sort { $a <=> $b } @{$self->{skiplist}}); | |
1352 | debugmsg ('info', "skip external VMs: $skip_string"); | |
1353 | } | |
60e049c2 | 1354 | |
aaeeeebe | 1355 | my $tasklist = []; |
df5875b4 AL |
1356 | my $vzdump_plugins = {}; |
1357 | foreach my $plugin (@{$self->{plugins}}) { | |
1358 | my $type = $plugin->type(); | |
1359 | next if exists $vzdump_plugins->{$type}; | |
1360 | $vzdump_plugins->{$type} = $plugin; | |
1361 | } | |
aaeeeebe | 1362 | |
df5875b4 | 1363 | my $vmlist = PVE::Cluster::get_vmlist(); |
4d73f0e2 FE |
1364 | my $vmids = [ sort { $a <=> $b } @{$opts->{vmids}} ]; |
1365 | foreach my $vmid (@{$vmids}) { | |
7f874148 FE |
1366 | my $plugin; |
1367 | if (defined($vmlist->{ids}->{$vmid})) { | |
1368 | my $guest_type = $vmlist->{ids}->{$vmid}->{type}; | |
1369 | $plugin = $vzdump_plugins->{$guest_type}; | |
1370 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all}); | |
1371 | } | |
df5875b4 | 1372 | push @$tasklist, { |
f839f3d1 | 1373 | mode => $opts->{mode}, |
df5875b4 | 1374 | plugin => $plugin, |
f839f3d1 TL |
1375 | state => 'todo', |
1376 | vmid => $vmid, | |
1377 | }; | |
aaeeeebe DM |
1378 | } |
1379 | ||
44b21574 WB |
1380 | # Use in-memory files for the outer hook logs to pass them to sendmail. |
1381 | my $job_start_log = ''; | |
1382 | my $job_end_log = ''; | |
1383 | open my $job_start_fd, '>', \$job_start_log; | |
1384 | open my $job_end_fd, '>', \$job_end_log; | |
1385 | ||
aaeeeebe DM |
1386 | my $starttime = time(); |
1387 | my $errcount = 0; | |
1388 | eval { | |
1389 | ||
44b21574 | 1390 | $self->run_hook_script ('job-start', undef, $job_start_fd); |
aaeeeebe DM |
1391 | |
1392 | foreach my $task (@$tasklist) { | |
1393 | $self->exec_backup_task ($task); | |
1394 | $errcount += 1 if $task->{state} ne 'ok'; | |
1395 | } | |
1396 | ||
44b21574 | 1397 | $self->run_hook_script ('job-end', undef, $job_end_fd); |
aaeeeebe DM |
1398 | }; |
1399 | my $err = $@; | |
1400 | ||
aaeeeebe | 1401 | if ($err) { |
573e8320 FE |
1402 | eval { $self->run_hook_script ('job-abort', undef, $job_end_fd); }; |
1403 | $err .= $@ if $@; | |
aaeeeebe DM |
1404 | debugmsg ('err', "Backup job failed - $err", undef, 1); |
1405 | } else { | |
1406 | if ($errcount) { | |
1407 | debugmsg ('info', "Backup job finished with errors", undef, 1); | |
1408 | } else { | |
61ca4432 | 1409 | debugmsg ('info', "Backup job finished successfully", undef, 1); |
aaeeeebe DM |
1410 | } |
1411 | } | |
1412 | ||
44b21574 WB |
1413 | close $job_start_fd; |
1414 | close $job_end_fd; | |
1415 | ||
aaeeeebe DM |
1416 | my $totaltime = time() - $starttime; |
1417 | ||
b241deb7 | 1418 | eval { |
c4afde55 | 1419 | # otherwise $self->send_notification() will interpret it as multiple problems |
268c6428 FE |
1420 | my $chomped_err = $err; |
1421 | chomp($chomped_err) if $chomped_err; | |
1422 | ||
c4afde55 | 1423 | $self->send_notification( |
b241deb7 FE |
1424 | $tasklist, |
1425 | $totaltime, | |
268c6428 | 1426 | $chomped_err, |
b241deb7 FE |
1427 | $self->{job_init_log} . $job_start_log, |
1428 | $job_end_log, | |
1429 | ); | |
1430 | }; | |
aaeeeebe | 1431 | debugmsg ('err', $@) if $@; |
4a4051d8 DM |
1432 | |
1433 | die $err if $err; | |
1434 | ||
60e049c2 | 1435 | die "job errors\n" if $errcount; |
8682f6fc WL |
1436 | |
1437 | unlink $pidfile; | |
aaeeeebe DM |
1438 | } |
1439 | ||
ac27b58d | 1440 | |
47664cbe DM |
1441 | sub option_exists { |
1442 | my $key = shift; | |
1443 | return defined($confdesc->{$key}); | |
1444 | } | |
1445 | ||
f8ed6af8 FE |
1446 | # NOTE it might make sense to merge this and verify_vzdump_parameters(), but one |
1447 | # needs to adapt command_line() in guest-common's PVE/VZDump/Common.pm and detect | |
1448 | # a second parsing attempt, because verify_vzdump_parameters() is called twice | |
1449 | # during the update_job API call. | |
1450 | sub parse_mailto_exclude_path { | |
1451 | my ($param) = @_; | |
1452 | ||
d6d3e55b | 1453 | # exclude-path list need to be 0 separated or be an array |
f8ed6af8 | 1454 | if (defined($param->{'exclude-path'})) { |
d6d3e55b DC |
1455 | my $expaths; |
1456 | if (ref($param->{'exclude-path'}) eq 'ARRAY') { | |
1457 | $expaths = $param->{'exclude-path'}; | |
1458 | } else { | |
1459 | $expaths = [split(/\0/, $param->{'exclude-path'} || '')]; | |
1460 | } | |
1461 | $param->{'exclude-path'} = $expaths; | |
f8ed6af8 FE |
1462 | } |
1463 | ||
1464 | if (defined($param->{mailto})) { | |
1465 | my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto')); | |
1466 | $param->{mailto} = [ @mailto ]; | |
1467 | } | |
1468 | ||
1469 | return; | |
1470 | } | |
1471 | ||
31aef761 DM |
1472 | sub verify_vzdump_parameters { |
1473 | my ($param, $check_missing) = @_; | |
1474 | ||
1475 | raise_param_exc({ all => "option conflicts with option 'vmid'"}) | |
eab837c4 | 1476 | if $param->{all} && $param->{vmid}; |
31aef761 DM |
1477 | |
1478 | raise_param_exc({ exclude => "option conflicts with option 'vmid'"}) | |
1479 | if $param->{exclude} && $param->{vmid}; | |
1480 | ||
f3376261 TM |
1481 | raise_param_exc({ pool => "option conflicts with option 'vmid'"}) |
1482 | if $param->{pool} && $param->{vmid}; | |
1483 | ||
e6946086 FE |
1484 | raise_param_exc({ 'prune-backups' => "option conflicts with option 'maxfiles'"}) |
1485 | if defined($param->{'prune-backups'}) && defined($param->{maxfiles}); | |
1486 | ||
1c56bcf3 | 1487 | $parse_prune_backups_maxfiles->($param, 'CLI parameters'); |
98cb465a | 1488 | parse_fleecing($param); |
93880785 | 1489 | parse_performance($param); |
e6946086 | 1490 | |
facf65a6 FE |
1491 | if (my $template = $param->{'notes-template'}) { |
1492 | eval { $verify_notes_template->($template); }; | |
1493 | raise_param_exc({'notes-template' => $@}) if $@; | |
1494 | } | |
010ff16e | 1495 | |
f3376261 | 1496 | $param->{all} = 1 if (defined($param->{exclude}) && !$param->{pool}); |
31aef761 DM |
1497 | |
1498 | return if !$check_missing; | |
1499 | ||
1500 | raise_param_exc({ vmid => "property is missing"}) | |
f3376261 | 1501 | if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid}; |
8682f6fc WL |
1502 | |
1503 | } | |
1504 | ||
eab837c4 | 1505 | sub stop_running_backups { |
8682f6fc WL |
1506 | my($self) = @_; |
1507 | ||
eab837c4 DM |
1508 | my $upid = PVE::Tools::file_read_firstline($pidfile); |
1509 | return if !$upid; | |
8682f6fc | 1510 | |
eab837c4 | 1511 | my $task = PVE::Tools::upid_decode($upid); |
8682f6fc | 1512 | |
60e049c2 | 1513 | if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) && |
eab837c4 DM |
1514 | PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) { |
1515 | kill(15, $task->{pid}); | |
1516 | # wait max 15 seconds to shut down (else, do nothing for now) | |
1517 | my $i; | |
1518 | for ($i = 15; $i > 0; $i--) { | |
1519 | last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart})); | |
1520 | sleep (1); | |
1521 | } | |
76189130 | 1522 | die "stopping backup process $task->{pid} failed\n" if $i == 0; |
8682f6fc | 1523 | } |
31aef761 DM |
1524 | } |
1525 | ||
5c4da4c3 AL |
1526 | sub get_included_guests { |
1527 | my ($job) = @_; | |
1528 | ||
5c4da4c3 | 1529 | my $vmids = []; |
df5875b4 AL |
1530 | my $vmids_per_node = {}; |
1531 | ||
1532 | my $vmlist = PVE::Cluster::get_vmlist(); | |
5c4da4c3 | 1533 | |
5c4da4c3 AL |
1534 | if ($job->{pool}) { |
1535 | $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool}); | |
df5875b4 | 1536 | } elsif ($job->{vmid}) { |
05447e04 | 1537 | $vmids = [ split_list($job->{vmid}) ]; |
da7cb515 | 1538 | } elsif ($job->{all}) { |
df5875b4 | 1539 | # all or exclude |
05447e04 TL |
1540 | my $exclude = check_vmids(split_list($job->{exclude})); |
1541 | my $excludehash = { map { $_ => 1 } @$exclude }; | |
df5875b4 | 1542 | |
05447e04 | 1543 | for my $id (keys %{$vmlist->{ids}}) { |
df5875b4 AL |
1544 | next if $excludehash->{$id}; |
1545 | push @$vmids, $id; | |
1546 | } | |
da7cb515 AL |
1547 | } else { |
1548 | return $vmids_per_node; | |
5c4da4c3 | 1549 | } |
50ba40ec | 1550 | $vmids = check_vmids(@$vmids); |
5c4da4c3 | 1551 | |
50ba40ec | 1552 | for my $vmid (@$vmids) { |
7f874148 FE |
1553 | if (defined($vmlist->{ids}->{$vmid})) { |
1554 | my $node = $vmlist->{ids}->{$vmid}->{node}; | |
1555 | next if (defined $job->{node} && $job->{node} ne $node); | |
50ba40ec | 1556 | |
7f874148 FE |
1557 | push @{$vmids_per_node->{$node}}, $vmid; |
1558 | } else { | |
1559 | push @{$vmids_per_node->{''}}, $vmid; | |
1560 | } | |
5c4da4c3 AL |
1561 | } |
1562 | ||
df5875b4 | 1563 | return $vmids_per_node; |
5c4da4c3 AL |
1564 | } |
1565 | ||
aaeeeebe | 1566 | 1; |