]>
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; |
5c4da4c3 AL |
24 | use PVE::Tools qw(extract_param); |
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 = []; | |
169 | foreach my $vmid (@vmids) { | |
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 FG |
200 | if (defined($res->{mailto})) { |
201 | my @mailto = PVE::Tools::split_list($res->{mailto}); | |
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 DM |
451 | if (defined($opts->{storage}) && $opts->{stdout}) { |
452 | die "unable to use option 'storage' with option 'stdout'\n"; | |
453 | } | |
454 | ||
aaeeeebe | 455 | if (!$opts->{dumpdir} && !$opts->{storage}) { |
19d5c0f2 | 456 | $opts->{storage} = 'local'; |
aaeeeebe DM |
457 | } |
458 | ||
403761c4 WB |
459 | my $errors = ''; |
460 | ||
aaeeeebe | 461 | if ($opts->{storage}) { |
1e4583d5 | 462 | my $info = eval { storage_info ($opts->{storage}) }; |
c3b58274 FG |
463 | $errors .= "could not get storage information for '$opts->{storage}': $@" |
464 | if ($@); | |
aaeeeebe | 465 | $opts->{dumpdir} = $info->{dumpdir}; |
1a87db9e | 466 | $opts->{scfg} = $info->{scfg}; |
7ddcb3af | 467 | $opts->{pbs} = $info->{pbs}; |
1e4583d5 | 468 | $maxfiles //= $info->{maxfiles}; |
aaeeeebe | 469 | } elsif ($opts->{dumpdir}) { |
403761c4 | 470 | $errors .= "dumpdir '$opts->{dumpdir}' does not exist" |
aaeeeebe DM |
471 | if ! -d $opts->{dumpdir}; |
472 | } else { | |
60e049c2 | 473 | die "internal error"; |
aaeeeebe DM |
474 | } |
475 | ||
476 | if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) { | |
403761c4 WB |
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"; | |
aaeeeebe DM |
485 | } |
486 | ||
84ad4385 | 487 | $opts->{maxfiles} = $maxfiles if defined($maxfiles); |
899b8373 | 488 | |
aaeeeebe DM |
489 | return $self; |
490 | ||
491 | } | |
492 | ||
aaeeeebe DM |
493 | sub get_mount_info { |
494 | my ($dir) = @_; | |
495 | ||
8572d646 DM |
496 | # Note: df 'available' can be negative, and percentage set to '-' |
497 | ||
d93d0459 | 498 | my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir]; |
aaeeeebe | 499 | |
d93d0459 | 500 | my $res; |
aaeeeebe | 501 | |
d93d0459 DM |
502 | my $parser = sub { |
503 | my $line = shift; | |
8572d646 DM |
504 | if (my ($fsid, $fstype, undef, $mp) = $line =~ |
505 | m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) { | |
d93d0459 DM |
506 | $res = { |
507 | device => $fsid, | |
508 | fstype => $fstype, | |
509 | mountpoint => $mp, | |
510 | }; | |
511 | } | |
aaeeeebe | 512 | }; |
d93d0459 DM |
513 | |
514 | eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); }; | |
515 | warn $@ if $@; | |
516 | ||
517 | return $res; | |
aaeeeebe DM |
518 | } |
519 | ||
aaeeeebe | 520 | sub getlock { |
eab837c4 | 521 | my ($self, $upid) = @_; |
aaeeeebe | 522 | |
8682f6fc | 523 | my $fh; |
60e049c2 | 524 | |
aaeeeebe | 525 | my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait}; |
60e049c2 | 526 | |
eab837c4 DM |
527 | die "missimg UPID" if !$upid; # should not happen |
528 | ||
aaeeeebe DM |
529 | if (!open (SERVER_FLCK, ">>$lockfile")) { |
530 | debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1); | |
6ec9de44 | 531 | die "can't open lock on file '$lockfile' - $!"; |
aaeeeebe DM |
532 | } |
533 | ||
eab837c4 | 534 | if (!flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) { |
aaeeeebe | 535 | |
eab837c4 | 536 | if (!$maxwait) { |
76189130 TL |
537 | debugmsg ('err', "can't acquire lock '$lockfile' (wait = 0)", undef, 1); |
538 | die "can't acquire lock '$lockfile' (wait = 0)"; | |
eab837c4 | 539 | } |
aaeeeebe | 540 | |
eab837c4 | 541 | debugmsg('info', "trying to get global lock - waiting...", undef, 1); |
aaeeeebe | 542 | |
eab837c4 DM |
543 | eval { |
544 | alarm ($maxwait * 60); | |
60e049c2 | 545 | |
eab837c4 | 546 | local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; }; |
aaeeeebe | 547 | |
eab837c4 DM |
548 | if (!flock (SERVER_FLCK, LOCK_EX)) { |
549 | my $err = $!; | |
550 | close (SERVER_FLCK); | |
551 | alarm (0); | |
552 | die "$err\n"; | |
553 | } | |
aaeeeebe | 554 | alarm (0); |
eab837c4 | 555 | }; |
aaeeeebe | 556 | alarm (0); |
60e049c2 | 557 | |
eab837c4 | 558 | my $err = $@; |
60e049c2 | 559 | |
eab837c4 | 560 | if ($err) { |
76189130 TL |
561 | debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1); |
562 | die "can't acquire lock '$lockfile' - $err"; | |
eab837c4 | 563 | } |
aaeeeebe | 564 | |
eab837c4 | 565 | debugmsg('info', "got global lock", undef, 1); |
aaeeeebe DM |
566 | } |
567 | ||
eab837c4 | 568 | PVE::Tools::file_set_contents($pidfile, $upid); |
aaeeeebe DM |
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}; | |
aaeeeebe DM |
577 | return if !$script; |
578 | ||
59798893 | 579 | if (!-x $script) { |
3d45fe51 | 580 | die "The hook script '$script' is not executable.\n"; |
59798893 MA |
581 | } |
582 | ||
aaeeeebe DM |
583 | my $cmd = "$script $phase"; |
584 | ||
585 | $cmd .= " $task->{mode} $task->{vmid}" if ($task); | |
586 | ||
587 | local %ENV; | |
28272ca2 DM |
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)) { | |
aaeeeebe DM |
593 | $ENV{uc($ek)} = $task->{$ek} if $task->{$ek}; |
594 | } | |
595 | ||
596 | run_command ($logfd, $cmd); | |
597 | } | |
598 | ||
d7550e09 | 599 | sub compressor_info { |
778d5b6d TL |
600 | my ($opts) = @_; |
601 | my $opt_compress = $opts->{compress}; | |
d7550e09 DM |
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') { | |
778d5b6d | 608 | if ($opts->{pigz} > 0) { |
8555b100 TL |
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 | } | |
819e4ff5 | 614 | return ("pigz -p ${pigz_threads} --rsyncable", 'gz'); |
778d5b6d | 615 | } else { |
e953f92a | 616 | return ('gzip --rsyncable', 'gz'); |
778d5b6d | 617 | } |
3e2c5105 AA |
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 | } | |
7420d7ff | 624 | return ("zstd --rsyncable --threads=${zstd_threads}", 'zst'); |
d7550e09 DM |
625 | } else { |
626 | die "internal error - unknown compression option '$opt_compress'"; | |
627 | } | |
628 | } | |
899b8373 DM |
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; | |
3e2c5105 | 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}))?)))$!) { |
899b8373 | 637 | $fn = "$dir/$1"; # untaint |
998e4eeb | 638 | my $t = timelocal ($7, $6, $5, $4, $3 - 1, $2); |
899b8373 DM |
639 | push @$bklist, [$fn, $t]; |
640 | } | |
641 | } | |
642 | ||
643 | return $bklist; | |
644 | } | |
60e049c2 | 645 | |
aaeeeebe DM |
646 | sub exec_backup_task { |
647 | my ($self, $task) = @_; | |
60e049c2 | 648 | |
aaeeeebe DM |
649 | my $opts = $self->{opts}; |
650 | ||
651 | my $vmid = $task->{vmid}; | |
652 | my $plugin = $task->{plugin}; | |
1a87db9e DM |
653 | my $vmtype = $plugin->type(); |
654 | ||
655 | $task->{backup_time} = time(); | |
656 | ||
657 | my $pbs_group_name; | |
658 | my $pbs_snapshot_name; | |
659 | ||
7ddcb3af | 660 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
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 | } | |
aaeeeebe DM |
671 | |
672 | my $vmstarttime = time (); | |
60e049c2 | 673 | |
aaeeeebe DM |
674 | my $logfd; |
675 | ||
676 | my $cleanup = {}; | |
677 | ||
4e0947c8 TL |
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 | }; | |
aaeeeebe DM |
684 | |
685 | eval { | |
686 | die "unable to find VM '$vmid'\n" if !$plugin; | |
687 | ||
2de3ee58 | 688 | # for now we deny backups of a running ha managed service in *stop* mode |
83ec8f81 | 689 | # as it interferes with the HA stack (started services should not stop). |
2de3ee58 | 690 | if ($opts->{mode} eq 'stop' && |
83ec8f81 | 691 | PVE::HA::Config::vm_is_ha_managed($vmid, 'started')) |
2de3ee58 TL |
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 | ||
aaeeeebe DM |
697 | my $tmplog = "$logdir/$vmtype-$vmid.log"; |
698 | ||
aaeeeebe | 699 | my $bkname = "vzdump-$vmtype-$vmid"; |
1a87db9e | 700 | my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time})); |
aaeeeebe | 701 | |
899b8373 DM |
702 | my $maxfiles = $opts->{maxfiles}; |
703 | ||
704 | if ($maxfiles && !$opts->{remove}) { | |
1a87db9e | 705 | my $count; |
7ddcb3af | 706 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
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 | } | |
6c09101a | 713 | die "There is a max backup limit of ($maxfiles) enforced by the". |
1a87db9e DM |
714 | " target storage or the vzdump parameters.". |
715 | " Either increase the limit or delete old backup(s).\n" | |
716 | if $count >= $maxfiles; | |
899b8373 DM |
717 | } |
718 | ||
7ddcb3af | 719 | if (!$self->{opts}->{pbs}) { |
1a87db9e DM |
720 | $task->{logfile} = "$opts->{dumpdir}/$basename.log"; |
721 | } | |
aaeeeebe | 722 | |
757fd3d5 | 723 | my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar'; |
778d5b6d | 724 | my ($comp, $comp_ext) = compressor_info($opts); |
d7550e09 DM |
725 | if ($comp && $comp_ext) { |
726 | $ext .= ".${comp_ext}"; | |
727 | } | |
aaeeeebe | 728 | |
7ddcb3af | 729 | if ($self->{opts}->{pbs}) { |
1a87db9e | 730 | die "unable to pipe backup to stdout\n" if $opts->{stdout}; |
aaeeeebe | 731 | } else { |
1a87db9e DM |
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 | } | |
aaeeeebe DM |
740 | } |
741 | ||
742 | $task->{vmtype} = $vmtype; | |
743 | ||
7ddcb3af | 744 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
745 | $task->{tmpdir} = "/var/tmp/vzdumptmp$$"; #fixme |
746 | } elsif ($opts->{tmpdir}) { | |
60e049c2 | 747 | $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$"; |
aaeeeebe DM |
748 | } else { |
749 | # dumpdir is posix? then use it as temporary dir | |
5dc86eb8 | 750 | my $info = get_mount_info($opts->{dumpdir}); |
60e049c2 | 751 | if ($vmtype eq 'qemu' || |
aaeeeebe DM |
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}; | |
ff00abe6 | 770 | $task->{storeid} = $opts->{storage}; |
1a87db9e | 771 | $task->{scfg} = $opts->{scfg}; |
aaeeeebe DM |
772 | $task->{tmplog} = $tmplog; |
773 | ||
1a87db9e | 774 | unlink $task->{logfile} if defined($task->{logfile}); |
aaeeeebe | 775 | |
29f26f6d DJ |
776 | debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1); |
777 | debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime())); | |
aaeeeebe DM |
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 | ||
6acb632a | 793 | my $mode = $running ? $task->{mode} : 'stop'; |
aaeeeebe DM |
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 | |
60e049c2 | 803 | %$task = %saved_task; |
aaeeeebe DM |
804 | } |
805 | } | |
806 | ||
6acb632a WB |
807 | $cleanup->{prepared} = 1; |
808 | ||
aaeeeebe DM |
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); | |
4e0947c8 | 826 | $task->{vmstoptime} = time(); |
aaeeeebe DM |
827 | $self->run_hook_script ('pre-stop', $task, $logfd); |
828 | $plugin->stop_vm ($task, $vmid); | |
829 | $cleanup->{restart} = 1; | |
830 | } | |
60e049c2 | 831 | |
aaeeeebe DM |
832 | |
833 | } elsif ($mode eq 'suspend') { | |
834 | ||
835 | $plugin->prepare ($task, $vmid, $mode); | |
836 | ||
837 | $self->run_hook_script ('backup-start', $task, $logfd); | |
838 | ||
8187f6b0 DM |
839 | if ($vmtype eq 'lxc') { |
840 | # pre-suspend rsync | |
841 | $plugin->copy_data_phase1($task, $vmid); | |
842 | } | |
843 | ||
aaeeeebe | 844 | debugmsg ('info', "suspend vm", $logfd); |
4e0947c8 | 845 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
846 | $self->run_hook_script ('pre-stop', $task, $logfd); |
847 | $plugin->suspend_vm ($task, $vmid); | |
848 | $cleanup->{resume} = 1; | |
849 | ||
8187f6b0 DM |
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); | |
d5047243 | 858 | $self->run_hook_script('post-restart', $task, $logfd); |
4e0947c8 | 859 | $log_vm_online_again->(); |
8187f6b0 | 860 | } |
60e049c2 | 861 | |
aaeeeebe DM |
862 | } elsif ($mode eq 'snapshot') { |
863 | ||
c5be0f8c DM |
864 | $self->run_hook_script ('backup-start', $task, $logfd); |
865 | ||
aaeeeebe DM |
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); | |
4e0947c8 | 872 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
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); | |
4e0947c8 | 885 | $log_vm_online_again->(); |
aaeeeebe DM |
886 | } |
887 | ||
d5047243 FG |
888 | $self->run_hook_script ('post-restart', $task, $logfd); |
889 | ||
aaeeeebe DM |
890 | } else { |
891 | die "internal error - unknown mode '$mode'\n"; | |
892 | } | |
893 | ||
894 | # assemble archive image | |
895 | $plugin->assemble ($task, $vmid); | |
60e049c2 TM |
896 | |
897 | # produce archive | |
aaeeeebe DM |
898 | |
899 | if ($opts->{stdout}) { | |
900 | debugmsg ('info', "sending archive to stdout", $logfd); | |
d7550e09 | 901 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe DM |
902 | $self->run_hook_script ('backup-end', $task, $logfd); |
903 | return; | |
904 | } | |
905 | ||
1a87db9e | 906 | # fixme: ?? |
7ddcb3af | 907 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
908 | debugmsg ('info', "creating pbs archive on storage '$opts->{storage}'", $logfd); |
909 | } else { | |
910 | debugmsg ('info', "creating archive '$task->{tarfile}'", $logfd); | |
911 | } | |
d7550e09 | 912 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe | 913 | |
7ddcb3af | 914 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
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"; | |
aaeeeebe | 920 | |
1a87db9e DM |
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 | } | |
aaeeeebe DM |
926 | |
927 | # purge older backup | |
899b8373 | 928 | if ($maxfiles && $opts->{remove}) { |
1a87db9e | 929 | |
7ddcb3af | 930 | if ($self->{opts}->{pbs}) { |
071e573a | 931 | my $args = [$pbs_group_name, '--quiet', '1', '--keep-last', $maxfiles]; |
1a87db9e DM |
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]; | |
3e2c5105 | 944 | $logfn =~ s/\.(tgz|((tar|vma)(\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?))$/\.log/; |
1a87db9e DM |
945 | unlink $logfn; |
946 | } | |
aaeeeebe DM |
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 | ||
6acb632a | 962 | if ($cleanup->{prepared}) { |
ca2605e7 DM |
963 | # only call cleanup when necessary (when prepare was executed) |
964 | eval { $plugin->cleanup ($task, $vmid) }; | |
965 | warn $@ if $@; | |
966 | } | |
aaeeeebe DM |
967 | |
968 | eval { $plugin->set_logfd (undef); }; | |
969 | warn $@ if $@; | |
970 | ||
60e049c2 TM |
971 | if ($cleanup->{resume} || $cleanup->{restart}) { |
972 | eval { | |
aaeeeebe DM |
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 { | |
757fd3d5 DM |
978 | my $running = $plugin->vm_status($vmid); |
979 | if (!$running) { | |
980 | debugmsg ('info', "restarting vm", $logfd); | |
981 | $plugin->start_vm ($task, $vmid); | |
982 | } | |
d5047243 FG |
983 | } |
984 | $self->run_hook_script ('post-restart', $task, $logfd); | |
aaeeeebe DM |
985 | }; |
986 | my $err = $@; | |
987 | if ($err) { | |
988 | warn $err; | |
989 | } else { | |
4e0947c8 | 990 | $log_vm_online_again->(); |
aaeeeebe DM |
991 | } |
992 | } | |
993 | } | |
994 | ||
995 | eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; }; | |
996 | warn $@ if $@; | |
997 | ||
fe6643b6 | 998 | eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; }; |
aaeeeebe DM |
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); | |
29f26f6d | 1007 | debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
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); | |
29f26f6d | 1015 | debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1016 | } |
1017 | ||
1018 | close ($logfd) if $logfd; | |
60e049c2 | 1019 | |
1a87db9e | 1020 | if ($task->{tmplog}) { |
7ddcb3af | 1021 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
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 | } | |
aaeeeebe DM |
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 { | |
d7550e09 | 1038 | my ($self, $rpcenv, $authuser) = @_; |
aaeeeebe DM |
1039 | |
1040 | my $opts = $self->{opts}; | |
1041 | ||
1042 | debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1); | |
a7e42354 DM |
1043 | debugmsg ('info', "skip external VMs: " . join(', ', @{$self->{skiplist}})) |
1044 | if scalar(@{$self->{skiplist}}); | |
60e049c2 | 1045 | |
aaeeeebe | 1046 | my $tasklist = []; |
df5875b4 AL |
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 | } | |
aaeeeebe | 1053 | |
df5875b4 AL |
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, { | |
f839f3d1 | 1060 | mode => $opts->{mode}, |
df5875b4 | 1061 | plugin => $plugin, |
f839f3d1 TL |
1062 | state => 'todo', |
1063 | vmid => $vmid, | |
1064 | }; | |
aaeeeebe DM |
1065 | } |
1066 | ||
44b21574 WB |
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 | ||
aaeeeebe DM |
1073 | my $starttime = time(); |
1074 | my $errcount = 0; | |
1075 | eval { | |
1076 | ||
44b21574 | 1077 | $self->run_hook_script ('job-start', undef, $job_start_fd); |
aaeeeebe DM |
1078 | |
1079 | foreach my $task (@$tasklist) { | |
1080 | $self->exec_backup_task ($task); | |
1081 | $errcount += 1 if $task->{state} ne 'ok'; | |
1082 | } | |
1083 | ||
44b21574 | 1084 | $self->run_hook_script ('job-end', undef, $job_end_fd); |
aaeeeebe DM |
1085 | }; |
1086 | my $err = $@; | |
1087 | ||
44b21574 | 1088 | $self->run_hook_script ('job-abort', undef, $job_end_fd) if $err; |
aaeeeebe DM |
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 { | |
61ca4432 | 1096 | debugmsg ('info', "Backup job finished successfully", undef, 1); |
aaeeeebe DM |
1097 | } |
1098 | } | |
1099 | ||
44b21574 WB |
1100 | close $job_start_fd; |
1101 | close $job_end_fd; | |
1102 | ||
aaeeeebe DM |
1103 | my $totaltime = time() - $starttime; |
1104 | ||
44b21574 | 1105 | eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); }; |
aaeeeebe | 1106 | debugmsg ('err', $@) if $@; |
4a4051d8 DM |
1107 | |
1108 | die $err if $err; | |
1109 | ||
60e049c2 | 1110 | die "job errors\n" if $errcount; |
8682f6fc WL |
1111 | |
1112 | unlink $pidfile; | |
aaeeeebe DM |
1113 | } |
1114 | ||
ac27b58d | 1115 | |
47664cbe DM |
1116 | sub option_exists { |
1117 | my $key = shift; | |
1118 | return defined($confdesc->{$key}); | |
1119 | } | |
1120 | ||
31aef761 DM |
1121 | sub verify_vzdump_parameters { |
1122 | my ($param, $check_missing) = @_; | |
1123 | ||
1124 | raise_param_exc({ all => "option conflicts with option 'vmid'"}) | |
eab837c4 | 1125 | if $param->{all} && $param->{vmid}; |
31aef761 DM |
1126 | |
1127 | raise_param_exc({ exclude => "option conflicts with option 'vmid'"}) | |
1128 | if $param->{exclude} && $param->{vmid}; | |
1129 | ||
f3376261 TM |
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}); | |
31aef761 | 1134 | |
e2a2525e FG |
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 | ||
31aef761 DM |
1139 | return if !$check_missing; |
1140 | ||
1141 | raise_param_exc({ vmid => "property is missing"}) | |
f3376261 | 1142 | if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid}; |
8682f6fc WL |
1143 | |
1144 | } | |
1145 | ||
eab837c4 | 1146 | sub stop_running_backups { |
8682f6fc WL |
1147 | my($self) = @_; |
1148 | ||
eab837c4 DM |
1149 | my $upid = PVE::Tools::file_read_firstline($pidfile); |
1150 | return if !$upid; | |
8682f6fc | 1151 | |
eab837c4 | 1152 | my $task = PVE::Tools::upid_decode($upid); |
8682f6fc | 1153 | |
60e049c2 | 1154 | if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) && |
eab837c4 DM |
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 | } | |
76189130 | 1163 | die "stopping backup process $task->{pid} failed\n" if $i == 0; |
8682f6fc | 1164 | } |
31aef761 DM |
1165 | } |
1166 | ||
5c4da4c3 AL |
1167 | sub get_included_guests { |
1168 | my ($job) = @_; | |
1169 | ||
1170 | my $nodename = PVE::INotify::nodename(); | |
1171 | my $vmids = []; | |
df5875b4 AL |
1172 | my $vmids_per_node = {}; |
1173 | ||
1174 | my $vmlist = PVE::Cluster::get_vmlist(); | |
5c4da4c3 | 1175 | |
5c4da4c3 AL |
1176 | if ($job->{pool}) { |
1177 | $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool}); | |
df5875b4 AL |
1178 | } elsif ($job->{vmid}) { |
1179 | $vmids = [ PVE::Tools::split_list($job->{vmid}) ]; | |
5c4da4c3 | 1180 | } else { |
df5875b4 AL |
1181 | # all or exclude |
1182 | my @exclude = PVE::Tools::split_list($job->{exclude}); | |
1183 | @exclude = @{PVE::VZDump::check_vmids(@exclude)}; | |
1184 | my $excludehash = { map { $_ => 1 } @exclude }; | |
1185 | ||
1186 | for my $id (keys %{ $vmlist->{ids} }) { | |
1187 | next if $excludehash->{$id}; | |
1188 | push @$vmids, $id; | |
1189 | } | |
5c4da4c3 | 1190 | } |
df5875b4 | 1191 | $vmids = [ sort {$a <=> $b} @$vmids]; |
5c4da4c3 | 1192 | |
df5875b4 AL |
1193 | $vmids = PVE::VZDump::check_vmids(@$vmids); |
1194 | ||
1195 | foreach my $vmid (@$vmids) { | |
1196 | my $vmid_data = $vmlist->{ids}->{$vmid}; | |
1197 | my $node = $vmid_data->{node}; | |
5c4da4c3 | 1198 | |
df5875b4 AL |
1199 | next if (defined $job->{node} && $job->{node} ne $node); |
1200 | push @{$vmids_per_node->{$node}}, $vmid; | |
5c4da4c3 AL |
1201 | } |
1202 | ||
df5875b4 | 1203 | return $vmids_per_node; |
5c4da4c3 AL |
1204 | } |
1205 | ||
aaeeeebe | 1206 | 1; |