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