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