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