]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/FileRestore.pm
1 package PVE
::API2
::Storage
::FileRestore
;
7 use PVE
::Exception
qw(raise_param_exc);
8 use PVE
::JSONSchema
qw(get_standard_option);
11 use PVE
::Tools
qw(extract_param);
14 use base
qw(PVE::RESTHandler);
16 my $parse_volname_or_id = sub {
17 my ($storeid, $volume) = @_;
20 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume, 1);
23 raise_param_exc
({ volume
=> "storage ID mismatch ($sid != $storeid)." })
27 } elsif ($volume =~ m/^backup\//) {
28 $volid = "$storeid:$volume";
30 $volid = "$storeid:backup/$volume";
36 __PACKAGE__-
>register_method ({
42 description
=> "You need read access for the volume.",
45 description
=> "List files and directories for single file restore under the given path.",
47 additionalProperties
=> 0,
49 node
=> get_standard_option
('pve-node'),
50 storage
=> get_standard_option
('pve-storage-id', {
51 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
54 description
=> "Backup volume ID or name. Currently only PBS snapshots are supported.",
56 completion
=> \
&PVE
::Storage
::complete_volume
,
59 description
=> 'base64-path to the directory or file being listed, or "/".',
70 description
=> "base64 path of the current entry",
74 description
=> "Entry type.",
78 description
=> "Entry display text.",
82 description
=> "If this entry is a leaf in the directory graph.",
86 description
=> "Entry file size.",
91 description
=> "Entry last-modified time (unix timestamp).",
102 my $rpcenv = PVE
::RPCEnvironment
::get
();
103 my $user = $rpcenv->get_user();
105 my $path = extract_param
($param, 'filepath') || "/";
106 my $base64 = $path ne "/";
108 my $storeid = extract_param
($param, 'storage');
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);
114 PVE
::Storage
::check_volume_access
($rpcenv, $user, $cfg, undef, $volid);
116 raise_param_exc
({'storage' => "Only PBS storages supported for file-restore."})
117 if $scfg->{type
} ne 'pbs';
119 my ($vtype, $snap) = PVE
::Storage
::parse_volname
($cfg, $volid);
120 raise_param_exc
({'volume' => 'Not a backup archive.'})
121 if $vtype ne 'backup';
123 my $client = PVE
::PBSClient-
>new($scfg, $storeid);
124 my $ret = $client->file_restore_list($snap, $path, $base64);
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;
135 __PACKAGE__-
>register_method ({
141 description
=> "You need read access for the volume.",
144 description
=> "Extract a file or directory (as zip archive) from a PBS backup.",
146 additionalProperties
=> 0,
148 node
=> get_standard_option
('pve-node'),
149 storage
=> get_standard_option
('pve-storage-id', {
150 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
153 description
=> "Backup volume ID or name. Currently only PBS snapshots are supported.",
155 completion
=> \
&PVE
::Storage
::complete_volume
,
158 description
=> 'base64-path to the directory or file to download.',
164 type
=> 'any', # download
170 my $rpcenv = PVE
::RPCEnvironment
::get
();
171 my $user = $rpcenv->get_user();
173 my $path = extract_param
($param, 'filepath');
174 my $storeid = extract_param
($param, 'storage');
175 my $volid = $parse_volname_or_id->($storeid, $param->{volume
});
177 my $cfg = PVE
::Storage
::config
();
178 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
180 PVE
::Storage
::check_volume_access
($rpcenv, $user, $cfg, undef, $volid);
182 raise_param_exc
({'storage' => "Only PBS storages supported for file-restore."})
183 if $scfg->{type
} ne 'pbs';
185 my ($vtype, $snap) = PVE
::Storage
::parse_volname
($cfg, $volid);
186 raise_param_exc
({'volume' => 'Not a backup archive.'})
187 if $vtype ne 'backup';
189 my $client = PVE
::PBSClient-
>new($scfg, $storeid);
190 my $fifo = $client->file_restore_extract_prepare();
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);
202 'content-type' => 'application/octet-stream',