]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Backup.pm
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
::Jobs
; # for VZDump Jobs
19 use Proxmox
::RS
::CalendarEvent
;
21 use base
qw(PVE::RESTHandler);
23 use constant ALL_DAYS
=> 'mon,tue,wed,thu,fri,sat,sun';
25 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
26 sub verify_day_of_week
{
27 my ($value, $noerr) = @_;
29 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
31 return undef if $noerr;
33 die "invalid day '$value'\n";
36 my $vzdump_job_id_prop = {
38 description
=> "The job ID.",
42 my $assert_param_permission = sub {
43 my ($param, $user) = @_;
44 return if $user eq 'root@pam'; # always OK
46 for my $key (qw(tmpdir dumpdir script)) {
47 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
51 my $convert_to_schedule = sub {
54 my $starttime = $job->{starttime
};
56 return "$starttime" if !$job->{dow
}; # dow is restrictive, so none means all days
58 # normalize as it could be a null-separated list previously
59 my $dow = join(',', PVE
::Tools
::split_list
($job->{dow
}));
61 return $dow eq ALL_DAYS ?
"$starttime" : "$dow $starttime";
64 my $schedule_param_check = sub {
65 my ($param, $required) = @_;
66 if (defined($param->{schedule
})) {
67 if (defined($param->{starttime
})) {
68 raise_param_exc
({ starttime
=> "'starttime' and 'schedule' cannot both be set" });
70 } elsif (!defined($param->{starttime
})) {
71 raise_param_exc
({ schedule
=> "neither 'starttime' nor 'schedule' were set" })
74 $param->{schedule
} = $convert_to_schedule->($param);
77 delete $param->{starttime
};
81 __PACKAGE__-
>register_method({
85 description
=> "List vzdump backup schedule.",
87 check
=> ['perm', '/', ['Sys.Audit']],
90 additionalProperties
=> 0,
98 id
=> $vzdump_job_id_prop
101 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
106 my $rpcenv = PVE
::RPCEnvironment
::get
();
107 my $user = $rpcenv->get_user();
109 my $data = cfs_read_file
('vzdump.cron');
110 my $jobs_data = cfs_read_file
('jobs.cfg');
111 my $order = $jobs_data->{order
};
112 my $jobs = $jobs_data->{ids
};
114 my $res = $data->{jobs
} || [];
115 foreach my $job (@$res) {
116 $job->{schedule
} = $convert_to_schedule->($job);
119 foreach my $jobid (sort { $order->{$a} <=> $order->{$b} } keys %$jobs) {
120 my $job = $jobs->{$jobid};
121 next if $job->{type
} ne 'vzdump';
123 if (my $schedule = $job->{schedule
}) {
124 # vzdump jobs are cluster wide, there maybe was no local run
125 # so simply calculate from now
126 my $last_run = time();
127 my $calspec = Proxmox
::RS
::CalendarEvent-
>new($schedule);
128 my $next_run = $calspec->compute_next_event($last_run);
129 $job->{'next-run'} = $next_run if defined($next_run);
138 __PACKAGE__-
>register_method({
139 name
=> 'create_job',
143 description
=> "Create new vzdump backup job.",
145 check
=> ['perm', '/', ['Sys.Modify']],
146 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
149 additionalProperties
=> 0,
150 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
153 description
=> "Job ID (will be autogenerated).",
154 format
=> 'pve-configid',
155 optional
=> 1, # FIXME: make required on 8.0
158 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
159 type
=> 'string', format
=> 'pve-calendar-event',
165 description
=> "Job Start time.",
166 pattern
=> '\d{1,2}:\d{1,2}',
171 type
=> 'string', format
=> 'pve-day-of-week-list',
173 description
=> "Day of week selection.",
174 requires
=> 'starttime',
180 description
=> "Enable or disable the job.",
186 description
=> "If true, the job will be run as soon as possible if it was missed".
187 " while the scheduler was not running.",
193 description
=> "Description for the Job.",
198 returns
=> { type
=> 'null' },
202 my $rpcenv = PVE
::RPCEnvironment
::get
();
203 my $user = $rpcenv->get_user();
205 $assert_param_permission->($param, $user);
207 if (my $pool = $param->{pool
}) {
208 $rpcenv->check_pool_exist($pool);
209 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
212 $schedule_param_check->($param, 1);
214 $param->{enabled
} = 1 if !defined($param->{enabled
});
216 # autogenerate id for api compatibility FIXME remove with 8.0
217 my $id = extract_param
($param, 'id') // UUID
::uuid
();
219 cfs_lock_file
('jobs.cfg', undef, sub {
220 my $data = cfs_read_file
('jobs.cfg');
222 die "Job '$id' already exists\n"
223 if $data->{ids
}->{$id};
225 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
226 my $plugin = PVE
::Jobs
::Plugin-
>lookup('vzdump');
227 my $opts = $plugin->check_config($id, $param, 1, 1);
229 $data->{ids
}->{$id} = $opts;
231 PVE
::Jobs
::create_job
($id, 'vzdump');
233 cfs_write_file
('jobs.cfg', $data);
240 __PACKAGE__-
>register_method({
244 description
=> "Read vzdump backup job definition.",
246 check
=> ['perm', '/', ['Sys.Audit']],
249 additionalProperties
=> 0,
251 id
=> $vzdump_job_id_prop
260 my $rpcenv = PVE
::RPCEnvironment
::get
();
261 my $user = $rpcenv->get_user();
263 my $data = cfs_read_file
('vzdump.cron');
265 my $jobs = $data->{jobs
} || [];
267 foreach my $job (@$jobs) {
268 if ($job->{id
} eq $param->{id
}) {
269 $job->{schedule
} = $convert_to_schedule->($job);
274 my $jobs_data = cfs_read_file
('jobs.cfg');
275 my $job = $jobs_data->{ids
}->{$param->{id
}};
276 return $job if $job && $job->{type
} eq 'vzdump';
278 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
282 __PACKAGE__-
>register_method({
283 name
=> 'delete_job',
286 description
=> "Delete vzdump backup job definition.",
288 check
=> ['perm', '/', ['Sys.Modify']],
292 additionalProperties
=> 0,
294 id
=> $vzdump_job_id_prop
297 returns
=> { type
=> 'null' },
301 my $rpcenv = PVE
::RPCEnvironment
::get
();
302 my $user = $rpcenv->get_user();
304 my $id = $param->{id
};
306 my $delete_job = sub {
307 my $data = cfs_read_file
('vzdump.cron');
309 my $jobs = $data->{jobs
} || [];
313 foreach my $job (@$jobs) {
314 if ($job->{id
} eq $id) {
317 push @$newjobs, $job;
322 cfs_lock_file
('jobs.cfg', undef, sub {
323 my $jobs_data = cfs_read_file
('jobs.cfg');
325 if (!defined($jobs_data->{ids
}->{$id})) {
326 raise_param_exc
({ id
=> "No such job '$id'" });
328 delete $jobs_data->{ids
}->{$id};
330 PVE
::Jobs
::remove_job
($id, 'vzdump');
332 cfs_write_file
('jobs.cfg', $jobs_data);
336 $data->{jobs
} = $newjobs;
338 cfs_write_file
('vzdump.cron', $data);
341 cfs_lock_file
('vzdump.cron', undef, $delete_job);
347 __PACKAGE__-
>register_method({
348 name
=> 'update_job',
352 description
=> "Update vzdump backup job definition.",
354 check
=> ['perm', '/', ['Sys.Modify']],
355 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
358 additionalProperties
=> 0,
359 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
360 id
=> $vzdump_job_id_prop,
362 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
363 type
=> 'string', format
=> 'pve-calendar-event',
369 description
=> "Job Start time.",
370 pattern
=> '\d{1,2}:\d{1,2}',
375 type
=> 'string', format
=> 'pve-day-of-week-list',
377 requires
=> 'starttime',
378 description
=> "Day of week selection.",
381 type
=> 'string', format
=> 'pve-configid-list',
382 description
=> "A list of settings you want to delete.",
388 description
=> "Enable or disable the job.",
394 description
=> "If true, the job will be run as soon as possible if it was missed".
395 " while the scheduler was not running.",
401 description
=> "Description for the Job.",
406 returns
=> { type
=> 'null' },
410 my $rpcenv = PVE
::RPCEnvironment
::get
();
411 my $user = $rpcenv->get_user();
413 $assert_param_permission->($param, $user);
415 if (my $pool = $param->{pool
}) {
416 $rpcenv->check_pool_exist($pool);
417 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
420 $schedule_param_check->($param);
422 my $id = extract_param
($param, 'id');
423 my $delete = extract_param
($param, 'delete');
425 $delete = [PVE
::Tools
::split_list
($delete)];
428 my $update_job = sub {
429 my $data = cfs_read_file
('vzdump.cron');
430 my $jobs_data = cfs_read_file
('jobs.cfg');
432 my $jobs = $data->{jobs
} || [];
434 die "no options specified\n" if !scalar(keys %$param);
436 PVE
::VZDump
::verify_vzdump_parameters
($param);
437 my $plugin = PVE
::Jobs
::Plugin-
>lookup('vzdump');
438 my $opts = $plugin->check_config($id, $param, 0, 1);
440 # try to find it in old vzdump.cron and convert it to a job
441 my ($idx) = grep { $jobs->[$_]->{id
} eq $id } (0 .. scalar(@$jobs) - 1);
445 $job = splice @$jobs, $idx, 1;
446 $job->{schedule
} = $convert_to_schedule->($job);
447 delete $job->{starttime
};
450 $job->{type
} = 'vzdump';
451 $jobs_data->{ids
}->{$id} = $job;
453 $job = $jobs_data->{ids
}->{$id};
454 die "no such vzdump job\n" if !$job || $job->{type
} ne 'vzdump';
457 my $old_enabled = $job->{enabled
} // 1;
461 'repeat-missed' => 1,
464 foreach my $k (@$delete) {
465 if (!PVE
::VZDump
::option_exists
($k) && !$deletable->{$k}) {
466 raise_param_exc
({ delete => "unknown option '$k'" });
472 my $need_run_time_update = 0;
473 if (defined($param->{schedule
}) && $param->{schedule
} ne $job->{schedule
}) {
474 $need_run_time_update = 1;
477 foreach my $k (keys %$param) {
478 $job->{$k} = $param->{$k};
481 my $new_enabled = $job->{enabled
} // 1;
483 if ($new_enabled && !$old_enabled) {
484 $need_run_time_update = 1;
487 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
489 if (defined($param->{vmid
})) {
491 delete $job->{exclude
};
493 } elsif ($param->{all
}) {
496 } elsif ($job->{pool
}) {
499 delete $job->{exclude
};
502 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
504 if ($need_run_time_update) {
505 PVE
::Jobs
::update_last_runtime
($id, 'vzdump');
509 cfs_write_file
('vzdump.cron', $data);
511 cfs_write_file
('jobs.cfg', $jobs_data);
514 cfs_lock_file
('vzdump.cron', undef, sub {
515 cfs_lock_file
('jobs.cfg', undef, $update_job);
521 __PACKAGE__-
>register_method({
522 name
=> 'get_volume_backup_included',
523 path
=> '{id}/included_volumes',
526 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
528 check
=> ['perm', '/', ['Sys.Audit']],
531 additionalProperties
=> 0,
533 id
=> $vzdump_job_id_prop
538 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
547 description
=> 'VMID of the guest.',
551 description
=> 'Name of the guest',
556 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
557 enum
=> ['qemu', 'lxc', 'unknown'],
562 description
=> 'The volumes of the guest with the information if they will be included in backups.',
568 description
=> 'Configuration key of the volume.',
572 description
=> 'Name of the volume.',
576 description
=> 'Whether the volume is included in the backup or not.',
580 description
=> 'The reason why the volume is included (or excluded).',
593 my $rpcenv = PVE
::RPCEnvironment
::get
();
595 my $user = $rpcenv->get_user();
597 my $vzconf = cfs_read_file
('vzdump.cron');
598 my $all_jobs = $vzconf->{jobs
} || [];
600 my $rrd = PVE
::Cluster
::rrd_dump
();
602 for my $j (@$all_jobs) {
603 if ($j->{id
} eq $param->{id
}) {
609 my $jobs_data = cfs_read_file
('jobs.cfg');
610 my $j = $jobs_data->{ids
}->{$param->{id
}};
611 if ($j && $j->{type
} eq 'vzdump') {
615 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
617 my $vmlist = PVE
::Cluster
::get_vmlist
();
621 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
623 for my $node (keys %{$included_guests}) {
624 my $node_vmids = $included_guests->{$node};
625 push(@job_vmids, @{$node_vmids});
628 # remove VMIDs to which the user has no permission to not leak infos
629 # like the guest name
630 my @allowed_vmids = grep {
631 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
638 for my $vmid (@allowed_vmids) {
642 # It's possible that a job has VMIDs configured that are not in
643 # vmlist. This could be because a guest was removed but not purged.
644 # Since there is no more data available we can only deliver the VMID
646 if (!defined $vmlist->{ids
}->{$vmid}) {
647 push(@{$result->{children
}}, {
655 my $type = $vmlist->{ids
}->{$vmid}->{type
};
656 my $node = $vmlist->{ids
}->{$vmid}->{node
};
662 if ($type eq 'qemu') {
663 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
664 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
665 $name = $conf->{name
};
666 } elsif ($type eq 'lxc') {
667 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
668 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
669 $name = $conf->{hostname
};
671 die "VMID $vmid is neither Qemu nor LXC guest\n";
674 foreach my $volume (@$volumes) {
676 # id field must be unique for ExtJS tree view
677 id
=> "$vmid:$volume->{key}",
678 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
679 included
=> $volume->{included
},
680 reason
=> $volume->{reason
},
683 push(@{$children}, $disk);
687 # it's possible for a guest to have no volumes configured
688 $leaf = 1 if !@{$children};
690 push(@{$result->{children
}}, {
694 children
=> $children,