]>
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 cfs_register_file
('vzdump.cron',
14 \
&parse_vzdump_cron_config
,
15 \
&write_vzdump_cron_config
);
17 my $dowhash_to_dow = sub {
21 push @da, $num ?
1 : 'mon' if $d->{mon
};
22 push @da, $num ?
2 : 'tue' if $d->{tue
};
23 push @da, $num ?
3 : 'wed' if $d->{wed
};
24 push @da, $num ?
4 : 'thu' if $d->{thu
};
25 push @da, $num ?
5 : 'fri' if $d->{fri
};
26 push @da, $num ?
6 : 'sat' if $d->{sat
};
27 push @da, $num ?
7 : 'sun' if $d->{sun
};
32 my $fixup_prune_backups_option = sub {
35 return if !defined($opts->{'prune-backups'});
37 $opts->{'prune-backups'} = PVE
::JSONSchema
::parse_property_string
(
39 $opts->{'prune-backups'}
43 # parse crontab style day of week
45 my ($dowstr, $noerr) = @_;
47 my $dowmap = {mon
=> 1, tue
=> 2, wed
=> 3, thu
=> 4,
48 fri
=> 5, sat
=> 6, sun
=> 7};
49 my $rdowmap = { '1' => 'mon', '2' => 'tue', '3' => 'wed', '4' => 'thu',
50 '5' => 'fri', '6' => 'sat', '7' => 'sun', '0' => 'sun'};
54 $dowstr = '1,2,3,4,5,6,7' if $dowstr eq '*';
56 foreach my $day (PVE
::Tools
::split_list
($dowstr)) {
57 if ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun)-(mon|tue|wed|thu|fri|sat|sun)$/i) {
58 for (my $i = $dowmap->{lc($1)}; $i <= $dowmap->{lc($2)}; $i++) {
59 my $r = $rdowmap->{$i};
62 } elsif ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun|[0-7])$/i) {
63 $day = $rdowmap->{$day} if $day =~ m/\d/;
66 return undef if $noerr;
67 die "unable to parse day of week '$dowstr'\n";
76 type
=> 'string', format
=> 'pve-vmid-list',
77 description
=> "The ID of the guest system you want to backup.",
78 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
81 node
=> get_standard_option
('pve-node', {
82 description
=> "Only run if executed on this node.",
83 completion
=> \
&PVE
::Cluster
::get_nodelist
,
88 description
=> "Backup all known guest systems on this host.",
94 description
=> "Exclude temporary files and logs.",
100 description
=> "Compress dump file.",
102 enum
=> ['0', '1', 'gzip', 'lzo', 'zstd'],
107 description
=> "Use pigz instead of gzip when N>0.".
108 " N=1 uses half of cores, N>1 uses N as thread count.",
114 description
=> "Zstd threads. N=0 uses half of the available cores,".
115 " N>0 uses N as thread count.",
121 description
=> "Be quiet.",
127 description
=> "Backup mode.",
129 default => 'snapshot',
130 enum
=> [ 'snapshot', 'suspend', 'stop' ],
133 type
=> 'string', format
=> 'pve-vmid-list',
134 description
=> "Exclude specified guest systems (assumes --all)",
138 type
=> 'string', format
=> 'string-alist',
139 description
=> "Exclude certain files/directories (shell globs)." .
140 " Paths starting with '/' are anchored to the container's root, " .
141 " other paths match relative to each subdirectory.",
146 format
=> 'email-or-username-list',
147 description
=> "Comma-separated list of email addresses or users that should" .
148 " receive email notifications.",
151 mailnotification
=> {
153 description
=> "Specify when to send an email",
155 enum
=> [ 'always', 'failure' ],
160 description
=> "Store temporary files to specified directory.",
165 description
=> "Store resulting files to specified directory.",
170 description
=> "Use specified hook script.",
173 storage
=> get_standard_option
('pve-storage-id', {
174 description
=> "Store resulting file to this storage.",
175 completion
=> \
&complete_backup_storage
,
180 description
=> "Stop running backup jobs on this host.",
186 description
=> "Limit I/O bandwidth (KBytes per second).",
193 description
=> "Set CFQ ionice priority.",
201 description
=> "Maximal time to wait for the global lock (minutes).",
204 default => 3*60, # 3 hours
208 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
211 default => 10, # 10 minutes
213 # FIXME remove with PVE 8.0 or PVE 9.0
216 description
=> "Deprecated: use 'prune-backups' instead. " .
217 "Maximal number of backup files per guest system.",
221 'prune-backups' => get_standard_option
('prune-backups', {
222 description
=> "Use these retention options instead of those from the storage configuration.",
224 default => "keep-all=1",
228 description
=> "Prune older backups according to 'prune-backups'.",
234 description
=> 'Backup all known guest systems included in the specified pool.',
243 # add JSON properties for create and set function
244 sub json_config_properties
{
247 foreach my $opt (keys %$confdesc) {
248 $prop->{$opt} = $confdesc->{$opt};
254 my $vzdump_properties = {
255 additionalProperties
=> 0,
256 properties
=> json_config_properties
({}),
259 sub parse_vzdump_cron_config
{
260 my ($filename, $raw) = @_;
262 my $jobs = []; # correct jobs
264 my $ejobs = []; # mailfomerd lines
266 my $jid = 1; # we start at 1
268 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
270 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
273 next if $line =~ m/^\#/;
274 next if $line =~ m/^\s*$/;
275 next if $line =~ m/^PATH\s*=/; # we always overwrite path
277 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?
(#)?vzdump(\s+(.*))?$|) {
279 my $minute = int($1);
285 my $dowhash = parse_dow
($dow, 1);
286 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
288 my $args = PVE
::Tools
::split_args
($param);
289 my $opts = PVE
::JSONSchema
::get_options
($vzdump_properties, $args, 'vmid');
291 $opts->{enabled
} = !defined($enabled);
292 $opts->{id
} = "$digest:$jid";
294 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
295 $opts->{dow
} = &$dowhash_to_dow($dowhash);
297 $fixup_prune_backups_option->($opts); # parse the property string
303 syslog
('err', "parse error in '$filename': $err");
304 push @$ejobs, { line
=> $line };
306 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
307 syslog
('err', "warning: malformed line in '$filename'");
308 push @$ejobs, { line
=> $line };
310 syslog
('err', "ignoring malformed line in '$filename'");
315 $res->{digest
} = $digest;
316 $res->{jobs
} = $jobs;
317 $res->{ejobs
} = $ejobs;
322 sub write_vzdump_cron_config
{
323 my ($filename, $cfg) = @_;
325 my $out = "# cluster wide vzdump cron schedule\n";
326 $out .= "# Automatically generated file - do not edit\n\n";
327 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
329 my $jobs = $cfg->{jobs
} || [];
330 foreach my $job (@$jobs) {
331 my $enabled = ($job->{enabled
}) ?
'' : '#';
332 my $dh = parse_dow
($job->{dow
});
334 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
335 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
338 $dow = &$dowhash_to_dow($dh, 1);
344 die "no job start time specified\n" if !$job->{starttime
};
345 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
346 ($hour, $minute) = (int($1), int($2));
347 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
348 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
350 die "unable to parse job start time\n";
353 $job->{quiet
} = 1; # we do not want messages from cron
355 my $cmd = command_line
($job);
357 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
360 my $ejobs = $cfg->{ejobs
} || [];
361 foreach my $job (@$ejobs) {
362 $out .= "$job->{line}\n" if $job->{line
};
373 if ($param->{vmid
}) {
374 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
377 foreach my $p (keys %$param) {
378 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
379 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
380 my $v = $param->{$p};
381 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
382 if ($p eq 'exclude-path') {
383 foreach my $path (split(/\0/, $v || '')) {
384 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
387 $v = join(",", PVE
::Tools
::split_list
($v)) if $p eq 'mailto';
388 $v = PVE
::JSONSchema
::print_property_string
($v, 'prune-backups')
389 if $p eq 'prune-backups';
391 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
398 # bash completion helpers
399 sub complete_backup_storage
{
401 my $cfg = PVE
::Storage
::config
();
402 my $ids = $cfg->{ids
};
404 my $nodename = PVE
::INotify
::nodename
();
407 foreach my $sid (keys %$ids) {
408 my $scfg = $ids->{$sid};
409 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
410 next if !$scfg->{content
}->{backup
};