]> git.proxmox.com Git - pve-guest-common.git/blob - src/PVE/VZDump/Common.pm
vzdump: deprecate mailto/mailnotification/notification-{target, policy}
[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 '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.",
95 type => 'integer',
96 minimum => 1,
97 default => 1048576,
98 optional => 1,
99 },
100 });
101
102 my $confdesc = {
103 vmid => {
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,
107 optional => 1,
108 },
109 node => get_standard_option('pve-node', {
110 description => "Only run if executed on this node.",
111 completion => \&PVE::Cluster::get_nodelist,
112 optional => 1,
113 }),
114 all => {
115 type => 'boolean',
116 description => "Backup all known guest systems on this host.",
117 optional => 1,
118 default => 0,
119 },
120 stdexcludes => {
121 type => 'boolean',
122 description => "Exclude temporary files and logs.",
123 optional => 1,
124 default => 1,
125 },
126 compress => {
127 type => 'string',
128 description => "Compress dump file.",
129 optional => 1,
130 enum => ['0', '1', 'gzip', 'lzo', 'zstd'],
131 default => '0',
132 },
133 pigz=> {
134 type => "integer",
135 description => "Use pigz instead of gzip when N>0.".
136 " N=1 uses half of cores, N>1 uses N as thread count.",
137 optional => 1,
138 default => 0,
139 },
140 zstd => {
141 type => "integer",
142 description => "Zstd threads. N=0 uses half of the available cores,".
143 " N>0 uses N as thread count.",
144 optional => 1,
145 default => 1,
146 },
147 quiet => {
148 type => 'boolean',
149 description => "Be quiet.",
150 optional => 1,
151 default => 0,
152 },
153 mode => {
154 type => 'string',
155 description => "Backup mode.",
156 optional => 1,
157 default => 'snapshot',
158 enum => [ 'snapshot', 'suspend', 'stop' ],
159 },
160 exclude => {
161 type => 'string', format => 'pve-vmid-list',
162 description => "Exclude specified guest systems (assumes --all)",
163 optional => 1,
164 },
165 'exclude-path' => {
166 type => 'array',
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.",
170 optional => 1,
171 items => {
172 type => 'string',
173 },
174 },
175 mailto => {
176 type => 'string',
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.",
181 optional => 1,
182 },
183 mailnotification => {
184 type => 'string',
185 description => "Deprecated: use notification targets/matchers instead." .
186 " Specify when to send a notification mail",
187 optional => 1,
188 enum => [ 'always', 'failure' ],
189 default => 'always',
190 },
191 'notification-policy' => {
192 type => 'string',
193 description => "Deprecated: Do not use",
194 optional => 1,
195 enum => [ 'always', 'failure', 'never'],
196 default => 'always',
197 },
198 'notification-target' => {
199 type => 'string',
200 format => 'pve-configid',
201 description => "Deprecated: Do not use",
202 optional => 1,
203 },
204 tmpdir => {
205 type => 'string',
206 description => "Store temporary files to specified directory.",
207 optional => 1,
208 },
209 dumpdir => {
210 type => 'string',
211 description => "Store resulting files to specified directory.",
212 optional => 1,
213 },
214 script => {
215 type => 'string',
216 description => "Use specified hook script.",
217 optional => 1,
218 },
219 storage => get_standard_option('pve-storage-id', {
220 description => "Store resulting file to this storage.",
221 completion => \&complete_backup_storage,
222 optional => 1,
223 }),
224 stop => {
225 type => 'boolean',
226 description => "Stop running backup jobs on this host.",
227 optional => 1,
228 default => 0,
229 },
230 bwlimit => {
231 type => 'integer',
232 description => "Limit I/O bandwidth (in KiB/s).",
233 optional => 1,
234 minimum => 0,
235 default => 0,
236 },
237 ionice => {
238 type => 'integer',
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 "
242 ."value.",
243 optional => 1,
244 minimum => 0,
245 maximum => 8,
246 default => 7,
247 },
248 performance => {
249 type => 'string',
250 description => "Other performance-related settings.",
251 format => 'backup-performance',
252 optional => 1,
253 },
254 lockwait => {
255 type => 'integer',
256 description => "Maximal time to wait for the global lock (minutes).",
257 optional => 1,
258 minimum => 0,
259 default => 3*60, # 3 hours
260 },
261 stopwait => {
262 type => 'integer',
263 description => "Maximal time to wait until a guest system is stopped (minutes).",
264 optional => 1,
265 minimum => 0,
266 default => 10, # 10 minutes
267 },
268 # FIXME remove with PVE 8.0 or PVE 9.0
269 maxfiles => {
270 type => 'integer',
271 description => "Deprecated: use 'prune-backups' instead. " .
272 "Maximal number of backup files per guest system.",
273 optional => 1,
274 minimum => 1,
275 },
276 'prune-backups' => get_standard_option('prune-backups', {
277 description => "Use these retention options instead of those from the storage configuration.",
278 optional => 1,
279 default => "keep-all=1",
280 }),
281 remove => {
282 type => 'boolean',
283 description => "Prune older backups according to 'prune-backups'.",
284 optional => 1,
285 default => 1,
286 },
287 pool => {
288 type => 'string',
289 description => 'Backup all known guest systems included in the specified pool.',
290 optional => 1,
291 },
292 'notes-template' => {
293 type => 'string',
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',
300 maxLength => 1024,
301 optional => 1,
302 },
303 protected => {
304 type => 'boolean',
305 description => "If true, mark backup(s) as protected.",
306 requires => 'storage',
307 optional => 1,
308 },
309 };
310
311 sub get_confdesc {
312 return $confdesc;
313 }
314
315 # add JSON properties for create and set function
316 sub json_config_properties {
317 my $prop = shift;
318
319 foreach my $opt (keys %$confdesc) {
320 $prop->{$opt} = $confdesc->{$opt};
321 }
322
323 return $prop;
324 }
325
326 my $vzdump_properties = {
327 additionalProperties => 0,
328 properties => json_config_properties({}),
329 };
330
331 sub parse_vzdump_cron_config {
332 my ($filename, $raw) = @_;
333
334 my $jobs = []; # correct jobs
335
336 my $ejobs = []; # mailfomerd lines
337
338 my $jid = 1; # we start at 1
339
340 my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
341
342 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
343 my $line = $1;
344
345 next if $line =~ m/^\#/;
346 next if $line =~ m/^\s*$/;
347 next if $line =~ m/^PATH\s*=/; # we always overwrite path
348
349 if ($line =~ m|^(\d+)\s+(\d+)\s+\*\s+\*\s+(\S+)\s+root\s+(/\S+/)?(#)?vzdump(\s+(.*))?$|) {
350 eval {
351 my $minute = int($1);
352 my $hour = int($2);
353 my $dow = $3;
354 my $param = $7;
355 my $enabled = $5;
356
357 my $dowhash = parse_dow($dow, 1);
358 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
359
360 my $args = PVE::Tools::split_args($param);
361 my $opts = PVE::JSONSchema::get_options($vzdump_properties, $args, 'vmid');
362
363 $opts->{enabled} = !defined($enabled);
364 $opts->{id} = "$digest:$jid";
365 $jid++;
366 $opts->{starttime} = sprintf "%02d:%02d", $hour, $minute;
367 $opts->{dow} = &$dowhash_to_dow($dowhash);
368
369 parse_property_strings($opts);
370
371 push @$jobs, $opts;
372 };
373 my $err = $@;
374 if ($err) {
375 syslog ('err', "parse error in '$filename': $err");
376 push @$ejobs, { line => $line };
377 }
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 };
381 } else {
382 syslog ('err', "ignoring malformed line in '$filename'");
383 }
384 }
385
386 my $res = {};
387 $res->{digest} = $digest;
388 $res->{jobs} = $jobs;
389 $res->{ejobs} = $ejobs;
390
391 return $res;
392 }
393
394 sub write_vzdump_cron_config {
395 my ($filename, $cfg) = @_;
396
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";
400
401 my $jobs = $cfg->{jobs} || [];
402 foreach my $job (@$jobs) {
403 my $enabled = ($job->{enabled}) ? '' : '#';
404 my $dh = parse_dow($job->{dow});
405 my $dow;
406 if ($dh->{mon} && $dh->{tue} && $dh->{wed} && $dh->{thu} &&
407 $dh->{fri} && $dh->{sat} && $dh->{sun}) {
408 $dow = '*';
409 } else {
410 $dow = &$dowhash_to_dow($dh, 1);
411 $dow = '*' if !$dow;
412 }
413
414 my ($hour, $minute);
415
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;
421 } else {
422 die "unable to parse job start time\n";
423 }
424
425 $job->{quiet} = 1; # we do not want messages from cron
426
427 my $cmd = command_line($job);
428
429 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
430 }
431
432 my $ejobs = $cfg->{ejobs} || [];
433 foreach my $job (@$ejobs) {
434 $out .= "$job->{line}\n" if $job->{line};
435 }
436
437 return $out;
438 }
439
440 sub command_line {
441 my ($param) = @_;
442
443 my $cmd = "vzdump";
444
445 if ($param->{vmid}) {
446 $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
447 }
448
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);
457 }
458 } else {
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};
462
463 $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
464 }
465 }
466
467 return $cmd;
468 }
469
470 # bash completion helpers
471 sub complete_backup_storage {
472
473 my $cfg = PVE::Storage::config();
474 my $ids = $cfg->{ids};
475
476 my $nodename = PVE::INotify::nodename();
477
478 my $res = [];
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};
483 push @$res, $sid;
484 }
485
486 return $res;
487 }
488
489 1;