]> git.proxmox.com Git - pve-zsync.git/commitdiff
remove all old snapshots belonging to a job
authorFabian Ebner <f.ebner@proxmox.com>
Tue, 11 May 2021 12:59:52 +0000 (14:59 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 25 May 2021 05:18:50 +0000 (07:18 +0200)
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

index 6ca2da16ef2ffe97b0ed3e2e4c3deed9ef31c393..ff05f467ac65ae89e7e6ff447bdb594c451708f6 100755 (executable)
--- 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 {