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 A18597B122
 for <pve-devel@lists.proxmox.com>; Tue, 11 May 2021 15:00:03 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 92F8A22322
 for <pve-devel@lists.proxmox.com>; Tue, 11 May 2021 15:00:03 +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 id D7E38222E6
 for <pve-devel@lists.proxmox.com>; Tue, 11 May 2021 15:00:00 +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 A8DB24625F
 for <pve-devel@lists.proxmox.com>; Tue, 11 May 2021 15:00:00 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 11 May 2021 14:59:52 +0200
Message-Id: <20210511125955.25105-4-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210511125955.25105-1-f.ebner@proxmox.com>
References: <20210511125955.25105-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.002 Adjusted score from AWL reputation of From: address
 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
Subject: [pve-devel] [PATCH zsync 3/6] remove all old snapshots belonging to
 a job
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: Tue, 11 May 2021 13:00:03 -0000

Changing maxsnap to something smaller can lead to left-over snaphsots otherwise,
as previously at most one snapshot would be removed, even if there are multiple
old snapshots according to the new setting.

Hopefully nobody relied on the fact that pve-zsync didn't clean up after itself
in such cases...

Negative values and 0 for 'maxsnap' should still be interpreted as infinity to
match the previous behavior.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---
 pve-zsync | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/pve-zsync b/pve-zsync
index 6ca2da1..ff05f46 100755
--- a/pve-zsync
+++ b/pve-zsync
@@ -685,8 +685,9 @@ sub sync {
 
 	    send_image($source, $dest, $param);
 
-	    snapshot_destroy($source, $dest, $param->{method}, $dest->{old_snap}, $param->{source_user}, $param->{dest_user}) if ($source->{destroy} && $dest->{old_snap});
-
+	    for my $old_snap (@{$dest->{old_snap}}) {
+		snapshot_destroy($source, $dest, $param->{method}, $old_snap, $param->{source_user}, $param->{dest_user});
+	    }
 	};
 
 	eval{
@@ -763,7 +764,7 @@ sub snapshot_get{
     my $index = 0;
     my $line = "";
     my $last_snap = undef;
-    my $old_snap;
+    my $old_snap = [];
 
     while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
 	$line = $1;
@@ -771,12 +772,15 @@ sub snapshot_get{
 	    $last_snap = $1 if (!$last_snap);
 	}
 	if ($line =~ m/(rep_\Q${name}\E_\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2})$/) {
-	    $old_snap = $1;
+	    # interpreted as infinity
+	    last if $max_snap <= 0;
+
+	    my $snap = $1;
 	    $index++;
-	    if ($index == $max_snap) {
-		$source->{destroy} = 1;
-		last;
-	    };
+
+	    if ($index >= $max_snap) {
+		push @{$old_snap}, $snap;
+	    }
 	}
     }
 
@@ -1067,8 +1071,8 @@ sub send_config{
 	    run_cmd(['scp', '--', "$source_user\@[$source->{ip}]:$source_target", $dest_target_new]);
 	}
 
-	if ($source->{destroy}){
-	    my $dest_target_old ="${config_dir}/$source->{vmid}.conf.$source->{vm_type}.$dest->{old_snap}";
+	for my $old_snap (@{$dest->{old_snap}}) {
+	    my $dest_target_old ="${config_dir}/$source->{vmid}.conf.$source->{vm_type}.${old_snap}";
 	    if($dest->{ip}){
 		run_cmd(['ssh', "$dest_user\@$dest->{ip}", '--', 'rm', '-f', '--', $dest_target_old]);
 	    } else {
-- 
2.20.1