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