1 package PVE
::API2
::Backup
;
9 use PVE
::Tools
qw(extract_param);
10 use PVE
::Cluster
qw(cfs_lock_file cfs_read_file cfs_write_file);
12 use PVE
::RPCEnvironment
;
15 use PVE
::Exception
qw(raise_param_exc);
17 use PVE
::VZDump
::Common
;
18 use PVE
::VZDump
::JobBase
;
19 use PVE
::Jobs
; # for VZDump Jobs
20 use Proxmox
::RS
::CalendarEvent
;
22 use base
qw(PVE::RESTHandler);
24 use constant ALL_DAYS
=> 'mon,tue,wed,thu,fri,sat,sun';
26 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
27 sub verify_day_of_week
{
28 my ($value, $noerr) = @_;
30 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
32 return undef if $noerr;
34 die "invalid day '$value'\n";
37 my $vzdump_job_id_prop = {
39 description
=> "The job ID.",
43 # NOTE: also used by the vzdump API call.
44 sub assert_param_permission_common
{
45 my ($rpcenv, $user, $param) = @_;
46 return if $user eq 'root@pam'; # always OK
48 for my $key (qw(tmpdir dumpdir script)) {
49 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
52 if (defined($param->{bwlimit
}) || defined($param->{ionice
}) || defined($param->{performance
})) {
53 $rpcenv->check($user, "/", [ 'Sys.Modify' ]);
57 my $convert_to_schedule = sub {
60 my $starttime = $job->{starttime
};
62 return "$starttime" if !$job->{dow
}; # dow is restrictive, so none means all days
64 # normalize as it could be a null-separated list previously
65 my $dow = join(',', PVE
::Tools
::split_list
($job->{dow
}));
67 return $dow eq ALL_DAYS ?
"$starttime" : "$dow $starttime";
70 my $schedule_param_check = sub {
71 my ($param, $required) = @_;
72 if (defined($param->{schedule
})) {
73 if (defined($param->{starttime
})) {
74 raise_param_exc
({ starttime
=> "'starttime' and 'schedule' cannot both be set" });
76 } elsif (!defined($param->{starttime
})) {
77 raise_param_exc
({ schedule
=> "neither 'starttime' nor 'schedule' were set" })
80 $param->{schedule
} = $convert_to_schedule->($param);
83 delete $param->{starttime
};
87 __PACKAGE__-
>register_method({
91 description
=> "List vzdump backup schedule.",
93 check
=> ['perm', '/', ['Sys.Audit']],
96 additionalProperties
=> 0,
104 id
=> $vzdump_job_id_prop
107 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
112 my $rpcenv = PVE
::RPCEnvironment
::get
();
113 my $user = $rpcenv->get_user();
115 my $data = cfs_read_file
('vzdump.cron');
116 my $jobs_data = cfs_read_file
('jobs.cfg');
117 my $order = $jobs_data->{order
};
118 my $jobs = $jobs_data->{ids
};
120 my $res = $data->{jobs
} || [];
121 foreach my $job (@$res) {
122 $job->{schedule
} = $convert_to_schedule->($job);
125 foreach my $jobid (sort { $order->{$a} <=> $order->{$b} } keys %$jobs) {
126 my $job = $jobs->{$jobid};
127 next if $job->{type
} ne 'vzdump';
129 if (my $schedule = $job->{schedule
}) {
130 # vzdump jobs are cluster wide, there maybe was no local run
131 # so simply calculate from now
132 my $last_run = time();
133 my $calspec = Proxmox
::RS
::CalendarEvent-
>new($schedule);
134 my $next_run = $calspec->compute_next_event($last_run);
135 $job->{'next-run'} = $next_run if defined($next_run);
138 # FIXME remove in PVE 8.0?
139 # backwards compat: before moving the job registry to pve-common, id was auto-injected
148 __PACKAGE__-
>register_method({
149 name
=> 'create_job',
153 description
=> "Create new vzdump backup job.",
155 check
=> ['perm', '/', ['Sys.Modify']],
156 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
159 additionalProperties
=> 0,
160 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
163 description
=> "Job ID (will be autogenerated).",
164 format
=> 'pve-configid',
165 optional
=> 1, # FIXME: make required on 8.0
168 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
169 type
=> 'string', format
=> 'pve-calendar-event',
175 description
=> "Job Start time.",
176 pattern
=> '\d{1,2}:\d{1,2}',
181 type
=> 'string', format
=> 'pve-day-of-week-list',
183 description
=> "Day of week selection.",
184 requires
=> 'starttime',
190 description
=> "Enable or disable the job.",
196 description
=> "If true, the job will be run as soon as possible if it was missed".
197 " while the scheduler was not running.",
203 description
=> "Description for the Job.",
208 returns
=> { type
=> 'null' },
212 my $rpcenv = PVE
::RPCEnvironment
::get
();
213 my $user = $rpcenv->get_user();
215 assert_param_permission_common
($rpcenv, $user, $param);
217 if (my $pool = $param->{pool
}) {
218 $rpcenv->check_pool_exist($pool);
219 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
222 $schedule_param_check->($param, 1);
224 $param->{enabled
} = 1 if !defined($param->{enabled
});
226 # autogenerate id for api compatibility FIXME remove with 8.0
227 my $id = extract_param
($param, 'id') // UUID
::uuid
();
229 cfs_lock_file
('jobs.cfg', undef, sub {
230 my $data = cfs_read_file
('jobs.cfg');
232 die "Job '$id' already exists\n"
233 if $data->{ids
}->{$id};
235 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
236 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 1, 1);
238 $data->{ids
}->{$id} = $opts;
240 PVE
::Jobs
::create_job
($id, 'vzdump', $opts);
242 cfs_write_file
('jobs.cfg', $data);
249 __PACKAGE__-
>register_method({
253 description
=> "Read vzdump backup job definition.",
255 check
=> ['perm', '/', ['Sys.Audit']],
258 additionalProperties
=> 0,
260 id
=> $vzdump_job_id_prop
269 my $rpcenv = PVE
::RPCEnvironment
::get
();
270 my $user = $rpcenv->get_user();
272 my $data = cfs_read_file
('vzdump.cron');
274 my $jobs = $data->{jobs
} || [];
276 foreach my $job (@$jobs) {
277 if ($job->{id
} eq $param->{id
}) {
278 $job->{schedule
} = $convert_to_schedule->($job);
283 my $jobs_data = cfs_read_file
('jobs.cfg');
284 my $job = $jobs_data->{ids
}->{$param->{id
}};
285 if ($job && $job->{type
} eq 'vzdump') {
286 # FIXME remove in PVE 8.0?
287 # backwards compat: before moving the job registry to pve-common, id was auto-injected
288 $job->{id
} = $param->{id
};
292 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
296 __PACKAGE__-
>register_method({
297 name
=> 'delete_job',
300 description
=> "Delete vzdump backup job definition.",
302 check
=> ['perm', '/', ['Sys.Modify']],
306 additionalProperties
=> 0,
308 id
=> $vzdump_job_id_prop
311 returns
=> { type
=> 'null' },
315 my $rpcenv = PVE
::RPCEnvironment
::get
();
316 my $user = $rpcenv->get_user();
318 my $id = $param->{id
};
320 my $delete_job = sub {
321 my $data = cfs_read_file
('vzdump.cron');
323 my $jobs = $data->{jobs
} || [];
327 foreach my $job (@$jobs) {
328 if ($job->{id
} eq $id) {
331 push @$newjobs, $job;
336 cfs_lock_file
('jobs.cfg', undef, sub {
337 my $jobs_data = cfs_read_file
('jobs.cfg');
339 if (!defined($jobs_data->{ids
}->{$id})) {
340 raise_param_exc
({ id
=> "No such job '$id'" });
342 delete $jobs_data->{ids
}->{$id};
344 PVE
::Jobs
::remove_job
($id, 'vzdump');
346 cfs_write_file
('jobs.cfg', $jobs_data);
350 $data->{jobs
} = $newjobs;
352 cfs_write_file
('vzdump.cron', $data);
355 cfs_lock_file
('vzdump.cron', undef, $delete_job);
361 __PACKAGE__-
>register_method({
362 name
=> 'update_job',
366 description
=> "Update vzdump backup job definition.",
368 check
=> ['perm', '/', ['Sys.Modify']],
369 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
372 additionalProperties
=> 0,
373 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
374 id
=> $vzdump_job_id_prop,
376 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
377 type
=> 'string', format
=> 'pve-calendar-event',
383 description
=> "Job Start time.",
384 pattern
=> '\d{1,2}:\d{1,2}',
389 type
=> 'string', format
=> 'pve-day-of-week-list',
391 requires
=> 'starttime',
392 description
=> "Day of week selection.",
395 type
=> 'string', format
=> 'pve-configid-list',
396 description
=> "A list of settings you want to delete.",
402 description
=> "Enable or disable the job.",
408 description
=> "If true, the job will be run as soon as possible if it was missed".
409 " while the scheduler was not running.",
415 description
=> "Description for the Job.",
420 returns
=> { type
=> 'null' },
424 my $rpcenv = PVE
::RPCEnvironment
::get
();
425 my $user = $rpcenv->get_user();
427 assert_param_permission_common
($rpcenv, $user, $param);
429 if (my $pool = $param->{pool
}) {
430 $rpcenv->check_pool_exist($pool);
431 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
434 $schedule_param_check->($param);
436 my $id = extract_param
($param, 'id');
437 my $delete = extract_param
($param, 'delete');
438 $delete = { map { $_ => 1 } PVE
::Tools
::split_list
($delete) } if $delete;
440 my $update_job = sub {
441 my $data = cfs_read_file
('vzdump.cron');
442 my $jobs_data = cfs_read_file
('jobs.cfg');
444 my $jobs = $data->{jobs
} || [];
446 die "no options specified\n" if !scalar(keys $param->%*) && !scalar(keys $delete->%*);
448 PVE
::VZDump
::verify_vzdump_parameters
($param);
449 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 0, 1);
451 # try to find it in old vzdump.cron and convert it to a job
452 my ($idx) = grep { $jobs->[$_]->{id
} eq $id } (0 .. scalar(@$jobs) - 1);
456 $job = splice @$jobs, $idx, 1;
457 $job->{schedule
} = $convert_to_schedule->($job);
458 delete $job->{starttime
};
461 $job->{type
} = 'vzdump';
462 $jobs_data->{ids
}->{$id} = $job;
464 $job = $jobs_data->{ids
}->{$id};
465 die "no such vzdump job\n" if !$job || $job->{type
} ne 'vzdump';
470 'repeat-missed' => 1,
473 for my $k (keys $delete->%*) {
474 if (!PVE
::VZDump
::option_exists
($k) && !$deletable->{$k}) {
475 raise_param_exc
({ delete => "unknown option '$k'" });
481 foreach my $k (keys %$param) {
482 $job->{$k} = $param->{$k};
485 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
487 if (defined($param->{vmid
})) {
489 delete $job->{exclude
};
491 } elsif ($param->{all
}) {
494 } elsif ($job->{pool
}) {
497 delete $job->{exclude
};
500 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
503 cfs_write_file
('vzdump.cron', $data);
505 cfs_write_file
('jobs.cfg', $jobs_data);
507 PVE
::Jobs
::detect_changed_runtime_props
($id, 'vzdump', $job);
511 cfs_lock_file
('vzdump.cron', undef, sub {
512 cfs_lock_file
('jobs.cfg', undef, $update_job);
518 __PACKAGE__-
>register_method({
519 name
=> 'get_volume_backup_included',
520 path
=> '{id}/included_volumes',
523 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
525 check
=> ['perm', '/', ['Sys.Audit']],
528 additionalProperties
=> 0,
530 id
=> $vzdump_job_id_prop
535 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
544 description
=> 'VMID of the guest.',
548 description
=> 'Name of the guest',
553 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
554 enum
=> ['qemu', 'lxc', 'unknown'],
559 description
=> 'The volumes of the guest with the information if they will be included in backups.',
565 description
=> 'Configuration key of the volume.',
569 description
=> 'Name of the volume.',
573 description
=> 'Whether the volume is included in the backup or not.',
577 description
=> 'The reason why the volume is included (or excluded).',
590 my $rpcenv = PVE
::RPCEnvironment
::get
();
592 my $user = $rpcenv->get_user();
594 my $vzconf = cfs_read_file
('vzdump.cron');
595 my $all_jobs = $vzconf->{jobs
} || [];
597 my $rrd = PVE
::Cluster
::rrd_dump
();
599 for my $j (@$all_jobs) {
600 if ($j->{id
} eq $param->{id
}) {
606 my $jobs_data = cfs_read_file
('jobs.cfg');
607 my $j = $jobs_data->{ids
}->{$param->{id
}};
608 if ($j && $j->{type
} eq 'vzdump') {
612 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
614 my $vmlist = PVE
::Cluster
::get_vmlist
();
618 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
620 for my $node (keys %{$included_guests}) {
621 my $node_vmids = $included_guests->{$node};
622 push(@job_vmids, @{$node_vmids});
625 # remove VMIDs to which the user has no permission to not leak infos
626 # like the guest name
627 my @allowed_vmids = grep {
628 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
635 for my $vmid (@allowed_vmids) {
639 # It's possible that a job has VMIDs configured that are not in
640 # vmlist. This could be because a guest was removed but not purged.
641 # Since there is no more data available we can only deliver the VMID
643 if (!defined $vmlist->{ids
}->{$vmid}) {
644 push(@{$result->{children
}}, {
652 my $type = $vmlist->{ids
}->{$vmid}->{type
};
653 my $node = $vmlist->{ids
}->{$vmid}->{node
};
659 if ($type eq 'qemu') {
660 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
661 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
662 $name = $conf->{name
};
663 } elsif ($type eq 'lxc') {
664 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
665 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
666 $name = $conf->{hostname
};
668 die "VMID $vmid is neither Qemu nor LXC guest\n";
671 foreach my $volume (@$volumes) {
673 # id field must be unique for ExtJS tree view
674 id
=> "$vmid:$volume->{key}",
675 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
676 included
=> $volume->{included
},
677 reason
=> $volume->{reason
},
680 push(@{$children}, $disk);
684 # it's possible for a guest to have no volumes configured
685 $leaf = 1 if !@{$children};
687 push(@{$result->{children
}}, {
691 children
=> $children,