From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id E29AF9C0CB
 for <pve-devel@lists.proxmox.com>; Mon, 23 Oct 2023 13:19:25 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A5DD512B68
 for <pve-devel@lists.proxmox.com>; Mon, 23 Oct 2023 13:18:55 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Mon, 23 Oct 2023 13:18:55 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CD3404457F
 for <pve-devel@lists.proxmox.com>; Mon, 23 Oct 2023 13:18:54 +0200 (CEST)
From: Christian Ebner <c.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Mon, 23 Oct 2023 13:18:34 +0200
Message-Id: <20231023111835.238407-3-c.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20231023111835.238407-1-c.ebner@proxmox.com>
References: <20231023111835.238407-1-c.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.078 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [create.pm, lxc.pm]
Subject: [pve-devel] [RFC v2 pve-container pve-manager 2/3] api: allow to
 exclude mountpoins from restore
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 23 Oct 2023 11:19:25 -0000

Adds an optional parameter which allows to specify the names of
mountpoints which should be excluded from restore.

These mountpoints are not (re-)created and files located below the
mountpoint are not restored from backup.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---

changes since v1:
    not present in v1

 src/PVE/API2/LXC.pm   | 14 ++++++++++++--
 src/PVE/LXC/Create.pm | 11 +++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 090dddf..523afb8 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -162,6 +162,12 @@ __PACKAGE__->register_method({
 		type => 'boolean',
 		description => "Mark this as restore task.",
 	    },
+	    'exclude-mps' => {
+		optional => 1,
+		type => 'string',
+		description => "Comma separated lists of mountpoint to exclude from restore.",
+		pattern => '(mp\d+)(,mp\d+)*',
+	    },
 	    unique => {
 		optional => 1,
 		type => 'boolean',
@@ -222,6 +228,8 @@ __PACKAGE__->register_method({
 	# 'unprivileged' is read-only, so we can't pass it to update_pct_config
 	my $unprivileged = extract_param($param, 'unprivileged');
 	my $restore = extract_param($param, 'restore');
+	my $exclude_mps = extract_param($param, 'exclude-mps');
+	my %excludes = map {$_ => 1} split(',', $exclude_mps) if $exclude_mps;
 	my $unique = extract_param($param, 'unique');
 
 	$param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
@@ -427,8 +435,9 @@ __PACKAGE__->register_method({
 			    if !defined($mp_param->{rootfs});
 			PVE::LXC::Config->foreach_volume($mp_param, sub {
 			    my ($ms, $mountpoint) = @_;
-			    if ($ms eq 'rootfs' || $mountpoint->{backup}) {
-				# backup conf contains the mp, clear for retsore
+			    if ($ms eq 'rootfs' || ($mountpoint->{backup} && !$excludes{$ms})) {
+				# backup conf contains the mp and it is not in exclude list,
+				# clear for retsore
 				$clear_mps->{$ms} = $mountpoint;
 			    } else {
 				# do not add as mp, will be attach as unused at the end
@@ -485,6 +494,7 @@ __PACKAGE__->register_method({
 
 		    my $restore_opts = {
 			'orig_mps' => $orig_mp_param,
+			'excludes' => \%excludes,
 		    };
 		    PVE::LXC::Create::restore_archive(
 			$storage_cfg, $archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit, $restore_opts);
diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 98ab4a4..34929d5 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -129,6 +129,11 @@ sub restore_proxmox_backup_archive {
 	    return;
 	}
 
+	if (defined($restore_opts->{excludes}->{$name})) {
+	    print "'$name': mp excluded from restore.\n";
+	    return;
+	}
+
 	if ($name ne 'rootfs' && (!defined($orig_mp->{backup}) || !$orig_mp->{backup})) {
 	    print "'$name': mp not included in original backup.\n";
 	    return;
@@ -221,6 +226,12 @@ sub restore_tar_archive {
     push @$cmd, '--skip-old-files';
     push @$cmd, '--anchored';
     push @$cmd, '--exclude' , './dev/*';
+    
+    my $orig_mps = $restore_opts->{orig_mps};
+    my $excludes = $restore_opts->{excludes};
+    foreach my $name (keys %$excludes) {
+	push @$cmd, '--exclude', ".$orig_mps->{$name}->{mp}" if defined($orig_mps->{$name}->{mp});
+    }
 
     if (defined($bwlimit)) {
 	$cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
-- 
2.39.2