]> git.proxmox.com Git - pve-guest-common.git/blob - src/PVE/VZDump/Common.pm
vzdump: config: improve description of ionice setting
[pve-guest-common.git] / src / PVE / VZDump / Common.pm
1 package PVE::VZDump::Common;
2
3 use strict;
4 use warnings;
5 use Digest::SHA;
6
7 use PVE::Tools;
8 use PVE::SafeSyslog qw(syslog);
9 use PVE::Storage;
10 use PVE::Cluster qw(cfs_register_file);
11 use PVE::JSONSchema qw(get_standard_option);
12
13 # NOTE: this is the legacy config, nowadays jobs.cfg is used (handled in pve-manager)
14 cfs_register_file(
15 'vzdump.cron',
16 \&parse_vzdump_cron_config,
17 \&write_vzdump_cron_config,
18 );
19
20 my $dowhash_to_dow = sub {
21 my ($d, $num) = @_;
22
23 my @da = ();
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};
31
32 return join ',', @da;
33 };
34
35 our $PROPERTY_STRINGS = {
36 'performance' => 'backup-performance',
37 'prune-backups' => 'prune-backups',
38 };
39
40 my sub parse_property_strings {
41 my ($opts) = @_;
42
43 for my $opt (keys $PROPERTY_STRINGS->%*) {
44 next if !defined($opts->{$opt});
45
46 my $format = $PROPERTY_STRINGS->{$opt};
47 $opts->{$opt} = PVE::JSONSchema::parse_property_string($format, $opts->{$opt});
48 }
49 }
50
51 # parse crontab style day of week
52 sub parse_dow {
53 my ($dowstr, $noerr) = @_;
54
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'};
59
60 my $res = {};
61
62 $dowstr = '1,2,3,4,5,6,7' if $dowstr eq '*';
63
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};
68 $res->{$r} = 1;
69 }
70 } elsif ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun|[0-7])$/i) {
71 $day = $rdowmap->{$day} if $day =~ m/\d/;
72 $res->{lc($day)} = 1;
73 } else {
74 return undef if $noerr;
75 die "unable to parse day of week '$dowstr'\n";
76 }
77 }
78
79 return $res;
80 };
81
82 PVE::JSONSchema::register_format('backup-performance', {
83 'max-workers' => {
84 description => "Applies to VMs. Allow up to this many IO workers at the same time.",
85 type => 'integer',
86 minimum => 1,
87 maximum => 256,
88 default => 16,
89 optional => 1,
90 },
91 });
92
93 my $confdesc = {
94 vmid => {
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,
98 optional => 1,
99 },
100 node => get_standard_option('pve-node', {
101 description => "Only run if executed on this node.",
102 completion => \&PVE::Cluster::get_nodelist,
103 optional => 1,
104 }),
105 all => {
106 type => 'boolean',
107 description => "Backup all known guest systems on this host.",
108 optional => 1,
109 default => 0,
110 },
111 stdexcludes => {
112 type => 'boolean',
113 description => "Exclude temporary files and logs.",
114 optional => 1,
115 default => 1,
116 },
117 compress => {
118 type => 'string',
119 description => "Compress dump file.",
120 optional => 1,
121 enum => ['0', '1', 'gzip', 'lzo', 'zstd'],
122 default => '0',
123 },
124 pigz=> {
125 type => "integer",
126 description => "Use pigz instead of gzip when N>0.".
127 " N=1 uses half of cores, N>1 uses N as thread count.",
128 optional => 1,
129 default => 0,
130 },
131 zstd => {
132 type => "integer",
133 description => "Zstd threads. N=0 uses half of the available cores,".
134 " N>0 uses N as thread count.",
135 optional => 1,
136 default => 1,
137 },
138 quiet => {
139 type => 'boolean',
140 description => "Be quiet.",
141 optional => 1,
142 default => 0,
143 },
144 mode => {
145 type => 'string',
146 description => "Backup mode.",
147 optional => 1,
148 default => 'snapshot',
149 enum => [ 'snapshot', 'suspend', 'stop' ],
150 },
151 exclude => {
152 type => 'string', format => 'pve-vmid-list',
153 description => "Exclude specified guest systems (assumes --all)",
154 optional => 1,
155 },
156 'exclude-path' => {
157 type => 'array',
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.",
161 optional => 1,
162 items => {
163 type => 'string',
164 },
165 },
166 mailto => {
167 type => 'string',
168 format => 'email-or-username-list',
169 description => "Comma-separated list of email addresses or users that should" .
170 " receive email notifications.",
171 optional => 1,
172 },
173 mailnotification => {
174 type => 'string',
175 description => "Specify when to send an email",
176 optional => 1,
177 enum => [ 'always', 'failure' ],
178 default => 'always',
179 },
180 tmpdir => {
181 type => 'string',
182 description => "Store temporary files to specified directory.",
183 optional => 1,
184 },
185 dumpdir => {
186 type => 'string',
187 description => "Store resulting files to specified directory.",
188 optional => 1,
189 },
190 script => {
191 type => 'string',
192 description => "Use specified hook script.",
193 optional => 1,
194 },
195 storage => get_standard_option('pve-storage-id', {
196 description => "Store resulting file to this storage.",
197 completion => \&complete_backup_storage,
198 optional => 1,
199 }),
200 stop => {
201 type => 'boolean',
202 description => "Stop running backup jobs on this host.",
203 optional => 1,
204 default => 0,
205 },
206 bwlimit => {
207 type => 'integer',
208 description => "Limit I/O bandwidth (in KiB/s).",
209 optional => 1,
210 minimum => 0,
211 default => 0,
212 },
213 ionice => {
214 type => 'integer',
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 "
218 ."value.",
219 optional => 1,
220 minimum => 0,
221 maximum => 8,
222 default => 7,
223 },
224 performance => {
225 type => 'string',
226 description => "Other performance-related settings.",
227 format => 'backup-performance',
228 optional => 1,
229 },
230 lockwait => {
231 type => 'integer',
232 description => "Maximal time to wait for the global lock (minutes).",
233 optional => 1,
234 minimum => 0,
235 default => 3*60, # 3 hours
236 },
237 stopwait => {
238 type => 'integer',
239 description => "Maximal time to wait until a guest system is stopped (minutes).",
240 optional => 1,
241 minimum => 0,
242 default => 10, # 10 minutes
243 },
244 # FIXME remove with PVE 8.0 or PVE 9.0
245 maxfiles => {
246 type => 'integer',
247 description => "Deprecated: use 'prune-backups' instead. " .
248 "Maximal number of backup files per guest system.",
249 optional => 1,
250 minimum => 1,
251 },
252 'prune-backups' => get_standard_option('prune-backups', {
253 description => "Use these retention options instead of those from the storage configuration.",
254 optional => 1,
255 default => "keep-all=1",
256 }),
257 remove => {
258 type => 'boolean',
259 description => "Prune older backups according to 'prune-backups'.",
260 optional => 1,
261 default => 1,
262 },
263 pool => {
264 type => 'string',
265 description => 'Backup all known guest systems included in the specified pool.',
266 optional => 1,
267 },
268 'notes-template' => {
269 type => 'string',
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',
276 maxLength => 1024,
277 optional => 1,
278 },
279 protected => {
280 type => 'boolean',
281 description => "If true, mark backup(s) as protected.",
282 requires => 'storage',
283 optional => 1,
284 },
285 };
286
287 sub get_confdesc {
288 return $confdesc;
289 }
290
291 # add JSON properties for create and set function
292 sub json_config_properties {
293 my $prop = shift;
294
295 foreach my $opt (keys %$confdesc) {
296 $prop->{$opt} = $confdesc->{$opt};
297 }
298
299 return $prop;
300 }
301
302 my $vzdump_properties = {
303 additionalProperties => 0,
304 properties => json_config_properties({}),
305 };
306
307 sub parse_vzdump_cron_config {
308 my ($filename, $raw) = @_;
309
310 my $jobs = []; # correct jobs
311
312 my $ejobs = []; # mailfomerd lines
313
314 my $jid = 1; # we start at 1
315
316 my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
317
318 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
319 my $line = $1;
320
321 next if $line =~ m/^\#/;
322 next if $line =~ m/^\s*$/;
323 next if $line =~ m/^PATH\s*=/; # we always overwrite path
324
325 if ($line =~ m|^(\d+)\s+(\d+)\s+\*\s+\*\s+(\S+)\s+root\s+(/\S+/)?(#)?vzdump(\s+(.*))?$|) {
326 eval {
327 my $minute = int($1);
328 my $hour = int($2);
329 my $dow = $3;
330 my $param = $7;
331 my $enabled = $5;
332
333 my $dowhash = parse_dow($dow, 1);
334 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
335
336 my $args = PVE::Tools::split_args($param);
337 my $opts = PVE::JSONSchema::get_options($vzdump_properties, $args, 'vmid');
338
339 $opts->{enabled} = !defined($enabled);
340 $opts->{id} = "$digest:$jid";
341 $jid++;
342 $opts->{starttime} = sprintf "%02d:%02d", $hour, $minute;
343 $opts->{dow} = &$dowhash_to_dow($dowhash);
344
345 parse_property_strings($opts);
346
347 push @$jobs, $opts;
348 };
349 my $err = $@;
350 if ($err) {
351 syslog ('err', "parse error in '$filename': $err");
352 push @$ejobs, { line => $line };
353 }
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 };
357 } else {
358 syslog ('err', "ignoring malformed line in '$filename'");
359 }
360 }
361
362 my $res = {};
363 $res->{digest} = $digest;
364 $res->{jobs} = $jobs;
365 $res->{ejobs} = $ejobs;
366
367 return $res;
368 }
369
370 sub write_vzdump_cron_config {
371 my ($filename, $cfg) = @_;
372
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";
376
377 my $jobs = $cfg->{jobs} || [];
378 foreach my $job (@$jobs) {
379 my $enabled = ($job->{enabled}) ? '' : '#';
380 my $dh = parse_dow($job->{dow});
381 my $dow;
382 if ($dh->{mon} && $dh->{tue} && $dh->{wed} && $dh->{thu} &&
383 $dh->{fri} && $dh->{sat} && $dh->{sun}) {
384 $dow = '*';
385 } else {
386 $dow = &$dowhash_to_dow($dh, 1);
387 $dow = '*' if !$dow;
388 }
389
390 my ($hour, $minute);
391
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;
397 } else {
398 die "unable to parse job start time\n";
399 }
400
401 $job->{quiet} = 1; # we do not want messages from cron
402
403 my $cmd = command_line($job);
404
405 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
406 }
407
408 my $ejobs = $cfg->{ejobs} || [];
409 foreach my $job (@$ejobs) {
410 $out .= "$job->{line}\n" if $job->{line};
411 }
412
413 return $out;
414 }
415
416 sub command_line {
417 my ($param) = @_;
418
419 my $cmd = "vzdump";
420
421 if ($param->{vmid}) {
422 $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
423 }
424
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);
433 }
434 } else {
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};
438
439 $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
440 }
441 }
442
443 return $cmd;
444 }
445
446 # bash completion helpers
447 sub complete_backup_storage {
448
449 my $cfg = PVE::Storage::config();
450 my $ids = $cfg->{ids};
451
452 my $nodename = PVE::INotify::nodename();
453
454 my $res = [];
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};
459 push @$res, $sid;
460 }
461
462 return $res;
463 }
464
465 1;