]> git.proxmox.com Git - pve-guest-common.git/blob - src/PVE/VZDump/Common.pm
vzdump: comment that vzdump.cron is the legacy parser
[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 => '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.",
161 optional => 1,
162 },
163 mailto => {
164 type => 'string',
165 format => 'email-or-username-list',
166 description => "Comma-separated list of email addresses or users that should" .
167 " receive email notifications.",
168 optional => 1,
169 },
170 mailnotification => {
171 type => 'string',
172 description => "Specify when to send an email",
173 optional => 1,
174 enum => [ 'always', 'failure' ],
175 default => 'always',
176 },
177 tmpdir => {
178 type => 'string',
179 description => "Store temporary files to specified directory.",
180 optional => 1,
181 },
182 dumpdir => {
183 type => 'string',
184 description => "Store resulting files to specified directory.",
185 optional => 1,
186 },
187 script => {
188 type => 'string',
189 description => "Use specified hook script.",
190 optional => 1,
191 },
192 storage => get_standard_option('pve-storage-id', {
193 description => "Store resulting file to this storage.",
194 completion => \&complete_backup_storage,
195 optional => 1,
196 }),
197 stop => {
198 type => 'boolean',
199 description => "Stop running backup jobs on this host.",
200 optional => 1,
201 default => 0,
202 },
203 bwlimit => {
204 type => 'integer',
205 description => "Limit I/O bandwidth (KBytes per second).",
206 optional => 1,
207 minimum => 0,
208 default => 0,
209 },
210 ionice => {
211 type => 'integer',
212 description => "Set CFQ ionice priority.",
213 optional => 1,
214 minimum => 0,
215 maximum => 8,
216 default => 7,
217 },
218 performance => {
219 type => 'string',
220 description => "Other performance-related settings.",
221 format => 'backup-performance',
222 optional => 1,
223 },
224 lockwait => {
225 type => 'integer',
226 description => "Maximal time to wait for the global lock (minutes).",
227 optional => 1,
228 minimum => 0,
229 default => 3*60, # 3 hours
230 },
231 stopwait => {
232 type => 'integer',
233 description => "Maximal time to wait until a guest system is stopped (minutes).",
234 optional => 1,
235 minimum => 0,
236 default => 10, # 10 minutes
237 },
238 # FIXME remove with PVE 8.0 or PVE 9.0
239 maxfiles => {
240 type => 'integer',
241 description => "Deprecated: use 'prune-backups' instead. " .
242 "Maximal number of backup files per guest system.",
243 optional => 1,
244 minimum => 1,
245 },
246 'prune-backups' => get_standard_option('prune-backups', {
247 description => "Use these retention options instead of those from the storage configuration.",
248 optional => 1,
249 default => "keep-all=1",
250 }),
251 remove => {
252 type => 'boolean',
253 description => "Prune older backups according to 'prune-backups'.",
254 optional => 1,
255 default => 1,
256 },
257 pool => {
258 type => 'string',
259 description => 'Backup all known guest systems included in the specified pool.',
260 optional => 1,
261 },
262 'notes-template' => {
263 type => 'string',
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',
270 maxLength => 1024,
271 optional => 1,
272 },
273 protected => {
274 type => 'boolean',
275 description => "If true, mark backup(s) as protected.",
276 requires => 'storage',
277 optional => 1,
278 },
279 };
280
281 sub get_confdesc {
282 return $confdesc;
283 }
284
285 # add JSON properties for create and set function
286 sub json_config_properties {
287 my $prop = shift;
288
289 foreach my $opt (keys %$confdesc) {
290 $prop->{$opt} = $confdesc->{$opt};
291 }
292
293 return $prop;
294 }
295
296 my $vzdump_properties = {
297 additionalProperties => 0,
298 properties => json_config_properties({}),
299 };
300
301 sub parse_vzdump_cron_config {
302 my ($filename, $raw) = @_;
303
304 my $jobs = []; # correct jobs
305
306 my $ejobs = []; # mailfomerd lines
307
308 my $jid = 1; # we start at 1
309
310 my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : '');
311
312 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
313 my $line = $1;
314
315 next if $line =~ m/^\#/;
316 next if $line =~ m/^\s*$/;
317 next if $line =~ m/^PATH\s*=/; # we always overwrite path
318
319 if ($line =~ m|^(\d+)\s+(\d+)\s+\*\s+\*\s+(\S+)\s+root\s+(/\S+/)?(#)?vzdump(\s+(.*))?$|) {
320 eval {
321 my $minute = int($1);
322 my $hour = int($2);
323 my $dow = $3;
324 my $param = $7;
325 my $enabled = $5;
326
327 my $dowhash = parse_dow($dow, 1);
328 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
329
330 my $args = PVE::Tools::split_args($param);
331 my $opts = PVE::JSONSchema::get_options($vzdump_properties, $args, 'vmid');
332
333 $opts->{enabled} = !defined($enabled);
334 $opts->{id} = "$digest:$jid";
335 $jid++;
336 $opts->{starttime} = sprintf "%02d:%02d", $hour, $minute;
337 $opts->{dow} = &$dowhash_to_dow($dowhash);
338
339 parse_property_strings($opts);
340
341 push @$jobs, $opts;
342 };
343 my $err = $@;
344 if ($err) {
345 syslog ('err', "parse error in '$filename': $err");
346 push @$ejobs, { line => $line };
347 }
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 };
351 } else {
352 syslog ('err', "ignoring malformed line in '$filename'");
353 }
354 }
355
356 my $res = {};
357 $res->{digest} = $digest;
358 $res->{jobs} = $jobs;
359 $res->{ejobs} = $ejobs;
360
361 return $res;
362 }
363
364 sub write_vzdump_cron_config {
365 my ($filename, $cfg) = @_;
366
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";
370
371 my $jobs = $cfg->{jobs} || [];
372 foreach my $job (@$jobs) {
373 my $enabled = ($job->{enabled}) ? '' : '#';
374 my $dh = parse_dow($job->{dow});
375 my $dow;
376 if ($dh->{mon} && $dh->{tue} && $dh->{wed} && $dh->{thu} &&
377 $dh->{fri} && $dh->{sat} && $dh->{sun}) {
378 $dow = '*';
379 } else {
380 $dow = &$dowhash_to_dow($dh, 1);
381 $dow = '*' if !$dow;
382 }
383
384 my ($hour, $minute);
385
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;
391 } else {
392 die "unable to parse job start time\n";
393 }
394
395 $job->{quiet} = 1; # we do not want messages from cron
396
397 my $cmd = command_line($job);
398
399 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
400 }
401
402 my $ejobs = $cfg->{ejobs} || [];
403 foreach my $job (@$ejobs) {
404 $out .= "$job->{line}\n" if $job->{line};
405 }
406
407 return $out;
408 }
409
410 sub command_line {
411 my ($param) = @_;
412
413 my $cmd = "vzdump";
414
415 if ($param->{vmid}) {
416 $cmd .= " " . join(' ', PVE::Tools::split_list($param->{vmid}));
417 }
418
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);
427 }
428 } else {
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};
432
433 $cmd .= " --$p " . PVE::Tools::shellquote($v) if defined($v) && $v ne '';
434 }
435 }
436
437 return $cmd;
438 }
439
440 # bash completion helpers
441 sub complete_backup_storage {
442
443 my $cfg = PVE::Storage::config();
444 my $ids = $cfg->{ids};
445
446 my $nodename = PVE::INotify::nodename();
447
448 my $res = [];
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};
453 push @$res, $sid;
454 }
455
456 return $res;
457 }
458
459 1;