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