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