]>
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)",
157 type
=> 'string', format
=> 'string-alist',
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.",
165 format
=> 'email-or-username-list',
166 description
=> "Comma-separated list of email addresses or users that should" .
167 " receive email notifications.",
170 mailnotification
=> {
172 description
=> "Specify when to send an email",
174 enum
=> [ 'always', 'failure' ],
179 description
=> "Store temporary files to specified directory.",
184 description
=> "Store resulting files to specified directory.",
189 description
=> "Use specified hook script.",
192 storage
=> get_standard_option
('pve-storage-id', {
193 description
=> "Store resulting file to this storage.",
194 completion
=> \
&complete_backup_storage
,
199 description
=> "Stop running backup jobs on this host.",
205 description
=> "Limit I/O bandwidth (KBytes per second).",
212 description
=> "Set CFQ ionice priority.",
220 description
=> "Other performance-related settings.",
221 format
=> 'backup-performance',
226 description
=> "Maximal time to wait for the global lock (minutes).",
229 default => 3*60, # 3 hours
233 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
236 default => 10, # 10 minutes
238 # FIXME remove with PVE 8.0 or PVE 9.0
241 description
=> "Deprecated: use 'prune-backups' instead. " .
242 "Maximal number of backup files per guest system.",
246 'prune-backups' => get_standard_option
('prune-backups', {
247 description
=> "Use these retention options instead of those from the storage configuration.",
249 default => "keep-all=1",
253 description
=> "Prune older backups according to 'prune-backups'.",
259 description
=> 'Backup all known guest systems included in the specified pool.',
262 'notes-template' => {
264 description
=> "Template string for generating notes for the backup(s). It can contain ".
265 "variables which will be replaced by their values. Currently supported are ".
266 "{{cluster}}, {{guestname}}, {{node}}, and {{vmid}}, but more might be added in the ".
267 "future. Needs to be a single line, newline and backslash need to be escaped as '\\n' ".
268 "and '\\\\' respectively.",
269 requires
=> 'storage',
275 description
=> "If true, mark backup(s) as protected.",
276 requires
=> 'storage',
285 # add JSON properties for create and set function
286 sub json_config_properties
{
289 foreach my $opt (keys %$confdesc) {
290 $prop->{$opt} = $confdesc->{$opt};
296 my $vzdump_properties = {
297 additionalProperties
=> 0,
298 properties
=> json_config_properties
({}),
301 sub parse_vzdump_cron_config
{
302 my ($filename, $raw) = @_;
304 my $jobs = []; # correct jobs
306 my $ejobs = []; # mailfomerd lines
308 my $jid = 1; # we start at 1
310 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
312 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
315 next if $line =~ m/^\#/;
316 next if $line =~ m/^\s*$/;
317 next if $line =~ m/^PATH\s*=/; # we always overwrite path
319 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?
(#)?vzdump(\s+(.*))?$|) {
321 my $minute = int($1);
327 my $dowhash = parse_dow
($dow, 1);
328 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
330 my $args = PVE
::Tools
::split_args
($param);
331 my $opts = PVE
::JSONSchema
::get_options
($vzdump_properties, $args, 'vmid');
333 $opts->{enabled
} = !defined($enabled);
334 $opts->{id
} = "$digest:$jid";
336 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
337 $opts->{dow
} = &$dowhash_to_dow($dowhash);
339 parse_property_strings
($opts);
345 syslog
('err', "parse error in '$filename': $err");
346 push @$ejobs, { line
=> $line };
348 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
349 syslog
('err', "warning: malformed line in '$filename'");
350 push @$ejobs, { line
=> $line };
352 syslog
('err', "ignoring malformed line in '$filename'");
357 $res->{digest
} = $digest;
358 $res->{jobs
} = $jobs;
359 $res->{ejobs
} = $ejobs;
364 sub write_vzdump_cron_config
{
365 my ($filename, $cfg) = @_;
367 my $out = "# cluster wide vzdump cron schedule\n";
368 $out .= "# Automatically generated file - do not edit\n\n";
369 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
371 my $jobs = $cfg->{jobs
} || [];
372 foreach my $job (@$jobs) {
373 my $enabled = ($job->{enabled
}) ?
'' : '#';
374 my $dh = parse_dow
($job->{dow
});
376 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
377 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
380 $dow = &$dowhash_to_dow($dh, 1);
386 die "no job start time specified\n" if !$job->{starttime
};
387 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
388 ($hour, $minute) = (int($1), int($2));
389 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
390 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
392 die "unable to parse job start time\n";
395 $job->{quiet
} = 1; # we do not want messages from cron
397 my $cmd = command_line
($job);
399 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
402 my $ejobs = $cfg->{ejobs
} || [];
403 foreach my $job (@$ejobs) {
404 $out .= "$job->{line}\n" if $job->{line
};
415 if ($param->{vmid
}) {
416 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
419 foreach my $p (keys %$param) {
420 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
421 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
422 my $v = $param->{$p};
423 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
424 if ($p eq 'exclude-path') {
425 foreach my $path (split(/\0/, $v || '')) {
426 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
429 $v = join(",", PVE
::Tools
::split_list
($v)) if $p eq 'mailto';
430 $v = PVE
::JSONSchema
::print_property_string
($v, $PROPERTY_STRINGS->{$p})
431 if $PROPERTY_STRINGS->{$p};
433 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
440 # bash completion helpers
441 sub complete_backup_storage
{
443 my $cfg = PVE
::Storage
::config
();
444 my $ids = $cfg->{ids
};
446 my $nodename = PVE
::INotify
::nodename
();
449 foreach my $sid (keys %$ids) {
450 my $scfg = $ids->{$sid};
451 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
452 next if !$scfg->{content
}->{backup
};