]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Backup.pm
1 package PVE
::API2
::Backup
;
8 use PVE
::Tools
qw(extract_param);
9 use PVE
::Cluster
qw(cfs_lock_file cfs_read_file cfs_write_file);
11 use PVE
::RPCEnvironment
;
14 use PVE
::Exception
qw(raise_param_exc);
16 use PVE
::VZDump
::Common
;
18 use base
qw(PVE::RESTHandler);
20 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
21 sub verify_day_of_week
{
22 my ($value, $noerr) = @_;
24 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
26 return undef if $noerr;
28 die "invalid day '$value'\n";
31 my $vzdump_job_id_prop = {
33 description
=> "The job ID.",
37 my $assert_param_permission = sub {
38 my ($param, $user) = @_;
39 return if $user eq 'root@pam'; # always OK
41 for my $key (qw(tmpdir dumpdir script)) {
42 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
46 __PACKAGE__-
>register_method({
50 description
=> "List vzdump backup schedule.",
52 check
=> ['perm', '/', ['Sys.Audit']],
55 additionalProperties
=> 0,
63 id
=> $vzdump_job_id_prop
66 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
71 my $rpcenv = PVE
::RPCEnvironment
::get
();
72 my $user = $rpcenv->get_user();
74 my $data = cfs_read_file
('vzdump.cron');
76 my $res = $data->{jobs
} || [];
81 __PACKAGE__-
>register_method({
86 description
=> "Create new vzdump backup job.",
88 check
=> ['perm', '/', ['Sys.Modify']],
89 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
92 additionalProperties
=> 0,
93 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
96 description
=> "Job Start time.",
97 pattern
=> '\d{1,2}:\d{1,2}',
101 type
=> 'string', format
=> 'pve-day-of-week-list',
103 description
=> "Day of week selection.",
104 default => 'mon,tue,wed,thu,fri,sat,sun',
109 description
=> "Enable or disable the job.",
114 returns
=> { type
=> 'null' },
118 my $rpcenv = PVE
::RPCEnvironment
::get
();
119 my $user = $rpcenv->get_user();
121 $assert_param_permission->($param, $user);
123 if (my $pool = $param->{pool
}) {
124 $rpcenv->check_pool_exist($pool);
125 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
129 my $create_job = sub {
130 my $data = cfs_read_file
('vzdump.cron');
132 $param->{dow
} = 'mon,tue,wed,thu,fri,sat,sun' if !defined($param->{dow
});
133 $param->{enabled
} = 1 if !defined($param->{enabled
});
134 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
136 push @{$data->{jobs
}}, $param;
138 cfs_write_file
('vzdump.cron', $data);
140 cfs_lock_file
('vzdump.cron', undef, $create_job);
146 __PACKAGE__-
>register_method({
150 description
=> "Read vzdump backup job definition.",
152 check
=> ['perm', '/', ['Sys.Audit']],
155 additionalProperties
=> 0,
157 id
=> $vzdump_job_id_prop
166 my $rpcenv = PVE
::RPCEnvironment
::get
();
167 my $user = $rpcenv->get_user();
169 my $data = cfs_read_file
('vzdump.cron');
171 my $jobs = $data->{jobs
} || [];
173 foreach my $job (@$jobs) {
174 return $job if $job->{id
} eq $param->{id
};
177 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
181 __PACKAGE__-
>register_method({
182 name
=> 'delete_job',
185 description
=> "Delete vzdump backup job definition.",
187 check
=> ['perm', '/', ['Sys.Modify']],
191 additionalProperties
=> 0,
193 id
=> $vzdump_job_id_prop
196 returns
=> { type
=> 'null' },
200 my $rpcenv = PVE
::RPCEnvironment
::get
();
201 my $user = $rpcenv->get_user();
203 my $delete_job = sub {
204 my $data = cfs_read_file
('vzdump.cron');
206 my $jobs = $data->{jobs
} || [];
210 foreach my $job (@$jobs) {
211 if ($job->{id
} eq $param->{id
}) {
214 push @$newjobs, $job;
218 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$found;
220 $data->{jobs
} = $newjobs;
222 cfs_write_file
('vzdump.cron', $data);
224 cfs_lock_file
('vzdump.cron', undef, $delete_job);
230 __PACKAGE__-
>register_method({
231 name
=> 'update_job',
235 description
=> "Update vzdump backup job definition.",
237 check
=> ['perm', '/', ['Sys.Modify']],
238 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
241 additionalProperties
=> 0,
242 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
243 id
=> $vzdump_job_id_prop,
246 description
=> "Job Start time.",
247 pattern
=> '\d{1,2}:\d{1,2}',
251 type
=> 'string', format
=> 'pve-day-of-week-list',
253 description
=> "Day of week selection.",
256 type
=> 'string', format
=> 'pve-configid-list',
257 description
=> "A list of settings you want to delete.",
263 description
=> "Enable or disable the job.",
268 returns
=> { type
=> 'null' },
272 my $rpcenv = PVE
::RPCEnvironment
::get
();
273 my $user = $rpcenv->get_user();
275 $assert_param_permission->($param, $user);
277 if (my $pool = $param->{pool
}) {
278 $rpcenv->check_pool_exist($pool);
279 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
282 my $update_job = sub {
283 my $data = cfs_read_file
('vzdump.cron');
285 my $jobs = $data->{jobs
} || [];
287 die "no options specified\n" if !scalar(keys %$param);
289 PVE
::VZDump
::verify_vzdump_parameters
($param);
291 my @delete = PVE
::Tools
::split_list
(extract_param
($param, 'delete'));
293 foreach my $job (@$jobs) {
294 if ($job->{id
} eq $param->{id
}) {
296 foreach my $k (@delete) {
297 if (!PVE
::VZDump
::option_exists
($k)) {
298 raise_param_exc
({ delete => "unknown option '$k'" });
304 foreach my $k (keys %$param) {
305 $job->{$k} = $param->{$k};
308 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
310 if (defined($param->{vmid
})) {
312 delete $job->{exclude
};
314 } elsif ($param->{all
}) {
317 } elsif ($job->{pool
}) {
320 delete $job->{exclude
};
323 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
325 cfs_write_file
('vzdump.cron', $data);
330 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
332 cfs_lock_file
('vzdump.cron', undef, $update_job);
336 __PACKAGE__-
>register_method({
337 name
=> 'get_volume_backup_included',
338 path
=> '{id}/included_volumes',
341 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
343 check
=> ['perm', '/', ['Sys.Audit']],
346 additionalProperties
=> 0,
348 id
=> $vzdump_job_id_prop
353 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
362 description
=> 'VMID of the guest.',
366 description
=> 'Name of the guest',
371 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
372 enum
=> ['qemu', 'lxc', 'unknown'],
377 description
=> 'The volumes of the guest with the information if they will be included in backups.',
383 description
=> 'Configuration key of the volume.',
387 description
=> 'Name of the volume.',
391 description
=> 'Whether the volume is included in the backup or not.',
395 description
=> 'The reason why the volume is included (or excluded).',
408 my $rpcenv = PVE
::RPCEnvironment
::get
();
410 my $user = $rpcenv->get_user();
412 my $vzconf = cfs_read_file
('vzdump.cron');
413 my $all_jobs = $vzconf->{jobs
} || [];
415 my $rrd = PVE
::Cluster
::rrd_dump
();
417 for my $j (@$all_jobs) {
418 if ($j->{id
} eq $param->{id
}) {
423 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
425 my $vmlist = PVE
::Cluster
::get_vmlist
();
429 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
431 for my $node (keys %{$included_guests}) {
432 my $node_vmids = $included_guests->{$node};
433 push(@job_vmids, @{$node_vmids});
436 # remove VMIDs to which the user has no permission to not leak infos
437 # like the guest name
438 my @allowed_vmids = grep {
439 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
446 for my $vmid (@allowed_vmids) {
450 # It's possible that a job has VMIDs configured that are not in
451 # vmlist. This could be because a guest was removed but not purged.
452 # Since there is no more data available we can only deliver the VMID
454 if (!defined $vmlist->{ids
}->{$vmid}) {
455 push(@{$result->{children
}}, {
463 my $type = $vmlist->{ids
}->{$vmid}->{type
};
464 my $node = $vmlist->{ids
}->{$vmid}->{node
};
470 if ($type eq 'qemu') {
471 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
472 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
473 $name = $conf->{name
};
474 } elsif ($type eq 'lxc') {
475 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
476 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
477 $name = $conf->{hostname
};
479 die "VMID $vmid is neither Qemu nor LXC guest\n";
482 foreach my $volume (@$volumes) {
484 # id field must be unique for ExtJS tree view
485 id
=> "$vmid:$volume->{key}",
486 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
487 included
=> $volume->{included
},
488 reason
=> $volume->{reason
},
491 push(@{$children}, $disk);
495 # it's possible for a guest to have no volumes configured
496 $leaf = 1 if !@{$children};
498 push(@{$result->{children
}}, {
502 children
=> $children,