]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/PruneBackups.pm
prune: mark renamed and protected backups differently
[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' => {
ecfe2505
FE
64 description => "Whether the backup would be kept or removed. Backups that are" .
65 " protected or don't use the standard naming scheme are not removed.",
25a95836 66 type => 'string',
ecfe2505 67 enum => ['keep', 'remove', 'protected', 'renamed'],
25a95836
FE
68 },
69 type => {
70 description => "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
71 type => 'string',
72 },
73 'vmid' => {
74 description => "The VM the backup belongs to.",
75 type => 'integer',
76 optional => 1,
77 },
78 },
79 },
80 },
81 code => sub {
82 my ($param) = @_;
83
84 my $cfg = PVE::Storage::config();
85
86 my $vmid = extract_param($param, 'vmid');
87 my $type = extract_param($param, 'type');
88 my $storeid = extract_param($param, 'storage');
89
90 my $prune_backups = extract_param($param, 'prune-backups');
91 $prune_backups = PVE::JSONSchema::parse_property_string('prune-backups', $prune_backups)
92 if defined($prune_backups);
93
94 return PVE::Storage::prune_backups($cfg, $storeid, $prune_backups, $vmid, $type, 1);
95 }});
96
97__PACKAGE__->register_method ({
98 name => 'delete',
99 path => '',
100 method => 'DELETE',
101 description => "Prune backups. Only those using the standard naming scheme are considered.",
102 permissions => {
103 description => "You need the 'Datastore.Allocate' privilege on the storage " .
104 "(or if a VM ID is specified, 'Datastore.AllocateSpace' and 'VM.Backup' for the VM).",
105 user => 'all',
106 },
107 protected => 1,
108 proxyto => 'node',
109 parameters => {
110 additionalProperties => 0,
111 properties => {
112 node => get_standard_option('pve-node'),
113 storage => get_standard_option('pve-storage-id', {
114 completion => \&PVE::Storage::complete_storage,
115 }),
116 'prune-backups' => get_standard_option('prune-backups', {
117 description => "Use these retention options instead of those from the storage configuration.",
118 }),
119 type => {
120 description => "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
121 type => 'string',
122 optional => 1,
123 enum => ['qemu', 'lxc'],
124 },
125 vmid => get_standard_option('pve-vmid', {
126 description => "Only prune backups for this VM.",
127 completion => \&PVE::Cluster::complete_vmid,
128 optional => 1,
129 }),
130 },
131 },
132 returns => { type => 'string' },
133 code => sub {
134 my ($param) = @_;
135
136 my $rpcenv = PVE::RPCEnvironment::get();
137 my $authuser = $rpcenv->get_user();
138
139 my $cfg = PVE::Storage::config();
140
141 my $vmid = extract_param($param, 'vmid');
142 my $type = extract_param($param, 'type');
143 my $storeid = extract_param($param, 'storage');
144
145 my $prune_backups = extract_param($param, 'prune-backups');
146 $prune_backups = PVE::JSONSchema::parse_property_string('prune-backups', $prune_backups)
147 if defined($prune_backups);
148
149 if (defined($vmid)) {
150 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
151 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup']);
152 } else {
153 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']);
154 }
155
156 my $id = (defined($vmid) ? "$vmid@" : '') . $storeid;
157 my $worker = sub {
158 PVE::Storage::prune_backups($cfg, $storeid, $prune_backups, $vmid, $type, 0);
159 };
160
161 return $rpcenv->fork_worker('prunebackups', $id, $authuser, $worker);
162 }});
163
1641;