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