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 sub assert_param_permission_update
{
58 my ($rpcenv, $user, $update, $delete) = @_;
59 return if $user eq 'root@pam'; # always OK
61 assert_param_permission_common
($rpcenv, $user, $update);
62 assert_param_permission_common
($rpcenv, $user, $delete);
65 my $convert_to_schedule = sub {
68 my $starttime = $job->{starttime
};
70 return "$starttime" if !$job->{dow
}; # dow is restrictive, so none means all days
72 # normalize as it could be a null-separated list previously
73 my $dow = join(',', PVE
::Tools
::split_list
($job->{dow
}));
75 return $dow eq ALL_DAYS ?
"$starttime" : "$dow $starttime";
78 my $schedule_param_check = sub {
79 my ($param, $required) = @_;
80 if (defined($param->{schedule
})) {
81 if (defined($param->{starttime
})) {
82 raise_param_exc
({ starttime
=> "'starttime' and 'schedule' cannot both be set" });
84 } elsif (!defined($param->{starttime
})) {
85 raise_param_exc
({ schedule
=> "neither 'starttime' nor 'schedule' were set" })
88 $param->{schedule
} = $convert_to_schedule->($param);
91 delete $param->{starttime
};
95 __PACKAGE__-
>register_method({
99 description
=> "List vzdump backup schedule.",
101 check
=> ['perm', '/', ['Sys.Audit']],
104 additionalProperties
=> 0,
112 id
=> $vzdump_job_id_prop
115 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
120 my $rpcenv = PVE
::RPCEnvironment
::get
();
121 my $user = $rpcenv->get_user();
123 my $data = cfs_read_file
('vzdump.cron');
124 my $jobs_data = cfs_read_file
('jobs.cfg');
125 my $order = $jobs_data->{order
};
126 my $jobs = $jobs_data->{ids
};
128 my $res = $data->{jobs
} || [];
129 foreach my $job (@$res) {
130 $job->{schedule
} = $convert_to_schedule->($job);
133 foreach my $jobid (sort { $order->{$a} <=> $order->{$b} } keys %$jobs) {
134 my $job = $jobs->{$jobid};
135 next if $job->{type
} ne 'vzdump';
137 if (my $schedule = $job->{schedule
}) {
138 # vzdump jobs are cluster wide, there maybe was no local run
139 # so simply calculate from now
140 my $last_run = time();
141 my $calspec = Proxmox
::RS
::CalendarEvent-
>new($schedule);
142 my $next_run = $calspec->compute_next_event($last_run);
143 $job->{'next-run'} = $next_run if defined($next_run);
146 # FIXME remove in PVE 8.0?
147 # backwards compat: before moving the job registry to pve-common, id was auto-injected
156 __PACKAGE__-
>register_method({
157 name
=> 'create_job',
161 description
=> "Create new vzdump backup job.",
163 check
=> ['perm', '/', ['Sys.Modify']],
164 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
167 additionalProperties
=> 0,
168 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
171 description
=> "Job ID (will be autogenerated).",
172 format
=> 'pve-configid',
173 optional
=> 1, # FIXME: make required on 8.0
176 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
177 type
=> 'string', format
=> 'pve-calendar-event',
183 description
=> "Job Start time.",
184 pattern
=> '\d{1,2}:\d{1,2}',
189 type
=> 'string', format
=> 'pve-day-of-week-list',
191 description
=> "Day of week selection.",
192 requires
=> 'starttime',
198 description
=> "Enable or disable the job.",
204 description
=> "If true, the job will be run as soon as possible if it was missed".
205 " while the scheduler was not running.",
211 description
=> "Description for the Job.",
216 returns
=> { type
=> 'null' },
220 my $rpcenv = PVE
::RPCEnvironment
::get
();
221 my $user = $rpcenv->get_user();
223 assert_param_permission_common
($rpcenv, $user, $param);
225 if (my $pool = $param->{pool
}) {
226 $rpcenv->check_pool_exist($pool);
227 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
230 $schedule_param_check->($param, 1);
232 $param->{enabled
} = 1 if !defined($param->{enabled
});
234 # autogenerate id for api compatibility FIXME remove with 8.0
235 my $id = extract_param
($param, 'id') // UUID
::uuid
();
237 cfs_lock_file
('jobs.cfg', undef, sub {
238 my $data = cfs_read_file
('jobs.cfg');
240 die "Job '$id' already exists\n"
241 if $data->{ids
}->{$id};
243 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
244 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 1, 1);
246 $data->{ids
}->{$id} = $opts;
248 PVE
::Jobs
::create_job
($id, 'vzdump', $opts);
250 cfs_write_file
('jobs.cfg', $data);
257 __PACKAGE__-
>register_method({
261 description
=> "Read vzdump backup job definition.",
263 check
=> ['perm', '/', ['Sys.Audit']],
266 additionalProperties
=> 0,
268 id
=> $vzdump_job_id_prop
277 my $rpcenv = PVE
::RPCEnvironment
::get
();
278 my $user = $rpcenv->get_user();
280 my $data = cfs_read_file
('vzdump.cron');
282 my $jobs = $data->{jobs
} || [];
284 foreach my $job (@$jobs) {
285 if ($job->{id
} eq $param->{id
}) {
286 $job->{schedule
} = $convert_to_schedule->($job);
291 my $jobs_data = cfs_read_file
('jobs.cfg');
292 my $job = $jobs_data->{ids
}->{$param->{id
}};
293 if ($job && $job->{type
} eq 'vzdump') {
294 # FIXME remove in PVE 8.0?
295 # backwards compat: before moving the job registry to pve-common, id was auto-injected
296 $job->{id
} = $param->{id
};
300 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
304 __PACKAGE__-
>register_method({
305 name
=> 'delete_job',
308 description
=> "Delete vzdump backup job definition.",
310 check
=> ['perm', '/', ['Sys.Modify']],
314 additionalProperties
=> 0,
316 id
=> $vzdump_job_id_prop
319 returns
=> { type
=> 'null' },
323 my $rpcenv = PVE
::RPCEnvironment
::get
();
324 my $user = $rpcenv->get_user();
326 my $id = $param->{id
};
328 my $delete_job = sub {
329 my $data = cfs_read_file
('vzdump.cron');
331 my $jobs = $data->{jobs
} || [];
335 foreach my $job (@$jobs) {
336 if ($job->{id
} eq $id) {
339 push @$newjobs, $job;
344 cfs_lock_file
('jobs.cfg', undef, sub {
345 my $jobs_data = cfs_read_file
('jobs.cfg');
347 if (!defined($jobs_data->{ids
}->{$id})) {
348 raise_param_exc
({ id
=> "No such job '$id'" });
350 delete $jobs_data->{ids
}->{$id};
352 PVE
::Jobs
::remove_job
($id, 'vzdump');
354 cfs_write_file
('jobs.cfg', $jobs_data);
358 $data->{jobs
} = $newjobs;
360 cfs_write_file
('vzdump.cron', $data);
363 cfs_lock_file
('vzdump.cron', undef, $delete_job);
369 __PACKAGE__-
>register_method({
370 name
=> 'update_job',
374 description
=> "Update vzdump backup job definition.",
376 check
=> ['perm', '/', ['Sys.Modify']],
377 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
380 additionalProperties
=> 0,
381 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
382 id
=> $vzdump_job_id_prop,
384 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
385 type
=> 'string', format
=> 'pve-calendar-event',
391 description
=> "Job Start time.",
392 pattern
=> '\d{1,2}:\d{1,2}',
397 type
=> 'string', format
=> 'pve-day-of-week-list',
399 requires
=> 'starttime',
400 description
=> "Day of week selection.",
403 type
=> 'string', format
=> 'pve-configid-list',
404 description
=> "A list of settings you want to delete.",
410 description
=> "Enable or disable the job.",
416 description
=> "If true, the job will be run as soon as possible if it was missed".
417 " while the scheduler was not running.",
423 description
=> "Description for the Job.",
428 returns
=> { type
=> 'null' },
432 my $rpcenv = PVE
::RPCEnvironment
::get
();
433 my $user = $rpcenv->get_user();
435 if (my $pool = $param->{pool
}) {
436 $rpcenv->check_pool_exist($pool);
437 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
440 $schedule_param_check->($param);
442 my $id = extract_param
($param, 'id');
443 my $delete = extract_param
($param, 'delete');
444 $delete = { map { $_ => 1 } PVE
::Tools
::split_list
($delete) } if $delete;
446 assert_param_permission_update
($rpcenv, $user, $param, $delete);
448 my $update_job = sub {
449 my $data = cfs_read_file
('vzdump.cron');
450 my $jobs_data = cfs_read_file
('jobs.cfg');
452 my $jobs = $data->{jobs
} || [];
454 die "no options specified\n" if !scalar(keys $param->%*) && !scalar(keys $delete->%*);
456 PVE
::VZDump
::verify_vzdump_parameters
($param);
457 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 0, 1);
459 # try to find it in old vzdump.cron and convert it to a job
460 my ($idx) = grep { $jobs->[$_]->{id
} eq $id } (0 .. scalar(@$jobs) - 1);
464 $job = splice @$jobs, $idx, 1;
465 $job->{schedule
} = $convert_to_schedule->($job);
466 delete $job->{starttime
};
469 $job->{type
} = 'vzdump';
470 $jobs_data->{ids
}->{$id} = $job;
472 $job = $jobs_data->{ids
}->{$id};
473 die "no such vzdump job\n" if !$job || $job->{type
} ne 'vzdump';
478 'repeat-missed' => 1,
481 for my $k (keys $delete->%*) {
482 if (!PVE
::VZDump
::option_exists
($k) && !$deletable->{$k}) {
483 raise_param_exc
({ delete => "unknown option '$k'" });
489 foreach my $k (keys %$param) {
490 $job->{$k} = $param->{$k};
493 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
495 if (defined($param->{vmid
})) {
497 delete $job->{exclude
};
499 } elsif ($param->{all
}) {
502 } elsif ($job->{pool
}) {
505 delete $job->{exclude
};
508 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
511 cfs_write_file
('vzdump.cron', $data);
513 cfs_write_file
('jobs.cfg', $jobs_data);
515 PVE
::Jobs
::detect_changed_runtime_props
($id, 'vzdump', $job);
519 cfs_lock_file
('vzdump.cron', undef, sub {
520 cfs_lock_file
('jobs.cfg', undef, $update_job);
526 __PACKAGE__-
>register_method({
527 name
=> 'get_volume_backup_included',
528 path
=> '{id}/included_volumes',
531 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
533 check
=> ['perm', '/', ['Sys.Audit']],
536 additionalProperties
=> 0,
538 id
=> $vzdump_job_id_prop
543 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
552 description
=> 'VMID of the guest.',
556 description
=> 'Name of the guest',
561 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
562 enum
=> ['qemu', 'lxc', 'unknown'],
567 description
=> 'The volumes of the guest with the information if they will be included in backups.',
573 description
=> 'Configuration key of the volume.',
577 description
=> 'Name of the volume.',
581 description
=> 'Whether the volume is included in the backup or not.',
585 description
=> 'The reason why the volume is included (or excluded).',
598 my $rpcenv = PVE
::RPCEnvironment
::get
();
600 my $user = $rpcenv->get_user();
602 my $vzconf = cfs_read_file
('vzdump.cron');
603 my $all_jobs = $vzconf->{jobs
} || [];
605 my $rrd = PVE
::Cluster
::rrd_dump
();
607 for my $j (@$all_jobs) {
608 if ($j->{id
} eq $param->{id
}) {
614 my $jobs_data = cfs_read_file
('jobs.cfg');
615 my $j = $jobs_data->{ids
}->{$param->{id
}};
616 if ($j && $j->{type
} eq 'vzdump') {
620 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
622 my $vmlist = PVE
::Cluster
::get_vmlist
();
626 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
628 for my $node (keys %{$included_guests}) {
629 my $node_vmids = $included_guests->{$node};
630 push(@job_vmids, @{$node_vmids});
633 # remove VMIDs to which the user has no permission to not leak infos
634 # like the guest name
635 my @allowed_vmids = grep {
636 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
643 for my $vmid (@allowed_vmids) {
647 # It's possible that a job has VMIDs configured that are not in
648 # vmlist. This could be because a guest was removed but not purged.
649 # Since there is no more data available we can only deliver the VMID
651 if (!defined $vmlist->{ids
}->{$vmid}) {
652 push(@{$result->{children
}}, {
660 my $type = $vmlist->{ids
}->{$vmid}->{type
};
661 my $node = $vmlist->{ids
}->{$vmid}->{node
};
667 if ($type eq 'qemu') {
668 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
669 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
670 $name = $conf->{name
};
671 } elsif ($type eq 'lxc') {
672 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
673 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
674 $name = $conf->{hostname
};
676 die "VMID $vmid is neither Qemu nor LXC guest\n";
679 foreach my $volume (@$volumes) {
681 # id field must be unique for ExtJS tree view
682 id
=> "$vmid:$volume->{key}",
683 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
684 included
=> $volume->{included
},
685 reason
=> $volume->{reason
},
688 push(@{$children}, $disk);
692 # it's possible for a guest to have no volumes configured
693 $leaf = 1 if !@{$children};
695 push(@{$result->{children
}}, {
699 children
=> $children,