]> git.proxmox.com Git - pve-storage.git/commitdiff
add FileRestore API for PBS
authorStefan Reiter <s.reiter@proxmox.com>
Thu, 22 Apr 2021 15:34:54 +0000 (17:34 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 23 Apr 2021 12:09:48 +0000 (14:09 +0200)
Includes list and restore calls.

Requires VM.Backup and Datastore.Audit permissions, for the accessed
VM/CT and containing datastore respectively.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
PVE/API2/Storage/FileRestore.pm [new file with mode: 0644]
PVE/API2/Storage/Makefile
PVE/API2/Storage/Status.pm

diff --git a/PVE/API2/Storage/FileRestore.pm b/PVE/API2/Storage/FileRestore.pm
new file mode 100644 (file)
index 0000000..68f81eb
--- /dev/null
@@ -0,0 +1,160 @@
+package PVE::API2::Storage::FileRestore;
+
+use strict;
+use warnings;
+
+use MIME::Base64;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::PBSClient;
+use PVE::Storage;
+use PVE::Tools qw(extract_param);
+
+use PVE::RESTHandler;
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'list',
+    path => 'list',
+    method => 'GET',
+    proxyto => 'node',
+    permissions => {
+       description => "You need read access for the volume.",
+       user => 'all',
+    },
+    description => "List files and directories for single file restore under the given path.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           storage => get_standard_option('pve-storage-id'),
+           snapshot => {
+               description => "Backup snapshot identifier.",
+               type => 'string',
+           },
+           filepath => {
+               description => 'base64-path to the directory or file being listed, or "/".',
+               type => 'string',
+           },
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {
+               filepath => {
+                   description => "base64 path of the current entry",
+                   type => 'string',
+               },
+               type => {
+                   description => "Entry type.",
+                   type => 'string',
+               },
+               text => {
+                   description => "Entry display text.",
+                   type => 'string',
+               },
+               leaf => {
+                   description => "If this entry is a leaf in the directory graph.",
+                   type => 'any', # JSON::PP::Boolean gets passed through
+               },
+               size => {
+                   description => "Entry file size.",
+                   type => 'integer',
+                   optional => 1,
+               },
+               mtime => {
+                   description => "Entry last-modified time (unix timestamp).",
+                   type => 'integer',
+                   optional => 1,
+               },
+           },
+       },
+    },
+    protected => 1,
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $user = $rpcenv->get_user();
+
+       my $path = extract_param($param, 'filepath') || "/";
+       my $base64 = $path ne "/";
+       my $snap = extract_param($param, 'snapshot');
+       my $storeid = extract_param($param, 'storage');
+       my $cfg = PVE::Storage::config();
+       my $scfg = PVE::Storage::storage_config($cfg, $storeid);
+
+       my $volid = "$storeid:backup/$snap";
+       PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
+
+       my $client = PVE::PBSClient->new($scfg, $storeid);
+       my $ret = $client->file_restore_list($snap, $path, $base64);
+
+       return $ret;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'download',
+    path => 'download',
+    method => 'GET',
+    proxyto => 'node',
+    permissions => {
+       description => "You need read access for the volume.",
+       user => 'all',
+    },
+    description => "Extract a file or directory (as zip archive) from a PBS backup.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           storage => get_standard_option('pve-storage-id'),
+           snapshot => {
+               description => "Backup snapshot identifier.",
+               type => 'string',
+           },
+           filepath => {
+               description => 'base64-path to the directory or file to download.',
+               type => 'string',
+           },
+       },
+    },
+    returns => {
+       type => 'any', # download
+    },
+    protected => 1,
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $user = $rpcenv->get_user();
+
+       my $path = extract_param($param, 'filepath');
+       my $snap = extract_param($param, 'snapshot');
+       my $storeid = extract_param($param, 'storage');
+       my $cfg = PVE::Storage::config();
+       my $scfg = PVE::Storage::storage_config($cfg, $storeid);
+
+       my $volid = "$storeid:backup/$snap";
+       PVE::Storage::check_volume_access($rpcenv, $user, $cfg, undef, $volid);
+
+       my $client = PVE::PBSClient->new($scfg, $storeid);
+       my $fifo = $client->file_restore_extract_prepare();
+
+       $rpcenv->fork_worker('pbs-download', undef, $user, sub {
+           my $name = decode_base64($path);
+           print "Starting download of file: $name\n";
+           $client->file_restore_extract($fifo, $snap, $path, 1);
+       });
+
+       my $ret = {
+           download => {
+               path => $fifo,
+               stream => 1,
+               'content-type' => 'application/octet-stream',
+           },
+       };
+       return $ret;
+    }});
+
+1;
index 690b437b564df5d28a04c0aff99ee15d51845fcd..17050803c86b219463df2f1bf2654ab2e27ee4dc 100644 (file)
@@ -1,5 +1,5 @@
 
-SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm
+SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm FileRestore.pm
 
 .PHONY: install
 install:
index d12643fc554a4345630f140e387441cc15698d1c..897b4a77e28b739a590c5ade6046dd87fd1f009f 100644 (file)
@@ -12,6 +12,7 @@ use PVE::RRD;
 use PVE::Storage;
 use PVE::API2::Storage::Content;
 use PVE::API2::Storage::PruneBackups;
+use PVE::API2::Storage::FileRestore;
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::JSONSchema qw(get_standard_option);
@@ -32,6 +33,11 @@ __PACKAGE__->register_method ({
     path => '{storage}/content',
 });
 
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Storage::FileRestore",
+   path => '{storage}/file-restore',
+});
+
 __PACKAGE__->register_method ({
     name => 'index',
     path => '',