]>
git.proxmox.com Git - pve-guest-common.git/blob - src/PVE/VZDump/Common.pm
1 package PVE
::VZDump
::Common
;
8 use PVE
::SafeSyslog
qw(syslog);
10 use PVE
::Cluster
qw(cfs_register_file);
11 use PVE
::JSONSchema
qw(get_standard_option);
13 # NOTE: this is the legacy config, nowadays jobs.cfg is used (handled in pve-manager)
16 \
&parse_vzdump_cron_config
,
17 \
&write_vzdump_cron_config
,
20 my $dowhash_to_dow = sub {
24 push @da, $num ?
1 : 'mon' if $d->{mon
};
25 push @da, $num ?
2 : 'tue' if $d->{tue
};
26 push @da, $num ?
3 : 'wed' if $d->{wed
};
27 push @da, $num ?
4 : 'thu' if $d->{thu
};
28 push @da, $num ?
5 : 'fri' if $d->{fri
};
29 push @da, $num ?
6 : 'sat' if $d->{sat
};
30 push @da, $num ?
7 : 'sun' if $d->{sun
};
35 our $PROPERTY_STRINGS = {
36 'performance' => 'backup-performance',
37 'prune-backups' => 'prune-backups',
40 my sub parse_property_strings
{
43 for my $opt (keys $PROPERTY_STRINGS->%*) {
44 next if !defined($opts->{$opt});
46 my $format = $PROPERTY_STRINGS->{$opt};
47 $opts->{$opt} = PVE
::JSONSchema
::parse_property_string
($format, $opts->{$opt});
51 # parse crontab style day of week
53 my ($dowstr, $noerr) = @_;
55 my $dowmap = {mon
=> 1, tue
=> 2, wed
=> 3, thu
=> 4,
56 fri
=> 5, sat
=> 6, sun
=> 7};
57 my $rdowmap = { '1' => 'mon', '2' => 'tue', '3' => 'wed', '4' => 'thu',
58 '5' => 'fri', '6' => 'sat', '7' => 'sun', '0' => 'sun'};
62 $dowstr = '1,2,3,4,5,6,7' if $dowstr eq '*';
64 foreach my $day (PVE
::Tools
::split_list
($dowstr)) {
65 if ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun)-(mon|tue|wed|thu|fri|sat|sun)$/i) {
66 for (my $i = $dowmap->{lc($1)}; $i <= $dowmap->{lc($2)}; $i++) {
67 my $r = $rdowmap->{$i};
70 } elsif ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun|[0-7])$/i) {
71 $day = $rdowmap->{$day} if $day =~ m/\d/;
74 return undef if $noerr;
75 die "unable to parse day of week '$dowstr'\n";
82 PVE
::JSONSchema
::register_format
('backup-performance', {
84 description
=> "Applies to VMs. Allow up to this many IO workers at the same time.",
95 type
=> 'string', format
=> 'pve-vmid-list',
96 description
=> "The ID of the guest system you want to backup.",
97 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
100 node
=> get_standard_option
('pve-node', {
101 description
=> "Only run if executed on this node.",
102 completion
=> \
&PVE
::Cluster
::get_nodelist
,
107 description
=> "Backup all known guest systems on this host.",
113 description
=> "Exclude temporary files and logs.",
119 description
=> "Compress dump file.",
121 enum
=> ['0', '1', 'gzip', 'lzo', 'zstd'],
126 description
=> "Use pigz instead of gzip when N>0.".
127 " N=1 uses half of cores, N>1 uses N as thread count.",
133 description
=> "Zstd threads. N=0 uses half of the available cores,".
134 " N>0 uses N as thread count.",
140 description
=> "Be quiet.",
146 description
=> "Backup mode.",
148 default => 'snapshot',
149 enum
=> [ 'snapshot', 'suspend', 'stop' ],
152 type
=> 'string', format
=> 'pve-vmid-list',
153 description
=> "Exclude specified guest systems (assumes --all)",
158 description
=> "Exclude certain files/directories (shell globs)." .
159 " Paths starting with '/' are anchored to the container's root, " .
160 " other paths match relative to each subdirectory.",
168 format
=> 'email-or-username-list',
169 description
=> "Comma-separated list of email addresses or users that should" .
170 " receive email notifications.",
173 mailnotification
=> {
175 description
=> "Specify when to send an email",
177 enum
=> [ 'always', 'failure' ],
182 description
=> "Store temporary files to specified directory.",
187 description
=> "Store resulting files to specified directory.",
192 description
=> "Use specified hook script.",
195 storage
=> get_standard_option
('pve-storage-id', {
196 description
=> "Store resulting file to this storage.",
197 completion
=> \
&complete_backup_storage
,
202 description
=> "Stop running backup jobs on this host.",
208 description
=> "Limit I/O bandwidth (in KiB/s).",
215 description
=> "Set IO priority when using the BFQ scheduler. For snapshot and suspend "
216 ."mode backups of VMs, this only affects the compressor. A value of 8 means the idle "
217 ."priority is used, otherwise the best-effort priority is used with the specified "
226 description
=> "Other performance-related settings.",
227 format
=> 'backup-performance',
232 description
=> "Maximal time to wait for the global lock (minutes).",
235 default => 3*60, # 3 hours
239 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
242 default => 10, # 10 minutes
244 # FIXME remove with PVE 8.0 or PVE 9.0
247 description
=> "Deprecated: use 'prune-backups' instead. " .
248 "Maximal number of backup files per guest system.",
252 'prune-backups' => get_standard_option
('prune-backups', {
253 description
=> "Use these retention options instead of those from the storage configuration.",
255 default => "keep-all=1",
259 description
=> "Prune older backups according to 'prune-backups'.",
265 description
=> 'Backup all known guest systems included in the specified pool.',
268 'notes-template' => {
270 description
=> "Template string for generating notes for the backup(s). It can contain ".
271 "variables which will be replaced by their values. Currently supported are ".
272 "{{cluster}}, {{guestname}}, {{node}}, and {{vmid}}, but more might be added in the ".
273 "future. Needs to be a single line, newline and backslash need to be escaped as '\\n' ".
274 "and '\\\\' respectively.",
275 requires
=> 'storage',
281 description
=> "If true, mark backup(s) as protected.",
282 requires
=> 'storage',
291 # add JSON properties for create and set function
292 sub json_config_properties
{
295 foreach my $opt (keys %$confdesc) {
296 $prop->{$opt} = $confdesc->{$opt};
302 my $vzdump_properties = {
303 additionalProperties
=> 0,
304 properties
=> json_config_properties
({}),
307 sub parse_vzdump_cron_config
{
308 my ($filename, $raw) = @_;
310 my $jobs = []; # correct jobs
312 my $ejobs = []; # mailfomerd lines
314 my $jid = 1; # we start at 1
316 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
318 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
321 next if $line =~ m/^\#/;
322 next if $line =~ m/^\s*$/;
323 next if $line =~ m/^PATH\s*=/; # we always overwrite path
325 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?
(#)?vzdump(\s+(.*))?$|) {
327 my $minute = int($1);
333 my $dowhash = parse_dow
($dow, 1);
334 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
336 my $args = PVE
::Tools
::split_args
($param);
337 my $opts = PVE
::JSONSchema
::get_options
($vzdump_properties, $args, 'vmid');
339 $opts->{enabled
} = !defined($enabled);
340 $opts->{id
} = "$digest:$jid";
342 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
343 $opts->{dow
} = &$dowhash_to_dow($dowhash);
345 parse_property_strings
($opts);
351 syslog
('err', "parse error in '$filename': $err");
352 push @$ejobs, { line
=> $line };
354 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
355 syslog
('err', "warning: malformed line in '$filename'");
356 push @$ejobs, { line
=> $line };
358 syslog
('err', "ignoring malformed line in '$filename'");
363 $res->{digest
} = $digest;
364 $res->{jobs
} = $jobs;
365 $res->{ejobs
} = $ejobs;
370 sub write_vzdump_cron_config
{
371 my ($filename, $cfg) = @_;
373 my $out = "# cluster wide vzdump cron schedule\n";
374 $out .= "# Automatically generated file - do not edit\n\n";
375 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
377 my $jobs = $cfg->{jobs
} || [];
378 foreach my $job (@$jobs) {
379 my $enabled = ($job->{enabled
}) ?
'' : '#';
380 my $dh = parse_dow
($job->{dow
});
382 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
383 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
386 $dow = &$dowhash_to_dow($dh, 1);
392 die "no job start time specified\n" if !$job->{starttime
};
393 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
394 ($hour, $minute) = (int($1), int($2));
395 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
396 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
398 die "unable to parse job start time\n";
401 $job->{quiet
} = 1; # we do not want messages from cron
403 my $cmd = command_line
($job);
405 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
408 my $ejobs = $cfg->{ejobs
} || [];
409 foreach my $job (@$ejobs) {
410 $out .= "$job->{line}\n" if $job->{line
};
421 if ($param->{vmid
}) {
422 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
425 foreach my $p (keys %$param) {
426 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
427 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
428 my $v = $param->{$p};
429 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
430 if ($p eq 'exclude-path') {
431 foreach my $path (@$v) {
432 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
435 $v = join(",", PVE
::Tools
::split_list
($v)) if $p eq 'mailto';
436 $v = PVE
::JSONSchema
::print_property_string
($v, $PROPERTY_STRINGS->{$p})
437 if $PROPERTY_STRINGS->{$p};
439 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
446 # bash completion helpers
447 sub complete_backup_storage
{
449 my $cfg = PVE
::Storage
::config
();
450 my $ids = $cfg->{ids
};
452 my $nodename = PVE
::INotify
::nodename
();
455 foreach my $sid (keys %$ids) {
456 my $scfg = $ids->{$sid};
457 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
458 next if !$scfg->{content
}->{backup
};