]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Backup.pm
47a7426d43b96558c734cf35427ea6b311814a5e
1 package PVE
::API2
::Backup
;
7 use PVE
::Tools
qw(extract_param);
8 use PVE
::Cluster
qw(cfs_register_file cfs_lock_file cfs_read_file cfs_write_file);
10 use PVE
::RPCEnvironment
;
13 use PVE
::Exception
qw(raise_param_exc);
16 use base
qw(PVE::RESTHandler);
18 cfs_register_file
('vzdump',
19 \
&parse_vzdump_cron_config
,
20 \
&write_vzdump_cron_config
);
22 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
23 sub verify_day_of_week
{
24 my ($value, $noerr) = @_;
26 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
28 return undef if $noerr;
30 die "invalid day '$value'\n";
34 my $dowhash_to_dow = sub {
38 push @da, $num ?
1 : 'mon' if $d->{mon
};
39 push @da, $num ?
2 : 'tue' if $d->{tue
};
40 push @da, $num ?
3 : 'wed' if $d->{wed
};
41 push @da, $num ?
4 : 'thu' if $d->{thu
};
42 push @da, $num ?
5 : 'fri' if $d->{fri
};
43 push @da, $num ?
6 : 'sat' if $d->{sat
};
44 push @da, $num ?
7 : 'sun' if $d->{sun
};
49 # parse crontab style day of week
51 my ($dowstr, $noerr) = @_;
53 my $dowmap = {mon
=> 1, tue
=> 2, wed
=> 3, thu
=> 4,
54 fri
=> 5, sat
=> 6, sun
=> 7};
55 my $rdowmap = { '1' => 'mon', '2' => 'tue', '3' => 'wed', '4' => 'thu',
56 '5' => 'fri', '6' => 'sat', '7' => 'sun', '0' => 'sun'};
60 $dowstr = '1,2,3,4,5,6,7' if $dowstr eq '*';
62 foreach my $day (PVE
::Tools
::split_list
($dowstr)) {
63 if ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun)-(mon|tue|wed|thu|fri|sat|sun)$/i) {
64 for (my $i = $dowmap->{lc($1)}; $i <= $dowmap->{lc($2)}; $i++) {
65 my $r = $rdowmap->{$i};
68 } elsif ($day =~ m/^(mon|tue|wed|thu|fri|sat|sun|[0-7])$/i) {
69 $day = $rdowmap->{$day} if $day =~ m/\d/;
72 return undef if $noerr;
73 die "unable to parse day of week '$dowstr'\n";
80 my $vzdump_propetries = {
81 additionalProperties
=> 0,
82 properties
=> PVE
::VZDump
::json_config_properties
({}),
85 sub parse_vzdump_cron_config
{
86 my ($filename, $raw) = @_;
88 my $jobs = []; # correct jobs
90 my $ejobs = []; # mailfomerd lines
92 my $jid = 1; # we start at 1
94 my $digest = Digest
::SHA1
::sha1_hex
(defined($raw) ?
$raw : '');
96 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
99 next if $line =~ m/^\#/;
100 next if $line =~ m/^\s*$/;
101 next if $line =~ m/^PATH\s*=/; # we always overwrite path
103 if ($line =~ m
|^(\d
+)\s
+(\d
+)\s
+\
*\s
+\
*\s
+(\S
+)\s
+root\s
+(/\S+/)?vzdump
(\s
+(.*))?
$|) {
105 my $minute = int($1);
110 my $dowhash = parse_dow
($dow, 1);
111 die "unable to parse day of week '$dow' in '$filename'\n" if !$dowhash;
113 my $args = PVE
::Tools
::split_args
($param);
114 my $opts = PVE
::JSONSchema
::get_options
($vzdump_propetries, $args, undef, undef, 'vmid');
116 $opts->{id
} = "$digest:$jid";
118 $opts->{starttime
} = sprintf "%02d:%02d", $hour, $minute;
119 $opts->{dow
} = &$dowhash_to_dow($dowhash);
125 syslog
('err', "parse error in '$filename': $err");
126 push @$ejobs, { line
=> $line };
128 } elsif ($line =~ m
|^\S
+\s
+(\S
+)\s
+\S
+\s
+\S
+\s
+\S
+\s
+\S
+\s
+(\S
.*)$|) {
129 syslog
('err', "warning: malformed line in '$filename'");
130 push @$ejobs, { line
=> $line };
132 syslog
('err', "ignoring malformed line in '$filename'");
137 $res->{digest
} = $digest;
138 $res->{jobs
} = $jobs;
139 $res->{ejobs
} = $ejobs;
144 sub write_vzdump_cron_config
{
145 my ($filename, $cfg) = @_;
147 my $out = "# cluster wide vzdump cron schedule\n";
148 $out .= "# Atomatically generated file - do not edit\n\n";
149 $out .= "PATH=\"/usr/sbin:/usr/bin:/sbin:/bin\"\n\n";
151 my $jobs = $cfg->{jobs
} || [];
152 foreach my $job (@$jobs) {
153 my $dh = parse_dow
($job->{dow
});
155 if ($dh->{mon
} && $dh->{tue
} && $dh->{wed
} && $dh->{thu
} &&
156 $dh->{fri
} && $dh->{sat
} && $dh->{sun
}) {
159 $dow = &$dowhash_to_dow($dh, 1);
165 die "no job start time specified\n" if !$job->{starttime
};
166 if ($job->{starttime
} =~ m/^(\d{1,2}):(\d{1,2})$/) {
167 ($hour, $minute) = (int($1), int($2));
168 die "hour '$hour' out of range\n" if $hour < 0 || $hour > 23;
169 die "minute '$minute' out of range\n" if $minute < 0 || $minute > 59;
171 die "unable to parse job start time\n";
175 foreach my $p (keys %$job) {
176 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' || $p eq 'dow';
178 $param .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
181 $param .= " $job->{vmid}" if $job->{vmid
};
183 $out .= sprintf "$minute $hour * * %-11s root vzdump$param\n", $dow;
186 my $ejobs = $cfg->{ejobs
} || [];
187 foreach my $job (@$ejobs) {
188 $out .= "$job->{line}\n" if $job->{line
};
194 __PACKAGE__-
>register_method({
198 description
=> "List vzdump backup schedule.",
200 additionalProperties
=> 0,
208 id
=> { type
=> 'string' },
211 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
216 my $rpcenv = PVE
::RPCEnvironment
::get
();
217 my $user = $rpcenv->get_user();
219 my $data = cfs_read_file
('vzdump');
221 my $res = $data->{jobs
} || [];
226 __PACKAGE__-
>register_method({
227 name
=> 'create_job',
231 description
=> "Create new vzdump backup job.",
233 additionalProperties
=> 0,
234 properties
=> PVE
::VZDump
::json_config_properties
({
237 description
=> "Job Start time.",
238 pattern
=> '\d{1,2}:\d{1,2}',
242 type
=> 'string', format
=> 'pve-day-of-week-list',
244 description
=> "Day of week selection.",
245 default => 'mon,tue,wed,thu,fri,sat,sun',
249 returns
=> { type
=> 'null' },
253 my $rpcenv = PVE
::RPCEnvironment
::get
();
254 my $user = $rpcenv->get_user();
256 my $data = cfs_read_file
('vzdump');
258 $param->{minute
} = 0 if !defined($param->{minute
});
259 $param->{dow
} = 'mon,tue,wed,thu,fri,sat,sun' if !defined($param->{dow
});
261 $param->{all
} = 1 if defined($param->{exclude
});
262 raise_param_exc
({ all
=> "option conflicts with option 'vmid'"})
263 if $param->{all
} && $param->{vmid
};
265 raise_param_exc
({ vmid
=> "property is missing"})
266 if !$param->{all
} && !$param->{vmid
};
268 push @{$data->{jobs
}}, $param;
270 cfs_write_file
('vzdump', $data);
275 __PACKAGE__-
>register_method({
279 description
=> "Read vzdump backup job definition.",
281 additionalProperties
=> 0,
285 description
=> "The job ID.",
296 my $rpcenv = PVE
::RPCEnvironment
::get
();
297 my $user = $rpcenv->get_user();
299 my $data = cfs_read_file
('vzdump');
301 my $jobs = $data->{jobs
} || [];
303 foreach my $job (@$jobs) {
304 return $job if $job->{id
} eq $param->{id
};
307 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
311 __PACKAGE__-
>register_method({
312 name
=> 'delete_job',
315 description
=> "Delete vzdump backup job definition.",
318 additionalProperties
=> 0,
322 description
=> "The job ID.",
327 returns
=> { type
=> 'null' },
331 my $rpcenv = PVE
::RPCEnvironment
::get
();
332 my $user = $rpcenv->get_user();
334 my $data = cfs_read_file
('vzdump');
336 my $jobs = $data->{jobs
} || [];
340 foreach my $job (@$jobs) {
341 if ($job->{id
} eq $param->{id
}) {
344 push @$newjobs, $job;
348 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$found;
350 $data->{jobs
} = $newjobs;
352 cfs_write_file
('vzdump', $data);
357 __PACKAGE__-
>register_method({
358 name
=> 'update_job',
362 description
=> "Update vzdump backup job definition.",
364 additionalProperties
=> 0,
365 properties
=> PVE
::VZDump
::json_config_properties
({
368 description
=> "The job ID.",
373 description
=> "Job Start time.",
374 pattern
=> '\d{1,2}:\d{1,2}',
378 type
=> 'string', format
=> 'pve-day-of-week-list',
380 description
=> "Day of week selection.",
384 returns
=> { type
=> 'null' },
388 my $rpcenv = PVE
::RPCEnvironment
::get
();
389 my $user = $rpcenv->get_user();
391 my $data = cfs_read_file
('vzdump');
393 my $jobs = $data->{jobs
} || [];
395 raise_param_exc
({ all
=> "option conflicts with option 'vmid'"})
396 if $param->{all
} && $param->{vmid
};
398 foreach my $job (@$jobs) {
399 if ($job->{id
} eq $param->{id
}) {
401 foreach my $k (keys %$param) {
402 $job->{$k} = $param->{$k};
405 $job->{all
} = 1 if defined($job->{exclude
});
407 if ($param->{vmid
}) {
409 delete $job->{exclude
};
410 } elsif ($param->{all
}) {
414 raise_param_exc
({ all
=> "option conflicts with option 'vmid'"})
415 if $job->{all
} && $job->{vmid
};
417 raise_param_exc
({ vmid
=> "property is missing"})
418 if !$job->{all
} && !$job->{vmid
};
420 cfs_write_file
('vzdump', $data);
426 raise_param_exc
({ id
=> "No such job '$param->{id}'" });