From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.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 E277D617B7
 for <pve-devel@lists.proxmox.com>; Thu,  9 Jul 2020 14:46:29 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id D5F42128D4
 for <pve-devel@lists.proxmox.com>; Thu,  9 Jul 2020 14:45:59 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (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 id EACF312875
 for <pve-devel@lists.proxmox.com>; Thu,  9 Jul 2020 14:45: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 AB2144449C
 for <pve-devel@lists.proxmox.com>; Thu,  9 Jul 2020 14:45:55 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu,  9 Jul 2020 14:45:47 +0200
Message-Id: <20200709124547.2913-8-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20200709124547.2913-1-f.ebner@proxmox.com>
References: <20200709124547.2913-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.172 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH v4 manager 7/7] Always use prune-backups instead
 of maxfiles internally
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: Thu, 09 Jul 2020 12:46:29 -0000

For the use case with '--dumpdir', it's not possible to call prune_backups
directly, so a little bit of special handling is required there.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---
 PVE/VZDump.pm | 42 ++++++++++++++++--------------------------
 1 file changed, 16 insertions(+), 26 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 17153fe4..d87ef857 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -484,8 +484,10 @@ sub new {
 	die "internal error";
     }
 
-    if (!defined($opts->{'prune-backups'}) && !defined($opts->{maxfiles})) {
-	$opts->{maxfiles} = $defaults->{maxfiles};
+    if (!defined($opts->{'prune-backups'})) {
+	$opts->{maxfiles} //= $defaults->{maxfiles};
+	$opts->{'prune_backups'} = { 'keep-last' => $opts->{maxfiles} };
+	delete $opts->{maxfiles};
     }
 
     if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
@@ -717,16 +719,11 @@ sub exec_backup_task {
 	my $bkname = "vzdump-$vmtype-$vmid";
 	my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time}));
 
-	my $maxfiles = $opts->{maxfiles};
 	my $prune_options = $opts->{'prune-backups'};
 
 	my $backup_limit = 0;
-	if (defined($maxfiles)) {
-	    $backup_limit = $maxfiles;
-	} elsif (defined($prune_options)) {
-	    foreach my $keep (values %{$prune_options}) {
-		$backup_limit += $keep;
-	    }
+	foreach my $keep (values %{$prune_options}) {
+	    $backup_limit += $keep;
 	}
 
 	if ($backup_limit && !$opts->{remove}) {
@@ -949,25 +946,18 @@ sub exec_backup_task {
 
 	# purge older backup
 	if ($opts->{remove}) {
-	    if ($maxfiles) {
+	    if (!defined($opts->{storage})) {
+		my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{target});
+		PVE::Storage::prune_mark_backup_group($bklist, $prune_options);
 
-		if ($self->{opts}->{pbs}) {
-		    my $args = [$pbs_group_name, '--quiet', '1', '--keep-last', $maxfiles];
-		    my $logfunc = sub { my $line = shift; debugmsg ('info', $line, $logfd); };
-		    PVE::Storage::PBSPlugin::run_raw_client_cmd(
-			$opts->{scfg}, $opts->{storage}, 'prune', $args, logfunc => $logfunc);
-		} else {
-		    my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname, $task->{target});
-		    $bklist = [ sort { $b->{ctime} <=> $a->{ctime} } @$bklist ];
-
-		    while (scalar (@$bklist) >= $maxfiles) {
-			my $d = pop @$bklist;
-			my $archive_path = $d->{path};
-			debugmsg ('info', "delete old backup '$archive_path'", $logfd);
-			PVE::Storage::archive_remove($archive_path);
-		    }
+		foreach my $prune_entry (@{$bklist}) {
+		    next if $prune_entry->{mark} ne 'remove';
+
+		    my $archive_path = $prune_entry->{path};
+		    debugmsg ('info', "delete old backup '$archive_path'", $logfd);
+		    PVE::Storage::archive_remove($archive_path);
 		}
-	    } elsif (defined($prune_options)) {
+	    } else {
 		my $logfunc = sub { debugmsg($_[0], $_[1], $logfd) };
 		PVE::Storage::prune_backups($cfg, $opts->{storage}, $prune_options, $vmid, $vmtype, 0, $logfunc);
 	    }
-- 
2.20.1