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