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