]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/PruneBackups.pm
a84d1c8089b19d6435e86d3bd1d753c99357a990
[pve-storage.git] / PVE / API2 / Storage / PruneBackups.pm
1 package PVE::API2::Storage::PruneBackups;
2
3 use strict;
4 use warnings;
5
6 use PVE::Cluster;
7 use PVE::JSONSchema qw(get_standard_option);
8 use PVE::RESTHandler;
9 use PVE::RPCEnvironment;
10 use PVE::Storage;
11 use PVE::Tools qw(extract_param);
12
13 use base qw(PVE::RESTHandler);
14
15 __PACKAGE__->register_method ({
16 name => 'dryrun',
17 path => '',
18 method => 'GET',
19 description => "Get prune information for backups. NOTE: this is only a preview and might not be exactly " .
20 "what a subsequent prune call does, if the hour changes or if backups are removed/added " .
21 "in the meantime.",
22 permissions => {
23 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
24 },
25 protected => 1,
26 proxyto => 'node',
27 parameters => {
28 additionalProperties => 0,
29 properties => {
30 node => get_standard_option('pve-node'),
31 storage => get_standard_option('pve-storage-id', {
32 completion => \&PVE::Storage::complete_storage_enabled,
33 }),
34 'prune-backups' => get_standard_option('prune-backups', {
35 description => "Use these retention options instead of those from the storage configuration.",
36 optional => 1,
37 }),
38 type => {
39 description => "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
40 type => 'string',
41 optional => 1,
42 enum => ['qemu', 'lxc'],
43 },
44 vmid => get_standard_option('pve-vmid', {
45 description => "Only consider backups for this guest.",
46 optional => 1,
47 completion => \&PVE::Cluster::complete_vmid,
48 }),
49 },
50 },
51 returns => {
52 type => 'array',
53 items => {
54 type => 'object',
55 properties => {
56 volid => {
57 description => "Backup volume ID.",
58 type => 'string',
59 },
60 'ctime' => {
61 description => "Creation time of the backup (seconds since the UNIX epoch).",
62 type => 'integer',
63 },
64 'mark' => {
65 description => "Whether the backup would be kept or removed. For backups that don't " .
66 "use the standard naming scheme, it's 'protected'.",
67 type => 'string',
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
164 1;