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