all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Stefan Reiter <s.reiter@proxmox.com>
To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 storage 10/13] add FileRestore API for PBS
Date: Thu, 22 Apr 2021 17:34:54 +0200	[thread overview]
Message-ID: <20210422153457.12265-11-s.reiter@proxmox.com> (raw)
In-Reply-To: <20210422153457.12265-1-s.reiter@proxmox.com>

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>
---

Requires updated pve-common, pve-http-server.

v2:
* use same permissions as regular backup access, check with check_volume_access

Note: I didn't add the "snapshot -> volume ID" change for now, but AFAICT this
should be a drop-in replacement (maybe aside from the parameter name). The
$real_volume_id sub in Content.pm should handle that just fine? Well, it would
need the "backup/" prefix to become a volid, but again, should be fine.
Feel free to adapt this though if you like the other way better, no hard
feelings tbh...

 PVE/API2/Storage/FileRestore.pm | 160 ++++++++++++++++++++++++++++++++
 PVE/API2/Storage/Makefile       |   2 +-
 PVE/API2/Storage/Status.pm      |   6 ++
 3 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 PVE/API2/Storage/FileRestore.pm

diff --git a/PVE/API2/Storage/FileRestore.pm b/PVE/API2/Storage/FileRestore.pm
new file mode 100644
index 0000000..68f81eb
--- /dev/null
+++ b/PVE/API2/Storage/FileRestore.pm
@@ -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;
diff --git a/PVE/API2/Storage/Makefile b/PVE/API2/Storage/Makefile
index 690b437..1705080 100644
--- a/PVE/API2/Storage/Makefile
+++ b/PVE/API2/Storage/Makefile
@@ -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:
diff --git a/PVE/API2/Storage/Status.pm b/PVE/API2/Storage/Status.pm
index d12643f..897b4a7 100644
--- a/PVE/API2/Storage/Status.pm
+++ b/PVE/API2/Storage/Status.pm
@@ -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 => '',
-- 
2.20.1





WARNING: multiple messages have this Message-ID
From: Stefan Reiter <s.reiter@proxmox.com>
To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v2 storage 10/13] add FileRestore API for PBS
Date: Thu, 22 Apr 2021 17:34:54 +0200	[thread overview]
Message-ID: <20210422153457.12265-11-s.reiter@proxmox.com> (raw)
In-Reply-To: <20210422153457.12265-1-s.reiter@proxmox.com>

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>
---

Requires updated pve-common, pve-http-server.

v2:
* use same permissions as regular backup access, check with check_volume_access

Note: I didn't add the "snapshot -> volume ID" change for now, but AFAICT this
should be a drop-in replacement (maybe aside from the parameter name). The
$real_volume_id sub in Content.pm should handle that just fine? Well, it would
need the "backup/" prefix to become a volid, but again, should be fine.
Feel free to adapt this though if you like the other way better, no hard
feelings tbh...

 PVE/API2/Storage/FileRestore.pm | 160 ++++++++++++++++++++++++++++++++
 PVE/API2/Storage/Makefile       |   2 +-
 PVE/API2/Storage/Status.pm      |   6 ++
 3 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 PVE/API2/Storage/FileRestore.pm

diff --git a/PVE/API2/Storage/FileRestore.pm b/PVE/API2/Storage/FileRestore.pm
new file mode 100644
index 0000000..68f81eb
--- /dev/null
+++ b/PVE/API2/Storage/FileRestore.pm
@@ -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;
diff --git a/PVE/API2/Storage/Makefile b/PVE/API2/Storage/Makefile
index 690b437..1705080 100644
--- a/PVE/API2/Storage/Makefile
+++ b/PVE/API2/Storage/Makefile
@@ -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:
diff --git a/PVE/API2/Storage/Status.pm b/PVE/API2/Storage/Status.pm
index d12643f..897b4a7 100644
--- a/PVE/API2/Storage/Status.pm
+++ b/PVE/API2/Storage/Status.pm
@@ -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 => '',
-- 
2.20.1





  parent reply	other threads:[~2021-04-22 15:35 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22 15:34 [pve-devel] [PATCH v2 00/13] Single-file-restore GUI for PBS snapshots Stefan Reiter
2021-04-22 15:34 ` [pbs-devel] " Stefan Reiter
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-backup 01/13] file-restore: don't force PBS_FINGERPRINT env var Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 17:07   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 17:07     ` [pbs-devel] applied: " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-backup 02/13] client-tools: add crypto_parameters_keep_fd Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 17:07   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 17:07     ` [pbs-devel] applied: " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-backup 03/13] file-restore: support encrypted VM backups Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 17:07   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 17:07     ` [pbs-devel] applied: " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 common 04/13] PBSClient: adapt error message to include full package names Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-23 12:17   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 12:17     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 common 05/13] PBSClient: add file_restore_list command Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-23 12:17   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 12:17     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 common 06/13] PBSClient: add file_restore_extract function Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-23 12:17   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 12:17     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 common 07/13] PBSClient: use crypt params for file 'list' and 'extract' Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 19:14   ` [pve-devel] " Thomas Lamprecht
2021-04-22 19:14     ` [pbs-devel] " Thomas Lamprecht
2021-04-23 12:18   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 12:18     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 http-server 08/13] support streaming data form fh to client Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-23 11:56   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 11:56     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 http-server 09/13] allow stream download from path and over pvedaemon-proxy Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-23 11:56   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-23 11:56     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` Stefan Reiter [this message]
2021-04-22 15:34   ` [pbs-devel] [PATCH v2 storage 10/13] add FileRestore API for PBS Stefan Reiter
2021-04-23 10:34   ` [pve-devel] [PATCH manager] file-restore: pass in full volume ID Fabian Grünbichler
2021-04-23 10:34     ` [pve-devel] [PATCH storage 1/2] file-restore: return perl-y booleans Fabian Grünbichler
2021-04-23 10:34     ` [pve-devel] [PATCH storage 2/2] file-restore: pass in volume ID or name Fabian Grünbichler
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-widget-toolkit 11/13] Utils: add errorCallback to monStoreErrors Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 18:41   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 18:41     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-widget-toolkit 12/13] FileBrowser: support 'virtual'/'v' file type Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 18:41   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 18:41     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:34 ` [pve-devel] [PATCH v2 proxmox-widget-toolkit 13/13] FileBrowser: show errors in messagebox and allow expand 'all' Stefan Reiter
2021-04-22 15:34   ` [pbs-devel] " Stefan Reiter
2021-04-22 18:41   ` [pve-devel] applied: " Thomas Lamprecht
2021-04-22 18:41     ` [pbs-devel] applied: [pve-devel] " Thomas Lamprecht
2021-04-22 15:47 ` [pve-devel] [PATCH v2 manager 1/2] backupview: add file restore button Stefan Reiter
2021-04-22 15:47   ` [pve-devel] [PATCH v2 manager 2/2] gui: add task name for 'pbs-download' Stefan Reiter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210422153457.12265-11-s.reiter@proxmox.com \
    --to=s.reiter@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal