]>
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 | |
dc6e0a52 | 525 | die "missing UPID" if !$upid; # should not happen |
eab837c4 | 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 | } | |
5d0d14f1 SI |
593 | # FIXME: for backwards compatibility - drop with PVE 7.0 |
594 | $ENV{TARFILE} = $task->{target} if $task->{target}; | |
aaeeeebe DM |
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; | |
de2d177e FE |
636 | |
637 | my $archive_info = eval { PVE::Storage::archive_info($fn) } // {}; | |
638 | if ($archive_info->{is_std_name}) { | |
639 | my $filename = $archive_info->{filename}; | |
640 | my $backup = { | |
641 | 'path' => "$dir/$filename", | |
642 | 'ctime' => $archive_info->{ctime}, | |
643 | }; | |
644 | push @{$bklist}, $backup; | |
899b8373 DM |
645 | } |
646 | } | |
647 | ||
648 | return $bklist; | |
649 | } | |
60e049c2 | 650 | |
aaeeeebe DM |
651 | sub exec_backup_task { |
652 | my ($self, $task) = @_; | |
60e049c2 | 653 | |
aaeeeebe DM |
654 | my $opts = $self->{opts}; |
655 | ||
656 | my $vmid = $task->{vmid}; | |
657 | my $plugin = $task->{plugin}; | |
1a87db9e DM |
658 | my $vmtype = $plugin->type(); |
659 | ||
660 | $task->{backup_time} = time(); | |
661 | ||
662 | my $pbs_group_name; | |
663 | my $pbs_snapshot_name; | |
664 | ||
7ddcb3af | 665 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
666 | if ($vmtype eq 'lxc') { |
667 | $pbs_group_name = "ct/$vmid"; | |
668 | } elsif ($vmtype eq 'qemu') { | |
669 | $pbs_group_name = "vm/$vmid"; | |
670 | } else { | |
671 | die "pbs backup not implemented for plugin type '$vmtype'\n"; | |
672 | } | |
673 | my $btime = strftime("%FT%TZ", gmtime($task->{backup_time})); | |
674 | $pbs_snapshot_name = "$pbs_group_name/$btime"; | |
675 | } | |
aaeeeebe DM |
676 | |
677 | my $vmstarttime = time (); | |
60e049c2 | 678 | |
aaeeeebe DM |
679 | my $logfd; |
680 | ||
681 | my $cleanup = {}; | |
682 | ||
4e0947c8 TL |
683 | my $log_vm_online_again = sub { |
684 | return if !defined($task->{vmstoptime}); | |
685 | $task->{vmconttime} //= time(); | |
686 | my $delay = $task->{vmconttime} - $task->{vmstoptime}; | |
310cde8b | 687 | $delay = '<1' if $delay < 1; |
4e0947c8 TL |
688 | debugmsg ('info', "guest is online again after $delay seconds", $logfd); |
689 | }; | |
aaeeeebe DM |
690 | |
691 | eval { | |
692 | die "unable to find VM '$vmid'\n" if !$plugin; | |
693 | ||
2de3ee58 | 694 | # for now we deny backups of a running ha managed service in *stop* mode |
83ec8f81 | 695 | # as it interferes with the HA stack (started services should not stop). |
2de3ee58 | 696 | if ($opts->{mode} eq 'stop' && |
83ec8f81 | 697 | PVE::HA::Config::vm_is_ha_managed($vmid, 'started')) |
2de3ee58 TL |
698 | { |
699 | die "Cannot execute a backup with stop mode on a HA managed and". | |
700 | " enabled Service. Use snapshot mode or disable the Service.\n"; | |
701 | } | |
702 | ||
aaeeeebe DM |
703 | my $tmplog = "$logdir/$vmtype-$vmid.log"; |
704 | ||
aaeeeebe | 705 | my $bkname = "vzdump-$vmtype-$vmid"; |
1a87db9e | 706 | my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time})); |
aaeeeebe | 707 | |
899b8373 DM |
708 | my $maxfiles = $opts->{maxfiles}; |
709 | ||
710 | if ($maxfiles && !$opts->{remove}) { | |
1a87db9e | 711 | my $count; |
7ddcb3af | 712 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
713 | my $res = PVE::Storage::PBSPlugin::run_client_cmd($opts->{scfg}, $opts->{storage}, 'snapshots', $pbs_group_name); |
714 | $count = scalar(@$res); | |
715 | } else { | |
716 | my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname); | |
717 | $count = scalar(@$bklist); | |
718 | } | |
6c09101a | 719 | die "There is a max backup limit of ($maxfiles) enforced by the". |
1a87db9e DM |
720 | " target storage or the vzdump parameters.". |
721 | " Either increase the limit or delete old backup(s).\n" | |
722 | if $count >= $maxfiles; | |
899b8373 DM |
723 | } |
724 | ||
7ddcb3af | 725 | if (!$self->{opts}->{pbs}) { |
1a87db9e DM |
726 | $task->{logfile} = "$opts->{dumpdir}/$basename.log"; |
727 | } | |
aaeeeebe | 728 | |
757fd3d5 | 729 | my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar'; |
778d5b6d | 730 | my ($comp, $comp_ext) = compressor_info($opts); |
d7550e09 DM |
731 | if ($comp && $comp_ext) { |
732 | $ext .= ".${comp_ext}"; | |
733 | } | |
aaeeeebe | 734 | |
7ddcb3af | 735 | if ($self->{opts}->{pbs}) { |
1a87db9e | 736 | die "unable to pipe backup to stdout\n" if $opts->{stdout}; |
b3c3304f | 737 | $task->{target} = $pbs_snapshot_name; |
aaeeeebe | 738 | } else { |
1a87db9e | 739 | if ($opts->{stdout}) { |
6cba1855 | 740 | $task->{target} = '-'; |
1a87db9e | 741 | } else { |
6cba1855 | 742 | $task->{target} = $task->{tmptar} = "$opts->{dumpdir}/$basename$ext"; |
1a87db9e DM |
743 | $task->{tmptar} =~ s/\.[^\.]+$/\.dat/; |
744 | unlink $task->{tmptar}; | |
745 | } | |
aaeeeebe DM |
746 | } |
747 | ||
748 | $task->{vmtype} = $vmtype; | |
749 | ||
01fc3672 | 750 | if ($opts->{tmpdir}) { |
60e049c2 | 751 | $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp$$"; |
01fc3672 FG |
752 | } elsif ($self->{opts}->{pbs}) { |
753 | $task->{tmpdir} = "/var/tmp/vzdumptmp$$"; #fixme | |
aaeeeebe DM |
754 | } else { |
755 | # dumpdir is posix? then use it as temporary dir | |
5dc86eb8 | 756 | my $info = get_mount_info($opts->{dumpdir}); |
60e049c2 | 757 | if ($vmtype eq 'qemu' || |
aaeeeebe DM |
758 | grep ($_ eq $info->{fstype}, @posix_filesystems)) { |
759 | $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp"; | |
760 | } else { | |
761 | $task->{tmpdir} = "/var/tmp/vzdumptmp$$"; | |
762 | debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" . | |
763 | "using $task->{tmpdir} for temporary files", $logfd); | |
764 | } | |
765 | } | |
766 | ||
767 | rmtree $task->{tmpdir}; | |
768 | mkdir $task->{tmpdir}; | |
769 | -d $task->{tmpdir} || | |
770 | die "unable to create temporary directory '$task->{tmpdir}'"; | |
771 | ||
772 | $logfd = IO::File->new (">$tmplog") || | |
773 | die "unable to create log file '$tmplog'"; | |
774 | ||
775 | $task->{dumpdir} = $opts->{dumpdir}; | |
ff00abe6 | 776 | $task->{storeid} = $opts->{storage}; |
1a87db9e | 777 | $task->{scfg} = $opts->{scfg}; |
aaeeeebe DM |
778 | $task->{tmplog} = $tmplog; |
779 | ||
1a87db9e | 780 | unlink $task->{logfile} if defined($task->{logfile}); |
aaeeeebe | 781 | |
29f26f6d DJ |
782 | debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1); |
783 | debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime())); | |
aaeeeebe DM |
784 | |
785 | $plugin->set_logfd ($logfd); | |
786 | ||
787 | # test is VM is running | |
788 | my ($running, $status_text) = $plugin->vm_status ($vmid); | |
789 | ||
790 | debugmsg ('info', "status = ${status_text}", $logfd); | |
791 | ||
792 | # lock VM (prevent config changes) | |
793 | $plugin->lock_vm ($vmid); | |
794 | ||
795 | $cleanup->{unlock} = 1; | |
796 | ||
797 | # prepare | |
798 | ||
6acb632a | 799 | my $mode = $running ? $task->{mode} : 'stop'; |
aaeeeebe DM |
800 | |
801 | if ($mode eq 'snapshot') { | |
802 | my %saved_task = %$task; | |
803 | eval { $plugin->prepare ($task, $vmid, $mode); }; | |
804 | if (my $err = $@) { | |
805 | die $err if $err !~ m/^mode failure/; | |
806 | debugmsg ('info', $err, $logfd); | |
807 | debugmsg ('info', "trying 'suspend' mode instead", $logfd); | |
808 | $mode = 'suspend'; # so prepare is called again below | |
60e049c2 | 809 | %$task = %saved_task; |
aaeeeebe DM |
810 | } |
811 | } | |
812 | ||
6acb632a WB |
813 | $cleanup->{prepared} = 1; |
814 | ||
aaeeeebe DM |
815 | $task->{mode} = $mode; |
816 | ||
817 | debugmsg ('info', "backup mode: $mode", $logfd); | |
818 | ||
819 | debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd) | |
820 | if $opts->{bwlimit}; | |
821 | ||
822 | debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd); | |
823 | ||
824 | if ($mode eq 'stop') { | |
825 | ||
826 | $plugin->prepare ($task, $vmid, $mode); | |
827 | ||
828 | $self->run_hook_script ('backup-start', $task, $logfd); | |
829 | ||
830 | if ($running) { | |
831 | debugmsg ('info', "stopping vm", $logfd); | |
4e0947c8 | 832 | $task->{vmstoptime} = time(); |
aaeeeebe DM |
833 | $self->run_hook_script ('pre-stop', $task, $logfd); |
834 | $plugin->stop_vm ($task, $vmid); | |
835 | $cleanup->{restart} = 1; | |
836 | } | |
60e049c2 | 837 | |
aaeeeebe DM |
838 | |
839 | } elsif ($mode eq 'suspend') { | |
840 | ||
841 | $plugin->prepare ($task, $vmid, $mode); | |
842 | ||
843 | $self->run_hook_script ('backup-start', $task, $logfd); | |
844 | ||
8187f6b0 DM |
845 | if ($vmtype eq 'lxc') { |
846 | # pre-suspend rsync | |
847 | $plugin->copy_data_phase1($task, $vmid); | |
848 | } | |
849 | ||
ef7d3fdd | 850 | debugmsg ('info', "suspending guest", $logfd); |
4e0947c8 | 851 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
852 | $self->run_hook_script ('pre-stop', $task, $logfd); |
853 | $plugin->suspend_vm ($task, $vmid); | |
854 | $cleanup->{resume} = 1; | |
855 | ||
8187f6b0 DM |
856 | if ($vmtype eq 'lxc') { |
857 | # post-suspend rsync | |
858 | $plugin->copy_data_phase2($task, $vmid); | |
859 | ||
ef7d3fdd | 860 | debugmsg ('info', "resuming guest", $logfd); |
8187f6b0 DM |
861 | $cleanup->{resume} = 0; |
862 | $self->run_hook_script('pre-restart', $task, $logfd); | |
863 | $plugin->resume_vm($task, $vmid); | |
d5047243 | 864 | $self->run_hook_script('post-restart', $task, $logfd); |
4e0947c8 | 865 | $log_vm_online_again->(); |
8187f6b0 | 866 | } |
60e049c2 | 867 | |
aaeeeebe DM |
868 | } elsif ($mode eq 'snapshot') { |
869 | ||
c5be0f8c DM |
870 | $self->run_hook_script ('backup-start', $task, $logfd); |
871 | ||
aaeeeebe DM |
872 | my $snapshot_count = $task->{snapshot_count} || 0; |
873 | ||
874 | $self->run_hook_script ('pre-stop', $task, $logfd); | |
875 | ||
876 | if ($snapshot_count > 1) { | |
877 | debugmsg ('info', "suspend vm to make snapshot", $logfd); | |
4e0947c8 | 878 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
879 | $plugin->suspend_vm ($task, $vmid); |
880 | $cleanup->{resume} = 1; | |
881 | } | |
882 | ||
883 | $plugin->snapshot ($task, $vmid); | |
884 | ||
885 | $self->run_hook_script ('pre-restart', $task, $logfd); | |
886 | ||
887 | if ($snapshot_count > 1) { | |
888 | debugmsg ('info', "resume vm", $logfd); | |
889 | $cleanup->{resume} = 0; | |
890 | $plugin->resume_vm ($task, $vmid); | |
4e0947c8 | 891 | $log_vm_online_again->(); |
aaeeeebe DM |
892 | } |
893 | ||
d5047243 FG |
894 | $self->run_hook_script ('post-restart', $task, $logfd); |
895 | ||
aaeeeebe DM |
896 | } else { |
897 | die "internal error - unknown mode '$mode'\n"; | |
898 | } | |
899 | ||
900 | # assemble archive image | |
901 | $plugin->assemble ($task, $vmid); | |
60e049c2 TM |
902 | |
903 | # produce archive | |
aaeeeebe DM |
904 | |
905 | if ($opts->{stdout}) { | |
906 | debugmsg ('info', "sending archive to stdout", $logfd); | |
d7550e09 | 907 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe DM |
908 | $self->run_hook_script ('backup-end', $task, $logfd); |
909 | return; | |
910 | } | |
911 | ||
b3c3304f TL |
912 | my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump'; |
913 | debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd); | |
d7550e09 | 914 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe | 915 | |
7ddcb3af | 916 | if ($self->{opts}->{pbs}) { |
e21a31a7 | 917 | # size is added to task struct in guest vzdump plugins |
1a87db9e | 918 | } else { |
6cba1855 TL |
919 | rename ($task->{tmptar}, $task->{target}) || |
920 | die "unable to rename '$task->{tmptar}' to '$task->{target}'\n"; | |
aaeeeebe | 921 | |
1a87db9e | 922 | # determine size |
6cba1855 | 923 | $task->{size} = (-s $task->{target}) || 0; |
1a87db9e DM |
924 | my $cs = format_size ($task->{size}); |
925 | debugmsg ('info', "archive file size: $cs", $logfd); | |
926 | } | |
aaeeeebe DM |
927 | |
928 | # purge older backup | |
899b8373 | 929 | if ($maxfiles && $opts->{remove}) { |
1a87db9e | 930 | |
7ddcb3af | 931 | if ($self->{opts}->{pbs}) { |
071e573a | 932 | my $args = [$pbs_group_name, '--quiet', '1', '--keep-last', $maxfiles]; |
1a87db9e DM |
933 | my $logfunc = sub { my $line = shift; debugmsg ('info', $line, $logfd); }; |
934 | PVE::Storage::PBSPlugin::run_raw_client_cmd( | |
935 | $opts->{scfg}, $opts->{storage}, 'prune', $args, logfunc => $logfunc); | |
936 | } else { | |
6cba1855 | 937 | my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{target}); |
de2d177e | 938 | $bklist = [ sort { $b->{ctime} <=> $a->{ctime} } @$bklist ]; |
1a87db9e DM |
939 | |
940 | while (scalar (@$bklist) >= $maxfiles) { | |
941 | my $d = pop @$bklist; | |
de2d177e FE |
942 | my $archive_path = $d->{path}; |
943 | debugmsg ('info', "delete old backup '$archive_path'", $logfd); | |
944 | PVE::Storage::archive_remove($archive_path); | |
1a87db9e | 945 | } |
aaeeeebe DM |
946 | } |
947 | } | |
948 | ||
949 | $self->run_hook_script ('backup-end', $task, $logfd); | |
950 | }; | |
951 | my $err = $@; | |
952 | ||
953 | if ($plugin) { | |
954 | # clean-up | |
955 | ||
956 | if ($cleanup->{unlock}) { | |
957 | eval { $plugin->unlock_vm ($vmid); }; | |
958 | warn $@ if $@; | |
959 | } | |
960 | ||
6acb632a | 961 | if ($cleanup->{prepared}) { |
ca2605e7 DM |
962 | # only call cleanup when necessary (when prepare was executed) |
963 | eval { $plugin->cleanup ($task, $vmid) }; | |
964 | warn $@ if $@; | |
965 | } | |
aaeeeebe DM |
966 | |
967 | eval { $plugin->set_logfd (undef); }; | |
968 | warn $@ if $@; | |
969 | ||
60e049c2 TM |
970 | if ($cleanup->{resume} || $cleanup->{restart}) { |
971 | eval { | |
aaeeeebe DM |
972 | $self->run_hook_script ('pre-restart', $task, $logfd); |
973 | if ($cleanup->{resume}) { | |
974 | debugmsg ('info', "resume vm", $logfd); | |
975 | $plugin->resume_vm ($task, $vmid); | |
976 | } else { | |
757fd3d5 DM |
977 | my $running = $plugin->vm_status($vmid); |
978 | if (!$running) { | |
979 | debugmsg ('info', "restarting vm", $logfd); | |
980 | $plugin->start_vm ($task, $vmid); | |
981 | } | |
d5047243 FG |
982 | } |
983 | $self->run_hook_script ('post-restart', $task, $logfd); | |
aaeeeebe DM |
984 | }; |
985 | my $err = $@; | |
986 | if ($err) { | |
987 | warn $err; | |
988 | } else { | |
4e0947c8 | 989 | $log_vm_online_again->(); |
aaeeeebe DM |
990 | } |
991 | } | |
992 | } | |
993 | ||
994 | eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; }; | |
995 | warn $@ if $@; | |
996 | ||
fe6643b6 | 997 | eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; }; |
aaeeeebe DM |
998 | warn $@ if $@; |
999 | ||
1000 | my $delay = $task->{backuptime} = time () - $vmstarttime; | |
1001 | ||
1002 | if ($err) { | |
1003 | $task->{state} = 'err'; | |
1004 | $task->{msg} = $err; | |
1005 | debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1); | |
29f26f6d | 1006 | debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1007 | |
1008 | eval { $self->run_hook_script ('backup-abort', $task, $logfd); }; | |
1009 | ||
1010 | } else { | |
1011 | $task->{state} = 'ok'; | |
1012 | my $tstr = format_time ($delay); | |
1013 | debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1); | |
29f26f6d | 1014 | debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1015 | } |
1016 | ||
1017 | close ($logfd) if $logfd; | |
60e049c2 | 1018 | |
1a87db9e | 1019 | if ($task->{tmplog}) { |
7ddcb3af | 1020 | if ($self->{opts}->{pbs}) { |
1a87db9e DM |
1021 | if ($task->{state} eq 'ok') { |
1022 | my $param = [$pbs_snapshot_name, $task->{tmplog}]; | |
1023 | PVE::Storage::PBSPlugin::run_raw_client_cmd( | |
1024 | $opts->{scfg}, $opts->{storage}, 'upload-log', $param, errmsg => "upload log failed"); | |
1025 | } | |
1026 | } elsif ($task->{logfile}) { | |
1027 | system {'cp'} 'cp', $task->{tmplog}, $task->{logfile}; | |
1028 | } | |
aaeeeebe DM |
1029 | } |
1030 | ||
1031 | eval { $self->run_hook_script ('log-end', $task); }; | |
1032 | ||
1033 | die $err if $err && $err =~ m/^interrupted by signal$/; | |
1034 | } | |
1035 | ||
1036 | sub exec_backup { | |
d7550e09 | 1037 | my ($self, $rpcenv, $authuser) = @_; |
aaeeeebe DM |
1038 | |
1039 | my $opts = $self->{opts}; | |
1040 | ||
1041 | debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1); | |
a7e42354 DM |
1042 | debugmsg ('info', "skip external VMs: " . join(', ', @{$self->{skiplist}})) |
1043 | if scalar(@{$self->{skiplist}}); | |
60e049c2 | 1044 | |
aaeeeebe | 1045 | my $tasklist = []; |
df5875b4 AL |
1046 | my $vzdump_plugins = {}; |
1047 | foreach my $plugin (@{$self->{plugins}}) { | |
1048 | my $type = $plugin->type(); | |
1049 | next if exists $vzdump_plugins->{$type}; | |
1050 | $vzdump_plugins->{$type} = $plugin; | |
1051 | } | |
aaeeeebe | 1052 | |
df5875b4 | 1053 | my $vmlist = PVE::Cluster::get_vmlist(); |
eb002150 | 1054 | foreach my $vmid (@{$opts->{vmids}}) { |
df5875b4 AL |
1055 | my $guest_type = $vmlist->{ids}->{$vmid}->{type}; |
1056 | my $plugin = $vzdump_plugins->{$guest_type}; | |
1057 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all}); | |
1058 | push @$tasklist, { | |
f839f3d1 | 1059 | mode => $opts->{mode}, |
df5875b4 | 1060 | plugin => $plugin, |
f839f3d1 TL |
1061 | state => 'todo', |
1062 | vmid => $vmid, | |
1063 | }; | |
aaeeeebe DM |
1064 | } |
1065 | ||
44b21574 WB |
1066 | # Use in-memory files for the outer hook logs to pass them to sendmail. |
1067 | my $job_start_log = ''; | |
1068 | my $job_end_log = ''; | |
1069 | open my $job_start_fd, '>', \$job_start_log; | |
1070 | open my $job_end_fd, '>', \$job_end_log; | |
1071 | ||
aaeeeebe DM |
1072 | my $starttime = time(); |
1073 | my $errcount = 0; | |
1074 | eval { | |
1075 | ||
44b21574 | 1076 | $self->run_hook_script ('job-start', undef, $job_start_fd); |
aaeeeebe DM |
1077 | |
1078 | foreach my $task (@$tasklist) { | |
1079 | $self->exec_backup_task ($task); | |
1080 | $errcount += 1 if $task->{state} ne 'ok'; | |
1081 | } | |
1082 | ||
44b21574 | 1083 | $self->run_hook_script ('job-end', undef, $job_end_fd); |
aaeeeebe DM |
1084 | }; |
1085 | my $err = $@; | |
1086 | ||
44b21574 | 1087 | $self->run_hook_script ('job-abort', undef, $job_end_fd) if $err; |
aaeeeebe DM |
1088 | |
1089 | if ($err) { | |
1090 | debugmsg ('err', "Backup job failed - $err", undef, 1); | |
1091 | } else { | |
1092 | if ($errcount) { | |
1093 | debugmsg ('info', "Backup job finished with errors", undef, 1); | |
1094 | } else { | |
61ca4432 | 1095 | debugmsg ('info', "Backup job finished successfully", undef, 1); |
aaeeeebe DM |
1096 | } |
1097 | } | |
1098 | ||
44b21574 WB |
1099 | close $job_start_fd; |
1100 | close $job_end_fd; | |
1101 | ||
aaeeeebe DM |
1102 | my $totaltime = time() - $starttime; |
1103 | ||
44b21574 | 1104 | eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); }; |
aaeeeebe | 1105 | debugmsg ('err', $@) if $@; |
4a4051d8 DM |
1106 | |
1107 | die $err if $err; | |
1108 | ||
60e049c2 | 1109 | die "job errors\n" if $errcount; |
8682f6fc WL |
1110 | |
1111 | unlink $pidfile; | |
aaeeeebe DM |
1112 | } |
1113 | ||
ac27b58d | 1114 | |
47664cbe DM |
1115 | sub option_exists { |
1116 | my $key = shift; | |
1117 | return defined($confdesc->{$key}); | |
1118 | } | |
1119 | ||
31aef761 DM |
1120 | sub verify_vzdump_parameters { |
1121 | my ($param, $check_missing) = @_; | |
1122 | ||
1123 | raise_param_exc({ all => "option conflicts with option 'vmid'"}) | |
eab837c4 | 1124 | if $param->{all} && $param->{vmid}; |
31aef761 DM |
1125 | |
1126 | raise_param_exc({ exclude => "option conflicts with option 'vmid'"}) | |
1127 | if $param->{exclude} && $param->{vmid}; | |
1128 | ||
f3376261 TM |
1129 | raise_param_exc({ pool => "option conflicts with option 'vmid'"}) |
1130 | if $param->{pool} && $param->{vmid}; | |
1131 | ||
1132 | $param->{all} = 1 if (defined($param->{exclude}) && !$param->{pool}); | |
31aef761 | 1133 | |
e2a2525e FG |
1134 | warn "option 'size' is deprecated and will be removed in a future " . |
1135 | "release, please update your script/configuration!\n" | |
1136 | if defined($param->{size}); | |
1137 | ||
31aef761 DM |
1138 | return if !$check_missing; |
1139 | ||
1140 | raise_param_exc({ vmid => "property is missing"}) | |
f3376261 | 1141 | if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid}; |
8682f6fc WL |
1142 | |
1143 | } | |
1144 | ||
eab837c4 | 1145 | sub stop_running_backups { |
8682f6fc WL |
1146 | my($self) = @_; |
1147 | ||
eab837c4 DM |
1148 | my $upid = PVE::Tools::file_read_firstline($pidfile); |
1149 | return if !$upid; | |
8682f6fc | 1150 | |
eab837c4 | 1151 | my $task = PVE::Tools::upid_decode($upid); |
8682f6fc | 1152 | |
60e049c2 | 1153 | if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) && |
eab837c4 DM |
1154 | PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) { |
1155 | kill(15, $task->{pid}); | |
1156 | # wait max 15 seconds to shut down (else, do nothing for now) | |
1157 | my $i; | |
1158 | for ($i = 15; $i > 0; $i--) { | |
1159 | last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart})); | |
1160 | sleep (1); | |
1161 | } | |
76189130 | 1162 | die "stopping backup process $task->{pid} failed\n" if $i == 0; |
8682f6fc | 1163 | } |
31aef761 DM |
1164 | } |
1165 | ||
5c4da4c3 AL |
1166 | sub get_included_guests { |
1167 | my ($job) = @_; | |
1168 | ||
1169 | my $nodename = PVE::INotify::nodename(); | |
1170 | my $vmids = []; | |
df5875b4 AL |
1171 | my $vmids_per_node = {}; |
1172 | ||
1173 | my $vmlist = PVE::Cluster::get_vmlist(); | |
5c4da4c3 | 1174 | |
5c4da4c3 AL |
1175 | if ($job->{pool}) { |
1176 | $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool}); | |
df5875b4 | 1177 | } elsif ($job->{vmid}) { |
05447e04 | 1178 | $vmids = [ split_list($job->{vmid}) ]; |
da7cb515 | 1179 | } elsif ($job->{all}) { |
df5875b4 | 1180 | # all or exclude |
05447e04 TL |
1181 | my $exclude = check_vmids(split_list($job->{exclude})); |
1182 | my $excludehash = { map { $_ => 1 } @$exclude }; | |
df5875b4 | 1183 | |
05447e04 | 1184 | for my $id (keys %{$vmlist->{ids}}) { |
df5875b4 AL |
1185 | next if $excludehash->{$id}; |
1186 | push @$vmids, $id; | |
1187 | } | |
da7cb515 AL |
1188 | } else { |
1189 | return $vmids_per_node; | |
5c4da4c3 | 1190 | } |
50ba40ec | 1191 | $vmids = check_vmids(@$vmids); |
5c4da4c3 | 1192 | |
50ba40ec TL |
1193 | for my $vmid (@$vmids) { |
1194 | my $node = $vmlist->{ids}->{$vmid}->{node}; | |
df5875b4 | 1195 | next if (defined $job->{node} && $job->{node} ne $node); |
50ba40ec | 1196 | |
df5875b4 | 1197 | push @{$vmids_per_node->{$node}}, $vmid; |
5c4da4c3 AL |
1198 | } |
1199 | ||
df5875b4 | 1200 | return $vmids_per_node; |
5c4da4c3 AL |
1201 | } |
1202 | ||
aaeeeebe | 1203 | 1; |