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