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