]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/PruneBackups.pm
prune_backups: fix message
[pve-storage.git] / PVE / API2 / Storage / PruneBackups.pm
CommitLineData
25a95836
FE
1package PVE::API2::Storage::PruneBackups;
2
3use strict;
4use warnings;
5
6use PVE::Cluster;
7use PVE::JSONSchema qw(get_standard_option);
8use PVE::RESTHandler;
9use PVE::RPCEnvironment;
10use PVE::Storage;
11use PVE::Tools qw(extract_param);
12
13use base qw(PVE::RESTHandler);
14
15__PACKAGE__->register_method ({
16 name => 'dryrun',
17 path => '',
18 method => 'GET',
7b73d327
FE
19 description => "Get prune information for backups. NOTE: this is only a preview and might not be " .
20 "what a subsequent prune call does if backups are removed/added in the meantime.",
25a95836
FE
21 permissions => {
22 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
23 },
24 protected => 1,
25 proxyto => 'node',
26 parameters => {
27 additionalProperties => 0,
28 properties => {
29 node => get_standard_option('pve-node'),
30 storage => get_standard_option('pve-storage-id', {
31 completion => \&PVE::Storage::complete_storage_enabled,
32 }),
33 'prune-backups' => get_standard_option('prune-backups', {
34 description => "Use these retention options instead of those from the storage configuration.",
35 optional => 1,
36 }),
37 type => {
38 description => "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
39 type => 'string',
40 optional => 1,
41 enum => ['qemu', 'lxc'],
42 },
43 vmid => get_standard_option('pve-vmid', {
44 description => "Only consider backups for this guest.",
45 optional => 1,
46 completion => \&PVE::Cluster::complete_vmid,
47 }),
48 },
49 },
50 returns => {
51 type => 'array',
52 items => {
53 type => 'object',
54 properties => {
55 volid => {
56 description => "Backup volume ID.",
57 type => 'string',
58 },
59 'ctime' => {
60 description => "Creation time of the backup (seconds since the UNIX epoch).",
61 type => 'integer',
62 },
63 'mark' => {
64 description => "Whether the backup would be kept or removed. For backups that don't " .
65 "use the standard naming scheme, it's 'protected'.",
66 type => 'string',
67 },
68 type => {
69 description => "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
70 type => 'string',
71 },
72 'vmid' => {
73 description => "The VM the backup belongs to.",
74 type => 'integer',
75 optional => 1,
76 },
77 },
78 },
79 },
80 code => sub {
81 my ($param) = @_;
82
83 my $cfg = PVE::Storage::config();
84
85 my $vmid = extract_param($param, 'vmid');
86 my $type = extract_param($param, 'type');
87 my $storeid = extract_param($param, 'storage');
88
89 my $prune_backups = extract_param($param, 'prune-backups');
90 $prune_backups = PVE::JSONSchema::parse_property_string('prune-backups', $prune_backups)
91 if defined($prune_backups);
92
93 return PVE::Storage::prune_backups($cfg, $storeid, $prune_backups, $vmid, $type, 1);
94 }});
95
96__PACKAGE__->register_method ({
97 name => 'delete',
98 path => '',
99 method => 'DELETE',
100 description => "Prune backups. Only those using the standard naming scheme are considered.",
101 permissions => {
102 description => "You need the 'Datastore.Allocate' privilege on the storage " .
103 "(or if a VM ID is specified, 'Datastore.AllocateSpace' and 'VM.Backup' for the VM).",
104 user => 'all',
105 },
106 protected => 1,
107 proxyto => 'node',
108 parameters => {
109 additionalProperties => 0,
110 properties => {
111 node => get_standard_option('pve-node'),
112 storage => get_standard_option('pve-storage-id', {
113 completion => \&PVE::Storage::complete_storage,
114 }),
115 'prune-backups' => get_standard_option('prune-backups', {
116 description => "Use these retention options instead of those from the storage configuration.",
117 }),
118 type => {
119 description => "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
120 type => 'string',
121 optional => 1,
122 enum => ['qemu', 'lxc'],
123 },
124 vmid => get_standard_option('pve-vmid', {
125 description => "Only prune backups for this VM.",
126 completion => \&PVE::Cluster::complete_vmid,
127 optional => 1,
128 }),
129 },
130 },
131 returns => { type => 'string' },
132 code => sub {
133 my ($param) = @_;
134
135 my $rpcenv = PVE::RPCEnvironment::get();
136 my $authuser = $rpcenv->get_user();
137
138 my $cfg = PVE::Storage::config();
139
140 my $vmid = extract_param($param, 'vmid');
141 my $type = extract_param($param, 'type');
142 my $storeid = extract_param($param, 'storage');
143
144 my $prune_backups = extract_param($param, 'prune-backups');
145 $prune_backups = PVE::JSONSchema::parse_property_string('prune-backups', $prune_backups)
146 if defined($prune_backups);
147
148 if (defined($vmid)) {
149 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
150 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup']);
151 } else {
152 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
153 }
154
155 my $id = (defined($vmid) ? "$vmid@" : '') . $storeid;
156 my $worker = sub {
157 PVE::Storage::prune_backups($cfg, $storeid, $prune_backups, $vmid, $type, 0);
158 };
159
160 return $rpcenv->fork_worker('prunebackups', $id, $authuser, $worker);
161 }});
162
1631;