]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/FileRestore.pm
prune: mark renamed and protected backups differently
[pve-storage.git] / PVE / API2 / Storage / FileRestore.pm
1 package PVE::API2::Storage::FileRestore;
2
3 use strict;
4 use warnings;
5
6 use MIME::Base64;
7 use PVE::Exception qw(raise_param_exc);
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::PBSClient;
10 use PVE::Storage;
11 use PVE::Tools qw(extract_param);
12
13 use PVE::RESTHandler;
14 use base qw(PVE::RESTHandler);
15
16 my $parse_volname_or_id = sub {
17 my ($storeid, $volume) = @_;
18
19 my $volid;
20 my ($sid, $volname) = PVE::Storage::parse_volume_id($volume, 1);
21
22 if (defined($sid)) {
23 raise_param_exc({ volume => "storage ID mismatch ($sid != $storeid)." })
24 if $sid ne $storeid;
25
26 $volid = $volume;
27 } elsif ($volume =~ m/^backup\//) {
28 $volid = "$storeid:$volume";
29 } else {
30 $volid = "$storeid:backup/$volume";
31 }
32
33 return $volid;
34 };
35
36 __PACKAGE__->register_method ({
37 name => 'list',
38 path => 'list',
39 method => 'GET',
40 proxyto => 'node',
41 permissions => {
42 description => "You need read access for the volume.",
43 user => 'all',
44 },
45 description => "List files and directories for single file restore under the given path.",
46 parameters => {
47 additionalProperties => 0,
48 properties => {
49 node => get_standard_option('pve-node'),
50 storage => get_standard_option('pve-storage-id', {
51 completion => \&PVE::Storage::complete_storage_enabled,
52 }),
53 volume => {
54 description => "Backup volume ID or name. Currently only PBS snapshots are supported.",
55 type => 'string',
56 completion => \&PVE::Storage::complete_volume,
57 },
58 filepath => {
59 description => 'base64-path to the directory or file being listed, or "/".',
60 type => 'string',
61 },
62 },
63 },
64 returns => {
65 type => 'array',
66 items => {
67 type => "object",
68 properties => {
69 filepath => {
70 description => "base64 path of the current entry",
71 type => 'string',
72 },
73 type => {
74 description => "Entry type.",
75 type => 'string',
76 },
77 text => {
78 description => "Entry display text.",
79 type => 'string',
80 },
81 leaf => {
82 description => "If this entry is a leaf in the directory graph.",
83 type => 'boolean',
84 },
85 size => {
86 description => "Entry file size.",
87 type => 'integer',
88 optional => 1,
89 },
90 mtime => {
91 description => "Entry last-modified time (unix timestamp).",
92 type => 'integer',
93 optional => 1,
94 },
95 },
96 },
97 },
98 protected => 1,
99 code => sub {
100 my ($param) = @_;
101
102 my $rpcenv = PVE::RPCEnvironment::get();
103 my $user = $rpcenv->get_user();
104
105 my $path = extract_param($param, 'filepath') || "/";
106 my $base64 = $path ne "/";
107
108 my $storeid = extract_param($param, 'storage');
109
110 my $volid = $parse_volname_or_id->($storeid, $param->{volume});
111 my $cfg = PVE::Storage::config();
112 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
113
114 PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
115
116 raise_param_exc({'storage' => "Only PBS storages supported for file-restore."})
117 if $scfg->{type} ne 'pbs';
118
119 my ($vtype, $snap) = PVE::Storage::parse_volname($cfg, $volid);
120 raise_param_exc({'volume' => 'Not a backup archive.'})
121 if $vtype ne 'backup';
122
123 my $client = PVE::PBSClient->new($scfg, $storeid);
124 my $ret = $client->file_restore_list($snap, $path, $base64);
125
126 # 'leaf' is a proper JSON boolean, map to perl-y bool
127 # TODO: make PBSClient decode all bools always as 1/0?
128 foreach my $item (@$ret) {
129 $item->{leaf} = $item->{leaf} ? 1 : 0;
130 }
131
132 return $ret;
133 }});
134
135 __PACKAGE__->register_method ({
136 name => 'download',
137 path => 'download',
138 method => 'GET',
139 proxyto => 'node',
140 permissions => {
141 description => "You need read access for the volume.",
142 user => 'all',
143 },
144 description => "Extract a file or directory (as zip archive) from a PBS backup.",
145 parameters => {
146 additionalProperties => 0,
147 properties => {
148 node => get_standard_option('pve-node'),
149 storage => get_standard_option('pve-storage-id', {
150 completion => \&PVE::Storage::complete_storage_enabled,
151 }),
152 volume => {
153 description => "Backup volume ID or name. Currently only PBS snapshots are supported.",
154 type => 'string',
155 completion => \&PVE::Storage::complete_volume,
156 },
157 filepath => {
158 description => 'base64-path to the directory or file to download.',
159 type => 'string',
160 },
161 },
162 },
163 returns => {
164 type => 'any', # download
165 },
166 protected => 1,
167 code => sub {
168 my ($param) = @_;
169
170 my $rpcenv = PVE::RPCEnvironment::get();
171 my $user = $rpcenv->get_user();
172
173 my $path = extract_param($param, 'filepath');
174 my $storeid = extract_param($param, 'storage');
175 my $volid = $parse_volname_or_id->($storeid, $param->{volume});
176
177 my $cfg = PVE::Storage::config();
178 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
179
180 PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
181
182 raise_param_exc({'storage' => "Only PBS storages supported for file-restore."})
183 if $scfg->{type} ne 'pbs';
184
185 my ($vtype, $snap) = PVE::Storage::parse_volname($cfg, $volid);
186 raise_param_exc({'volume' => 'Not a backup archive.'})
187 if $vtype ne 'backup';
188
189 my $client = PVE::PBSClient->new($scfg, $storeid);
190 my $fifo = $client->file_restore_extract_prepare();
191
192 $rpcenv->fork_worker('pbs-download', undef, $user, sub {
193 my $name = decode_base64($path);
194 print "Starting download of file: $name\n";
195 $client->file_restore_extract($fifo, $snap, $path, 1);
196 });
197
198 my $ret = {
199 download => {
200 path => $fifo,
201 stream => 1,
202 'content-type' => 'application/octet-stream',
203 },
204 };
205 return $ret;
206 }});
207
208 1;