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