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