]>
git.proxmox.com Git - pve-guest-common.git/blob - PVE/VZDump/Common.pm
4789a50dfc2cd89d961aaa9edc3915a939e6e926
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 # parse crontab style day of week
34 my ($dowstr, $noerr) = @_;
36 my $dowmap = {mon
=> 1, tue
=> 2, wed
=> 3, thu
=> 4,
37 fri
=> 5, sat
=> 6, sun
=> 7};
38 my $rdowmap = { '1' => 'mon', '2' => 'tue', '3' => 'wed', '4' => 'thu',
39 '5' => 'fri', '6' => 'sat', '7' => 'sun', '0' => 'sun'};
43 $dowstr = '1,2,3,4,5,6,7' if $dowstr eq '*';
45 foreach my $day (PVE
::Tools
::split_list
($dowstr)) {
46 if ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun)-(mon|tue|wed|thu|fri|sat|sun)$/i) {
47 for (my $i = $dowmap->{lc($1)}; $i <= $dowmap->{lc($2)}; $i++) {
48 my $r = $rdowmap->{$i};
51 } elsif ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun|[0-7])$/i) {
52 $day = $rdowmap->{$day} if $day =~ m/\d/;
55 return undef if $noerr;
56 die "unable to parse day of week '$dowstr'\n";
65 type
=> 'string', format
=> 'pve-vmid-list',
66 description
=> "The ID of the guest system you want to backup.",
67 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
70 node
=> get_standard_option
('pve-node', {
71 description
=> "Only run if executed on this node.",
72 completion
=> \
&PVE
::Cluster
::get_nodelist
,
77 description
=> "Backup all known guest systems on this host.",
83 description
=> "Exclude temporary files and logs.",
89 description
=> "Compress dump file.",
91 enum
=> ['0', '1', 'gzip', 'lzo'],
96 description
=> "Use pigz instead of gzip when N>0.".
97 " N=1 uses half of cores, N>1 uses N as thread count.",
103 description
=> "Be quiet.",
109 description
=> "Backup mode.",
111 default => 'snapshot',
112 enum
=> [ 'snapshot', 'suspend', 'stop' ],
115 type
=> 'string', format
=> 'pve-vmid-list',
116 description
=> "Exclude specified guest systems (assumes --all)",
120 type
=> 'string', format
=> 'string-alist',
121 description
=> "Exclude certain files/directories (shell globs).",
125 type
=> 'string', format
=> 'string-list',
126 description
=> "Comma-separated list of email addresses that should" .
127 " receive email notifications.",
130 mailnotification
=> {
132 description
=> "Specify when to send an email",
134 enum
=> [ 'always', 'failure' ],
139 description
=> "Store temporary files to specified directory.",
144 description
=> "Store resulting files to specified directory.",
149 description
=> "Use specified hook script.",
152 storage
=> get_standard_option
('pve-storage-id', {
153 description
=> "Store resulting file to this storage.",
154 completion
=> \
&complete_backup_storage
,
159 description
=> "Stop running backup jobs on this host.",
165 description
=> "Unused, will be removed in a future release.",
172 description
=> "Limit I/O bandwidth (KBytes per second).",
179 description
=> "Set CFQ ionice priority.",
187 description
=> "Maximal time to wait for the global lock (minutes).",
190 default => 3*60, # 3 hours
194 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
197 default => 10, # 10 minutes
201 description
=> "Maximal number of backup files per guest system.",
208 description
=> "Remove old backup files if there are more than 'maxfiles' backup files.",
214 description
=> 'Backup all known guest systems included in the specified pool.',
223 # add JSON properties for create and set function
224 sub json_config_properties
{
227 foreach my $opt (keys %$confdesc) {
228 $prop->{$opt} = $confdesc->{$opt};
234 my $vzdump_properties = {
235 additionalProperties
=> 0,
236 properties
=> json_config_properties
({}),
239 sub parse_vzdump_cron_config
{
240 my ($filename, $raw) = @_;
242 my $jobs = []; # correct jobs
244 my $ejobs = []; # mailfomerd lines
246 my $jid = 1; # we start at 1
248 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
250 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
253 next if $line =~ m/^\#/;
254 next if $line =~ m/^\s*$/;
255 next if $line =~ m/^PATH\s*=/; # we always overwrite path
257 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?
(#)?vzdump(\s+(.*))?$|) {
259 my $minute = int($1);
265 my $dowhash = parse_dow
($dow, 1);
266 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
268 my $args = PVE
::Tools
::split_args
($param);
269 my $opts = PVE
::JSONSchema
::get_options
($vzdump_properties, $args, 'vmid');
271 $opts->{enabled
} = !defined($enabled);
272 $opts->{id
} = "$digest:$jid";
274 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
275 $opts->{dow
} = &$dowhash_to_dow($dowhash);
281 syslog
('err', "parse error in '$filename': $err");
282 push @$ejobs, { line
=> $line };
284 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
285 syslog
('err', "warning: malformed line in '$filename'");
286 push @$ejobs, { line
=> $line };
288 syslog
('err', "ignoring malformed line in '$filename'");
293 $res->{digest
} = $digest;
294 $res->{jobs
} = $jobs;
295 $res->{ejobs
} = $ejobs;
300 sub write_vzdump_cron_config
{
301 my ($filename, $cfg) = @_;
303 my $out = "# cluster wide vzdump cron schedule\n";
304 $out .= "# Automatically generated file - do not edit\n\n";
305 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
307 my $jobs = $cfg->{jobs
} || [];
308 foreach my $job (@$jobs) {
309 my $enabled = ($job->{enabled
}) ?
'' : '#';
310 my $dh = parse_dow
($job->{dow
});
312 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
313 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
316 $dow = &$dowhash_to_dow($dh, 1);
322 die "no job start time specified\n" if !$job->{starttime
};
323 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
324 ($hour, $minute) = (int($1), int($2));
325 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
326 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
328 die "unable to parse job start time\n";
331 $job->{quiet
} = 1; # we do not want messages from cron
333 my $cmd = command_line
($job);
335 $out .= sprintf "$minute $hour * * %-11s root $enabled$cmd\n", $dow;
338 my $ejobs = $cfg->{ejobs
} || [];
339 foreach my $job (@$ejobs) {
340 $out .= "$job->{line}\n" if $job->{line
};
351 if ($param->{vmid
}) {
352 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
355 foreach my $p (keys %$param) {
356 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
357 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
358 my $v = $param->{$p};
359 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
360 if ($p eq 'exclude-path') {
361 foreach my $path (split(/\0/, $v || '')) {
362 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
365 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
372 # bash completion helpers
373 sub complete_backup_storage
{
375 my $cfg = PVE
::Storage
::config
();
376 my $ids = $cfg->{ids
};
378 my $nodename = PVE
::INotify
::nodename
();
381 foreach my $sid (keys %$ids) {
382 my $scfg = $ids->{$sid};
383 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
384 next if !$scfg->{content
}->{backup
};