]>
Commit | Line | Data |
---|---|---|
f1a3ce3b SR |
1 | package PVE::API2::Storage::FileRestore; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use MIME::Base64; | |
c1ec1acb | 7 | use PVE::Exception qw(raise_param_exc); |
f1a3ce3b SR |
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 | ||
c1ec1acb FG |
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 | ||
f1a3ce3b SR |
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'), | |
c1ec1acb FG |
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.", | |
f1a3ce3b | 55 | type => 'string', |
c1ec1acb | 56 | completion => \&PVE::Storage::complete_volume, |
f1a3ce3b SR |
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.", | |
82f764e1 | 83 | type => 'boolean', |
f1a3ce3b SR |
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 "/"; | |
c1ec1acb | 107 | |
f1a3ce3b | 108 | my $storeid = extract_param($param, 'storage'); |
c1ec1acb FG |
109 | |
110 | my $volid = $parse_volname_or_id->($storeid, $param->{volume}); | |
f1a3ce3b SR |
111 | my $cfg = PVE::Storage::config(); |
112 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
113 | ||
89a7507c | 114 | PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid, 'backup'); |
f1a3ce3b | 115 | |
c1ec1acb FG |
116 | raise_param_exc({'storage' => "Only PBS storages supported for file-restore."}) |
117 | if $scfg->{type} ne 'pbs'; | |
118 | ||
89a7507c | 119 | my (undef, $snap) = PVE::Storage::parse_volname($cfg, $volid); |
c1ec1acb | 120 | |
f1a3ce3b | 121 | my $client = PVE::PBSClient->new($scfg, $storeid); |
1de000bb | 122 | my $ret = $client->file_restore_list($snap, $path, $base64, { timeout => 25 }); |
f1a3ce3b | 123 | |
14f99a16 DC |
124 | if (ref($ret) eq "HASH") { |
125 | my $msg = $ret->{message}; | |
126 | if (my $code = $ret->{code}) { | |
127 | die PVE::Exception->new("$msg\n", code => $code); | |
128 | } else { | |
129 | die "$msg\n"; | |
130 | } | |
131 | } elsif (ref($ret) eq "ARRAY") { | |
132 | # 'leaf' is a proper JSON boolean, map to perl-y bool | |
133 | # TODO: make PBSClient decode all bools always as 1/0? | |
134 | foreach my $item (@$ret) { | |
135 | $item->{leaf} = $item->{leaf} ? 1 : 0; | |
136 | } | |
137 | ||
138 | return $ret; | |
82f764e1 FG |
139 | } |
140 | ||
14f99a16 | 141 | die "invalid proxmox-file-restore output"; |
f1a3ce3b SR |
142 | }}); |
143 | ||
144 | __PACKAGE__->register_method ({ | |
145 | name => 'download', | |
146 | path => 'download', | |
147 | method => 'GET', | |
148 | proxyto => 'node', | |
149 | permissions => { | |
150 | description => "You need read access for the volume.", | |
151 | user => 'all', | |
152 | }, | |
153 | description => "Extract a file or directory (as zip archive) from a PBS backup.", | |
154 | parameters => { | |
155 | additionalProperties => 0, | |
156 | properties => { | |
157 | node => get_standard_option('pve-node'), | |
c1ec1acb FG |
158 | storage => get_standard_option('pve-storage-id', { |
159 | completion => \&PVE::Storage::complete_storage_enabled, | |
160 | }), | |
161 | volume => { | |
162 | description => "Backup volume ID or name. Currently only PBS snapshots are supported.", | |
f1a3ce3b | 163 | type => 'string', |
c1ec1acb | 164 | completion => \&PVE::Storage::complete_volume, |
f1a3ce3b SR |
165 | }, |
166 | filepath => { | |
167 | description => 'base64-path to the directory or file to download.', | |
168 | type => 'string', | |
169 | }, | |
170 | }, | |
171 | }, | |
172 | returns => { | |
173 | type => 'any', # download | |
174 | }, | |
175 | protected => 1, | |
176 | code => sub { | |
177 | my ($param) = @_; | |
178 | ||
179 | my $rpcenv = PVE::RPCEnvironment::get(); | |
180 | my $user = $rpcenv->get_user(); | |
181 | ||
182 | my $path = extract_param($param, 'filepath'); | |
f1a3ce3b | 183 | my $storeid = extract_param($param, 'storage'); |
c1ec1acb FG |
184 | my $volid = $parse_volname_or_id->($storeid, $param->{volume}); |
185 | ||
f1a3ce3b SR |
186 | my $cfg = PVE::Storage::config(); |
187 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
188 | ||
89a7507c | 189 | PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid, 'backup'); |
f1a3ce3b | 190 | |
c1ec1acb FG |
191 | raise_param_exc({'storage' => "Only PBS storages supported for file-restore."}) |
192 | if $scfg->{type} ne 'pbs'; | |
193 | ||
89a7507c | 194 | my (undef, $snap) = PVE::Storage::parse_volname($cfg, $volid); |
c1ec1acb | 195 | |
f1a3ce3b SR |
196 | my $client = PVE::PBSClient->new($scfg, $storeid); |
197 | my $fifo = $client->file_restore_extract_prepare(); | |
198 | ||
199 | $rpcenv->fork_worker('pbs-download', undef, $user, sub { | |
200 | my $name = decode_base64($path); | |
201 | print "Starting download of file: $name\n"; | |
f8b890f1 | 202 | $client->file_restore_extract($fifo, $snap, $path, 1); |
f1a3ce3b SR |
203 | }); |
204 | ||
205 | my $ret = { | |
206 | download => { | |
207 | path => $fifo, | |
208 | stream => 1, | |
209 | 'content-type' => 'application/octet-stream', | |
210 | }, | |
211 | }; | |
212 | return $ret; | |
213 | }}); | |
214 | ||
215 | 1; |