]>
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 SR |
121 | my $client = PVE::PBSClient->new($scfg, $storeid); |
122 | my $ret = $client->file_restore_list($snap, $path, $base64); | |
123 | ||
82f764e1 FG |
124 | # 'leaf' is a proper JSON boolean, map to perl-y bool |
125 | # TODO: make PBSClient decode all bools always as 1/0? | |
126 | foreach my $item (@$ret) { | |
127 | $item->{leaf} = $item->{leaf} ? 1 : 0; | |
128 | } | |
129 | ||
f1a3ce3b SR |
130 | return $ret; |
131 | }}); | |
132 | ||
133 | __PACKAGE__->register_method ({ | |
134 | name => 'download', | |
135 | path => 'download', | |
136 | method => 'GET', | |
137 | proxyto => 'node', | |
138 | permissions => { | |
139 | description => "You need read access for the volume.", | |
140 | user => 'all', | |
141 | }, | |
142 | description => "Extract a file or directory (as zip archive) from a PBS backup.", | |
143 | parameters => { | |
144 | additionalProperties => 0, | |
145 | properties => { | |
146 | node => get_standard_option('pve-node'), | |
c1ec1acb FG |
147 | storage => get_standard_option('pve-storage-id', { |
148 | completion => \&PVE::Storage::complete_storage_enabled, | |
149 | }), | |
150 | volume => { | |
151 | description => "Backup volume ID or name. Currently only PBS snapshots are supported.", | |
f1a3ce3b | 152 | type => 'string', |
c1ec1acb | 153 | completion => \&PVE::Storage::complete_volume, |
f1a3ce3b SR |
154 | }, |
155 | filepath => { | |
156 | description => 'base64-path to the directory or file to download.', | |
157 | type => 'string', | |
158 | }, | |
159 | }, | |
160 | }, | |
161 | returns => { | |
162 | type => 'any', # download | |
163 | }, | |
164 | protected => 1, | |
165 | code => sub { | |
166 | my ($param) = @_; | |
167 | ||
168 | my $rpcenv = PVE::RPCEnvironment::get(); | |
169 | my $user = $rpcenv->get_user(); | |
170 | ||
171 | my $path = extract_param($param, 'filepath'); | |
f1a3ce3b | 172 | my $storeid = extract_param($param, 'storage'); |
c1ec1acb FG |
173 | my $volid = $parse_volname_or_id->($storeid, $param->{volume}); |
174 | ||
f1a3ce3b SR |
175 | my $cfg = PVE::Storage::config(); |
176 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
177 | ||
89a7507c | 178 | PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid, 'backup'); |
f1a3ce3b | 179 | |
c1ec1acb FG |
180 | raise_param_exc({'storage' => "Only PBS storages supported for file-restore."}) |
181 | if $scfg->{type} ne 'pbs'; | |
182 | ||
89a7507c | 183 | my (undef, $snap) = PVE::Storage::parse_volname($cfg, $volid); |
c1ec1acb | 184 | |
f1a3ce3b SR |
185 | my $client = PVE::PBSClient->new($scfg, $storeid); |
186 | my $fifo = $client->file_restore_extract_prepare(); | |
187 | ||
188 | $rpcenv->fork_worker('pbs-download', undef, $user, sub { | |
189 | my $name = decode_base64($path); | |
190 | print "Starting download of file: $name\n"; | |
191 | $client->file_restore_extract($fifo, $snap, $path, 1); | |
192 | }); | |
193 | ||
194 | my $ret = { | |
195 | download => { | |
196 | path => $fifo, | |
197 | stream => 1, | |
198 | 'content-type' => 'application/octet-stream', | |
199 | }, | |
200 | }; | |
201 | return $ret; | |
202 | }}); | |
203 | ||
204 | 1; |