]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Backup.pm
428f4cb390bd7a7199c163b02362725237e2ffc5
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
20 use base
qw(PVE::RESTHandler);
22 use constant ALL_DAYS
=> 'mon,tue,wed,thu,fri,sat,sun';
24 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
25 sub verify_day_of_week
{
26 my ($value, $noerr) = @_;
28 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
30 return undef if $noerr;
32 die "invalid day '$value'\n";
35 my $vzdump_job_id_prop = {
37 description
=> "The job ID.",
41 my $assert_param_permission = sub {
42 my ($param, $user) = @_;
43 return if $user eq 'root@pam'; # always OK
45 for my $key (qw(tmpdir dumpdir script)) {
46 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
50 my $convert_to_schedule = sub {
53 my $starttime = $job->{starttime
};
54 my $dow = $job->{dow
};
56 if (!$dow || $dow eq ALL_DAYS
) {
60 return "$dow $starttime";
63 my $schedule_param_check = sub {
65 if (defined($param->{schedule
})) {
66 if (defined($param->{starttime
})) {
67 raise_param_exc
({ starttime
=> "'starttime' and 'schedule' cannot both be set" });
69 } elsif (!defined($param->{starttime
})) {
70 raise_param_exc
({ schedule
=> "neither 'starttime' nor 'schedule' were set" });
72 $param->{schedule
} = $convert_to_schedule->($param);
75 delete $param->{starttime
};
79 __PACKAGE__-
>register_method({
83 description
=> "List vzdump backup schedule.",
85 check
=> ['perm', '/', ['Sys.Audit']],
88 additionalProperties
=> 0,
96 id
=> $vzdump_job_id_prop
99 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
104 my $rpcenv = PVE
::RPCEnvironment
::get
();
105 my $user = $rpcenv->get_user();
107 my $data = cfs_read_file
('vzdump.cron');
108 my $jobs_data = cfs_read_file
('jobs.cfg');
109 my $order = $jobs_data->{order
};
110 my $jobs = $jobs_data->{ids
};
112 my $res = $data->{jobs
} || [];
113 foreach my $job (@$res) {
114 $job->{schedule
} = $convert_to_schedule->($job);
117 foreach my $jobid (sort { $order->{$a} <=> $order->{$b} } keys %$jobs) {
118 my $job = $jobs->{$jobid};
119 next if $job->{type
} ne 'vzdump';
126 __PACKAGE__-
>register_method({
127 name
=> 'create_job',
131 description
=> "Create new vzdump backup job.",
133 check
=> ['perm', '/', ['Sys.Modify']],
134 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
137 additionalProperties
=> 0,
138 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
141 description
=> "Job ID (will be autogenerated).",
142 format
=> 'pve-configid',
143 optional
=> 1, # FIXME: make required on 8.0
146 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
147 type
=> 'string', format
=> 'pve-calendar-event',
153 description
=> "Job Start time.",
154 pattern
=> '\d{1,2}:\d{1,2}',
159 type
=> 'string', format
=> 'pve-day-of-week-list',
161 description
=> "Day of week selection.",
162 requires
=> 'starttime',
168 description
=> "Enable or disable the job.",
174 description
=> "Description for the Job.",
179 returns
=> { type
=> 'null' },
183 my $rpcenv = PVE
::RPCEnvironment
::get
();
184 my $user = $rpcenv->get_user();
186 $assert_param_permission->($param, $user);
188 if (my $pool = $param->{pool
}) {
189 $rpcenv->check_pool_exist($pool);
190 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
193 $schedule_param_check->($param);
195 $param->{enabled
} = 1 if !defined($param->{enabled
});
197 # autogenerate id for api compatibility FIXME remove with 8.0
198 my $id = extract_param
($param, 'id') // uuid
();
200 cfs_lock_file
('jobs.cfg', undef, sub {
201 my $data = cfs_read_file
('jobs.cfg');
203 die "Job '$id' already exists\n"
204 if $data->{ids
}->{$id};
206 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
207 my $plugin = PVE
::Jobs
::Plugin-
>lookup('vzdump');
208 my $opts = $plugin->check_config($id, $param, 1, 1);
210 $data->{ids
}->{$id} = $opts;
212 PVE
::Jobs
::create_job
($id, 'vzdump');
214 cfs_write_file
('jobs.cfg', $data);
221 __PACKAGE__-
>register_method({
225 description
=> "Read vzdump backup job definition.",
227 check
=> ['perm', '/', ['Sys.Audit']],
230 additionalProperties
=> 0,
232 id
=> $vzdump_job_id_prop
241 my $rpcenv = PVE
::RPCEnvironment
::get
();
242 my $user = $rpcenv->get_user();
244 my $data = cfs_read_file
('vzdump.cron');
246 my $jobs = $data->{jobs
} || [];
248 foreach my $job (@$jobs) {
249 if ($job->{id
} eq $param->{id
}) {
250 $job->{schedule
} = $convert_to_schedule->($job);
255 my $jobs_data = cfs_read_file
('jobs.cfg');
256 my $job = $jobs_data->{ids
}->{$param->{id
}};
257 return $job if $job && $job->{type
} eq 'vzdump';
259 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
263 __PACKAGE__-
>register_method({
264 name
=> 'delete_job',
267 description
=> "Delete vzdump backup job definition.",
269 check
=> ['perm', '/', ['Sys.Modify']],
273 additionalProperties
=> 0,
275 id
=> $vzdump_job_id_prop
278 returns
=> { type
=> 'null' },
282 my $rpcenv = PVE
::RPCEnvironment
::get
();
283 my $user = $rpcenv->get_user();
285 my $id = $param->{id
};
287 my $delete_job = sub {
288 my $data = cfs_read_file
('vzdump.cron');
290 my $jobs = $data->{jobs
} || [];
294 foreach my $job (@$jobs) {
295 if ($job->{id
} eq $id) {
298 push @$newjobs, $job;
303 cfs_lock_file
('jobs.cfg', undef, sub {
304 my $jobs_data = cfs_read_file
('jobs.cfg');
306 if (!defined($jobs_data->{ids
}->{$id})) {
307 raise_param_exc
({ id
=> "No such job '$id'" });
309 delete $jobs_data->{ids
}->{$id};
311 PVE
::Jobs
::remove_job
($id, 'vzdump');
313 cfs_write_file
('jobs.cfg', $jobs_data);
317 $data->{jobs
} = $newjobs;
319 cfs_write_file
('vzdump.cron', $data);
322 cfs_lock_file
('vzdump.cron', undef, $delete_job);
328 __PACKAGE__-
>register_method({
329 name
=> 'update_job',
333 description
=> "Update vzdump backup job definition.",
335 check
=> ['perm', '/', ['Sys.Modify']],
336 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
339 additionalProperties
=> 0,
340 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
341 id
=> $vzdump_job_id_prop,
343 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
344 type
=> 'string', format
=> 'pve-calendar-event',
350 description
=> "Job Start time.",
351 pattern
=> '\d{1,2}:\d{1,2}',
356 type
=> 'string', format
=> 'pve-day-of-week-list',
358 requires
=> 'starttime',
359 description
=> "Day of week selection.",
362 type
=> 'string', format
=> 'pve-configid-list',
363 description
=> "A list of settings you want to delete.",
369 description
=> "Enable or disable the job.",
375 description
=> "Description for the Job.",
380 returns
=> { type
=> 'null' },
384 my $rpcenv = PVE
::RPCEnvironment
::get
();
385 my $user = $rpcenv->get_user();
387 $assert_param_permission->($param, $user);
389 if (my $pool = $param->{pool
}) {
390 $rpcenv->check_pool_exist($pool);
391 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
394 $schedule_param_check->($param);
396 my $id = extract_param
($param, 'id');
397 my $delete = extract_param
($param, 'delete');
399 $delete = [PVE
::Tools
::split_list
($delete)];
402 my $update_job = sub {
403 my $data = cfs_read_file
('vzdump.cron');
404 my $jobs_data = cfs_read_file
('jobs.cfg');
406 my $jobs = $data->{jobs
} || [];
408 die "no options specified\n" if !scalar(keys %$param);
410 PVE
::VZDump
::verify_vzdump_parameters
($param);
411 my $plugin = PVE
::Jobs
::Plugin-
>lookup('vzdump');
412 my $opts = $plugin->check_config($id, $param, 0, 1);
414 # try to find it in old vzdump.cron and convert it to a job
415 my ($idx) = grep { $jobs->[$_]->{id
} eq $id } (0 .. scalar(@$jobs) - 1);
419 $job = splice @$jobs, $idx, 1;
420 $job->{schedule
} = $convert_to_schedule->($job);
421 delete $job->{starttime
};
424 $job->{type
} = 'vzdump';
425 $jobs_data->{ids
}->{$id} = $job;
427 $job = $jobs_data->{ids
}->{$id};
428 die "no such vzdump job\n" if !$job || $job->{type
} ne 'vzdump';
431 foreach my $k (@$delete) {
432 if (!PVE
::VZDump
::option_exists
($k) && $k ne 'comment') {
433 raise_param_exc
({ delete => "unknown option '$k'" });
439 my $schedule_updated = 0;
440 if ($param->{schedule
} ne $job->{schedule
}) {
441 $schedule_updated = 1;
444 foreach my $k (keys %$param) {
445 $job->{$k} = $param->{$k};
448 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
450 if (defined($param->{vmid
})) {
452 delete $job->{exclude
};
454 } elsif ($param->{all
}) {
457 } elsif ($job->{pool
}) {
460 delete $job->{exclude
};
463 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
465 if ($schedule_updated) {
466 PVE
::Jobs
::updated_job_schedule
($id, 'vzdump');
470 cfs_write_file
('vzdump.cron', $data);
472 cfs_write_file
('jobs.cfg', $jobs_data);
475 cfs_lock_file
('vzdump.cron', undef, sub {
476 cfs_lock_file
('jobs.cfg', undef, $update_job);
482 __PACKAGE__-
>register_method({
483 name
=> 'get_volume_backup_included',
484 path
=> '{id}/included_volumes',
487 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
489 check
=> ['perm', '/', ['Sys.Audit']],
492 additionalProperties
=> 0,
494 id
=> $vzdump_job_id_prop
499 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
508 description
=> 'VMID of the guest.',
512 description
=> 'Name of the guest',
517 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
518 enum
=> ['qemu', 'lxc', 'unknown'],
523 description
=> 'The volumes of the guest with the information if they will be included in backups.',
529 description
=> 'Configuration key of the volume.',
533 description
=> 'Name of the volume.',
537 description
=> 'Whether the volume is included in the backup or not.',
541 description
=> 'The reason why the volume is included (or excluded).',
554 my $rpcenv = PVE
::RPCEnvironment
::get
();
556 my $user = $rpcenv->get_user();
558 my $vzconf = cfs_read_file
('vzdump.cron');
559 my $all_jobs = $vzconf->{jobs
} || [];
561 my $rrd = PVE
::Cluster
::rrd_dump
();
563 for my $j (@$all_jobs) {
564 if ($j->{id
} eq $param->{id
}) {
570 my $jobs_data = cfs_read_file
('jobs.cfg');
571 my $j = $jobs_data->{ids
}->{$param->{id
}};
572 if ($j && $j->{type
} eq 'vzdump') {
576 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
578 my $vmlist = PVE
::Cluster
::get_vmlist
();
582 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
584 for my $node (keys %{$included_guests}) {
585 my $node_vmids = $included_guests->{$node};
586 push(@job_vmids, @{$node_vmids});
589 # remove VMIDs to which the user has no permission to not leak infos
590 # like the guest name
591 my @allowed_vmids = grep {
592 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
599 for my $vmid (@allowed_vmids) {
603 # It's possible that a job has VMIDs configured that are not in
604 # vmlist. This could be because a guest was removed but not purged.
605 # Since there is no more data available we can only deliver the VMID
607 if (!defined $vmlist->{ids
}->{$vmid}) {
608 push(@{$result->{children
}}, {
616 my $type = $vmlist->{ids
}->{$vmid}->{type
};
617 my $node = $vmlist->{ids
}->{$vmid}->{node
};
623 if ($type eq 'qemu') {
624 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
625 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
626 $name = $conf->{name
};
627 } elsif ($type eq 'lxc') {
628 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
629 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
630 $name = $conf->{hostname
};
632 die "VMID $vmid is neither Qemu nor LXC guest\n";
635 foreach my $volume (@$volumes) {
637 # id field must be unique for ExtJS tree view
638 id
=> "$vmid:$volume->{key}",
639 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
640 included
=> $volume->{included
},
641 reason
=> $volume->{reason
},
644 push(@{$children}, $disk);
648 # it's possible for a guest to have no volumes configured
649 $leaf = 1 if !@{$children};
651 push(@{$result->{children
}}, {
655 children
=> $children,