]>
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 use constant ALL_DAYS
=> 'mon,tue,wed,thu,fri,sat,sun';
22 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
23 sub verify_day_of_week
{
24 my ($value, $noerr) = @_;
26 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
28 return undef if $noerr;
30 die "invalid day '$value'\n";
33 my $vzdump_job_id_prop = {
35 description
=> "The job ID.",
39 my $assert_param_permission = sub {
40 my ($param, $user) = @_;
41 return if $user eq 'root@pam'; # always OK
43 for my $key (qw(tmpdir dumpdir script)) {
44 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
48 __PACKAGE__-
>register_method({
52 description
=> "List vzdump backup schedule.",
54 check
=> ['perm', '/', ['Sys.Audit']],
57 additionalProperties
=> 0,
65 id
=> $vzdump_job_id_prop
68 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
73 my $rpcenv = PVE
::RPCEnvironment
::get
();
74 my $user = $rpcenv->get_user();
76 my $data = cfs_read_file
('vzdump.cron');
78 my $res = $data->{jobs
} || [];
83 __PACKAGE__-
>register_method({
88 description
=> "Create new vzdump backup job.",
90 check
=> ['perm', '/', ['Sys.Modify']],
91 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
94 additionalProperties
=> 0,
95 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
98 description
=> "Job Start time.",
99 pattern
=> '\d{1,2}:\d{1,2}',
103 type
=> 'string', format
=> 'pve-day-of-week-list',
105 description
=> "Day of week selection.",
111 description
=> "Enable or disable the job.",
116 returns
=> { type
=> 'null' },
120 my $rpcenv = PVE
::RPCEnvironment
::get
();
121 my $user = $rpcenv->get_user();
123 $assert_param_permission->($param, $user);
125 if (my $pool = $param->{pool
}) {
126 $rpcenv->check_pool_exist($pool);
127 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
131 my $create_job = sub {
132 my $data = cfs_read_file
('vzdump.cron');
134 $param->{dow
} = ALL_DAYS
if !defined($param->{dow
});
135 $param->{enabled
} = 1 if !defined($param->{enabled
});
136 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
138 push @{$data->{jobs
}}, $param;
140 cfs_write_file
('vzdump.cron', $data);
142 cfs_lock_file
('vzdump.cron', undef, $create_job);
148 __PACKAGE__-
>register_method({
152 description
=> "Read vzdump backup job definition.",
154 check
=> ['perm', '/', ['Sys.Audit']],
157 additionalProperties
=> 0,
159 id
=> $vzdump_job_id_prop
168 my $rpcenv = PVE
::RPCEnvironment
::get
();
169 my $user = $rpcenv->get_user();
171 my $data = cfs_read_file
('vzdump.cron');
173 my $jobs = $data->{jobs
} || [];
175 foreach my $job (@$jobs) {
176 return $job if $job->{id
} eq $param->{id
};
179 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
183 __PACKAGE__-
>register_method({
184 name
=> 'delete_job',
187 description
=> "Delete vzdump backup job definition.",
189 check
=> ['perm', '/', ['Sys.Modify']],
193 additionalProperties
=> 0,
195 id
=> $vzdump_job_id_prop
198 returns
=> { type
=> 'null' },
202 my $rpcenv = PVE
::RPCEnvironment
::get
();
203 my $user = $rpcenv->get_user();
205 my $delete_job = sub {
206 my $data = cfs_read_file
('vzdump.cron');
208 my $jobs = $data->{jobs
} || [];
212 foreach my $job (@$jobs) {
213 if ($job->{id
} eq $param->{id
}) {
216 push @$newjobs, $job;
220 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$found;
222 $data->{jobs
} = $newjobs;
224 cfs_write_file
('vzdump.cron', $data);
226 cfs_lock_file
('vzdump.cron', undef, $delete_job);
232 __PACKAGE__-
>register_method({
233 name
=> 'update_job',
237 description
=> "Update vzdump backup job definition.",
239 check
=> ['perm', '/', ['Sys.Modify']],
240 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
243 additionalProperties
=> 0,
244 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
245 id
=> $vzdump_job_id_prop,
248 description
=> "Job Start time.",
249 pattern
=> '\d{1,2}:\d{1,2}',
253 type
=> 'string', format
=> 'pve-day-of-week-list',
255 description
=> "Day of week selection.",
258 type
=> 'string', format
=> 'pve-configid-list',
259 description
=> "A list of settings you want to delete.",
265 description
=> "Enable or disable the job.",
270 returns
=> { type
=> 'null' },
274 my $rpcenv = PVE
::RPCEnvironment
::get
();
275 my $user = $rpcenv->get_user();
277 $assert_param_permission->($param, $user);
279 if (my $pool = $param->{pool
}) {
280 $rpcenv->check_pool_exist($pool);
281 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
284 my $update_job = sub {
285 my $data = cfs_read_file
('vzdump.cron');
287 my $jobs = $data->{jobs
} || [];
289 die "no options specified\n" if !scalar(keys %$param);
291 PVE
::VZDump
::verify_vzdump_parameters
($param);
293 my @delete = PVE
::Tools
::split_list
(extract_param
($param, 'delete'));
295 foreach my $job (@$jobs) {
296 if ($job->{id
} eq $param->{id
}) {
298 foreach my $k (@delete) {
299 if (!PVE
::VZDump
::option_exists
($k)) {
300 raise_param_exc
({ delete => "unknown option '$k'" });
306 foreach my $k (keys %$param) {
307 $job->{$k} = $param->{$k};
310 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
312 if (defined($param->{vmid
})) {
314 delete $job->{exclude
};
316 } elsif ($param->{all
}) {
319 } elsif ($job->{pool
}) {
322 delete $job->{exclude
};
325 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
327 cfs_write_file
('vzdump.cron', $data);
332 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
334 cfs_lock_file
('vzdump.cron', undef, $update_job);
338 __PACKAGE__-
>register_method({
339 name
=> 'get_volume_backup_included',
340 path
=> '{id}/included_volumes',
343 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
345 check
=> ['perm', '/', ['Sys.Audit']],
348 additionalProperties
=> 0,
350 id
=> $vzdump_job_id_prop
355 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
364 description
=> 'VMID of the guest.',
368 description
=> 'Name of the guest',
373 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
374 enum
=> ['qemu', 'lxc', 'unknown'],
379 description
=> 'The volumes of the guest with the information if they will be included in backups.',
385 description
=> 'Configuration key of the volume.',
389 description
=> 'Name of the volume.',
393 description
=> 'Whether the volume is included in the backup or not.',
397 description
=> 'The reason why the volume is included (or excluded).',
410 my $rpcenv = PVE
::RPCEnvironment
::get
();
412 my $user = $rpcenv->get_user();
414 my $vzconf = cfs_read_file
('vzdump.cron');
415 my $all_jobs = $vzconf->{jobs
} || [];
417 my $rrd = PVE
::Cluster
::rrd_dump
();
419 for my $j (@$all_jobs) {
420 if ($j->{id
} eq $param->{id
}) {
425 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
427 my $vmlist = PVE
::Cluster
::get_vmlist
();
431 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
433 for my $node (keys %{$included_guests}) {
434 my $node_vmids = $included_guests->{$node};
435 push(@job_vmids, @{$node_vmids});
438 # remove VMIDs to which the user has no permission to not leak infos
439 # like the guest name
440 my @allowed_vmids = grep {
441 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
448 for my $vmid (@allowed_vmids) {
452 # It's possible that a job has VMIDs configured that are not in
453 # vmlist. This could be because a guest was removed but not purged.
454 # Since there is no more data available we can only deliver the VMID
456 if (!defined $vmlist->{ids
}->{$vmid}) {
457 push(@{$result->{children
}}, {
465 my $type = $vmlist->{ids
}->{$vmid}->{type
};
466 my $node = $vmlist->{ids
}->{$vmid}->{node
};
472 if ($type eq 'qemu') {
473 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
474 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
475 $name = $conf->{name
};
476 } elsif ($type eq 'lxc') {
477 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
478 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
479 $name = $conf->{hostname
};
481 die "VMID $vmid is neither Qemu nor LXC guest\n";
484 foreach my $volume (@$volumes) {
486 # id field must be unique for ExtJS tree view
487 id
=> "$vmid:$volume->{key}",
488 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
489 included
=> $volume->{included
},
490 reason
=> $volume->{reason
},
493 push(@{$children}, $disk);
497 # it's possible for a guest to have no volumes configured
498 $leaf = 1 if !@{$children};
500 push(@{$result->{children
}}, {
504 children
=> $children,