From: Fabian Ebner Date: Tue, 11 May 2021 12:59:52 +0000 (+0200) Subject: remove all old snapshots belonging to a job X-Git-Url: https://git.proxmox.com/?p=pve-zsync.git;a=commitdiff_plain;h=42b8fc946ff66c1cb739954a52ae4aa7fd8395d2 remove all old snapshots belonging to a job 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 --- 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 {