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