[pve-devel] [PATCH v2 manager 13/13] Always use prune-backups instead of maxfiles internally

Fabian Ebner f.ebner at proxmox.com
Wed Jun 10 13:24:04 CEST 2020


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.

Note that $opts->{'prune-backups'} is always defined after new()

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---

New in v2

 PVE/VZDump.pm | 39 ++++++++++++++-------------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 75a72e2e..bc2092b5 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -483,6 +483,8 @@ sub new {
 	$errors .= "cannot have 'maxfiles' and 'prune-backups' configured at the same time";
     } elsif (!defined($opts->{'prune-backups'})) {
 	$opts->{maxfiles} //= $defaults->{maxfiles};
+	$opts->{'prune_backups'} = { 'keep-last' => $opts->{maxfiles} };
+	delete $opts->{maxfiles};
     }
 
     if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) {
@@ -714,16 +716,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}) {
@@ -951,26 +948,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->{tarfile});
+		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->{tarfile});
-		    $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';
 
-	    } elsif (defined($prune_options)) {
+		    my $archive_path = $prune_entry->{path};
+		    debugmsg ('info', "delete old backup '$archive_path'", $logfd);
+		    PVE::Storage::archive_remove($archive_path);
+		}
+	    } else {
 		my @prune_list = PVE::Storage::prune_backups($cfg, $opts->{storage}, $prune_options, $vmid, 0);
 		foreach my $prune_entry (@prune_list) {
 		    if ($prune_entry->{mark} eq 'remove') {
-- 
2.20.1





More information about the pve-devel mailing list