]>
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.",
91 'pbs-entries-max' => {
92 description
=> "Applies to container backups sent to PBS. Limits the number of entries "
93 ."allowed in memory at a given time to avoid unintended OOM situations. Increase it to "
94 ."enable backups of containers with a large amount of files.",
104 type
=> 'string', format
=> 'pve-vmid-list',
105 description
=> "The ID of the guest system you want to backup.",
106 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
109 node
=> get_standard_option
('pve-node', {
110 description
=> "Only run if executed on this node.",
111 completion
=> \
&PVE
::Cluster
::get_nodelist
,
116 description
=> "Backup all known guest systems on this host.",
122 description
=> "Exclude temporary files and logs.",
128 description
=> "Compress dump file.",
130 enum
=> ['0', '1', 'gzip', 'lzo', 'zstd'],
135 description
=> "Use pigz instead of gzip when N>0.".
136 " N=1 uses half of cores, N>1 uses N as thread count.",
142 description
=> "Zstd threads. N=0 uses half of the available cores,".
143 " N>0 uses N as thread count.",
149 description
=> "Be quiet.",
155 description
=> "Backup mode.",
157 default => 'snapshot',
158 enum
=> [ 'snapshot', 'suspend', 'stop' ],
161 type
=> 'string', format
=> 'pve-vmid-list',
162 description
=> "Exclude specified guest systems (assumes --all)",
167 description
=> "Exclude certain files/directories (shell globs)." .
168 " Paths starting with '/' are anchored to the container's root, " .
169 " other paths match relative to each subdirectory.",
177 format
=> 'email-or-username-list',
178 description
=> "Deprecated: Use notification targets/matchers instead." .
179 " Comma-separated list of email addresses or users that should" .
180 " receive email notifications.",
183 mailnotification
=> {
185 description
=> "Deprecated: use notification targets/matchers instead." .
186 " Specify when to send a notification mail",
188 enum
=> [ 'always', 'failure' ],
191 'notification-policy' => {
193 description
=> "Deprecated: Do not use",
195 enum
=> [ 'always', 'failure', 'never'],
198 'notification-target' => {
200 format
=> 'pve-configid',
201 description
=> "Deprecated: Do not use",
206 description
=> "Store temporary files to specified directory.",
211 description
=> "Store resulting files to specified directory.",
216 description
=> "Use specified hook script.",
219 storage
=> get_standard_option
('pve-storage-id', {
220 description
=> "Store resulting file to this storage.",
221 completion
=> \
&complete_backup_storage
,
226 description
=> "Stop running backup jobs on this host.",
232 description
=> "Limit I/O bandwidth (in KiB/s).",
239 description
=> "Set IO priority when using the BFQ scheduler. For snapshot and suspend "
240 ."mode backups of VMs, this only affects the compressor. A value of 8 means the idle "
241 ."priority is used, otherwise the best-effort priority is used with the specified "
250 description
=> "Other performance-related settings.",
251 format
=> 'backup-performance',
256 description
=> "Maximal time to wait for the global lock (minutes).",
259 default => 3*60, # 3 hours
263 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
266 default => 10, # 10 minutes
268 # FIXME remove with PVE 8.0 or PVE 9.0
271 description
=> "Deprecated: use 'prune-backups' instead. " .
272 "Maximal number of backup files per guest system.",
276 'prune-backups' => get_standard_option
('prune-backups', {
277 description
=> "Use these retention options instead of those from the storage configuration.",
279 default => "keep-all=1",
283 description
=> "Prune older backups according to 'prune-backups'.",
289 description
=> 'Backup all known guest systems included in the specified pool.',
292 'notes-template' => {
294 description
=> "Template string for generating notes for the backup(s). It can contain ".
295 "variables which will be replaced by their values. Currently supported are ".
296 "{{cluster}}, {{guestname}}, {{node}}, and {{vmid}}, but more might be added in the ".
297 "future. Needs to be a single line, newline and backslash need to be escaped as '\\n' ".
298 "and '\\\\' respectively.",
299 requires
=> 'storage',
305 description
=> "If true, mark backup(s) as protected.",
306 requires
=> 'storage',
315 # add JSON properties for create and set function
316 sub json_config_properties
{
319 foreach my $opt (keys %$confdesc) {
320 $prop->{$opt} = $confdesc->{$opt};
326 my $vzdump_properties = {
327 additionalProperties
=> 0,
328 properties
=> json_config_properties
({}),
331 sub parse_vzdump_cron_config
{
332 my ($filename, $raw) = @_;
334 my $jobs = []; # correct jobs
336 my $ejobs = []; # mailfomerd lines
338 my $jid = 1; # we start at 1
340 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
342 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
345 next if $line =~ m/^\#/;
346 next if $line =~ m/^\s*$/;
347 next if $line =~ m/^PATH\s*=/; # we always overwrite path
349 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?
(#)?vzdump(\s+(.*))?$|) {
351 my $minute = int($1);
357 my $dowhash = parse_dow
($dow, 1);
358 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
360 my $args = PVE
::Tools
::split_args
($param);
361 my $opts = PVE
::JSONSchema
::get_options
($vzdump_properties, $args, 'vmid');
363 $opts->{enabled
} = !defined($enabled);
364 $opts->{id
} = "$digest:$jid";
366 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
367 $opts->{dow
} = &$dowhash_to_dow($dowhash);
369 parse_property_strings
($opts);
375 syslog
('err', "parse error in '$filename': $err");
376 push @$ejobs, { line
=> $line };
378 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
379 syslog
('err', "warning: malformed line in '$filename'");
380 push @$ejobs, { line
=> $line };
382 syslog
('err', "ignoring malformed line in '$filename'");
387 $res->{digest
} = $digest;
388 $res->{jobs
} = $jobs;
389 $res->{ejobs
} = $ejobs;
394 sub write_vzdump_cron_config
{
395 my ($filename, $cfg) = @_;
397 my $out = "# cluster wide vzdump cron schedule\n";
398 $out .= "# Automatically generated file - do not edit\n\n";
399 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
401 my $jobs = $cfg->{jobs
} || [];
402 foreach my $job (@$jobs) {
403 my $enabled = ($job->{enabled
}) ?
'' : '#';
404 my $dh = parse_dow
($job->{dow
});
406 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
407 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
410 $dow = &$dowhash_to_dow($dh, 1);
416 die "no job start time specified\n" if !$job->{starttime
};
417 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
418 ($hour, $minute) = (int($1), int($2));
419 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
420 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
422 die "unable to parse job start time\n";
425 $job->{quiet
} = 1; # we do not want messages from cron
427 my $cmd = command_line
($job);
429 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
432 my $ejobs = $cfg->{ejobs
} || [];
433 foreach my $job (@$ejobs) {
434 $out .= "$job->{line}\n" if $job->{line
};
445 if ($param->{vmid
}) {
446 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
449 foreach my $p (keys %$param) {
450 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
451 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
452 my $v = $param->{$p};
453 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
454 if ($p eq 'exclude-path') {
455 foreach my $path (@$v) {
456 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
459 $v = join(",", PVE
::Tools
::split_list
($v)) if $p eq 'mailto';
460 $v = PVE
::JSONSchema
::print_property_string
($v, $PROPERTY_STRINGS->{$p})
461 if $PROPERTY_STRINGS->{$p};
463 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
470 # bash completion helpers
471 sub complete_backup_storage
{
473 my $cfg = PVE
::Storage
::config
();
474 my $ids = $cfg->{ids
};
476 my $nodename = PVE
::INotify
::nodename
();
479 foreach my $sid (keys %$ids) {
480 my $scfg = $ids->{$sid};
481 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
482 next if !$scfg->{content
}->{backup
};