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