]> git.proxmox.com Git - pve-zsync.git/commitdiff
fix #1669: add prepend-storage-id flag
authorFabian Ebner <f.ebner@proxmox.com>
Tue, 4 May 2021 08:10:04 +0000 (10:10 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 4 May 2021 10:12:51 +0000 (12:12 +0200)
so one can set up jobs for guests with disks with the same name on different
storages. For example,
    storageA:vm-123-disk-0
    storageB:vm-123-disk-0
will be synced to
    target/storageA/vm-123-disk-0
    target/storageB/vm-123-disk-0
when the flag is specified.

The necessary parent file systems (one per storage ID) are created as needed.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
pve-zsync

index 75d4d7ce33171456e8088ac357457efc17787984..521abf0752828a8d310d25c766a7acb7a7d59b1b 100755 (executable)
--- a/pve-zsync
+++ b/pve-zsync
@@ -249,6 +249,7 @@ sub parse_argv {
        method => undef,
        source_user => undef,
        dest_user => undef,
+       prepend_storage_id => undef,
        properties => undef,
        dest_config_path => undef,
     };
@@ -265,6 +266,7 @@ sub parse_argv {
        'method=s' => \$param->{method},
        'source-user=s' => \$param->{source_user},
        'dest-user=s' => \$param->{dest_user},
+       'prepend-storage-id' => \$param->{prepend_storage_id},
        'properties' => \$param->{properties},
        'dest-config-path=s' => \$param->{dest_config_path},
     );
@@ -336,6 +338,7 @@ sub param_to_job {
     $job->{source} = $param->{source};
     $job->{source_user} = $param->{source_user};
     $job->{dest_user} = $param->{dest_user};
+    $job->{prepend_storage_id} = !!$param->{prepend_storage_id};
     $job->{properties} = !!$param->{properties};
     $job->{dest_config_path} = $param->{dest_config_path} if $param->{dest_config_path};
 
@@ -461,6 +464,7 @@ sub format_job {
     $text .= " --verbose" if $job->{verbose};
     $text .= " --source-user $job->{source_user}";
     $text .= " --dest-user $job->{dest_user}";
+    $text .= " --prepend-storage-id" if $job->{prepend_storage_id};
     $text .= " --properties" if $job->{properties};
     $text .= " --dest-config-path $job->{dest_config_path}" if $job->{dest_config_path};
     $text .= "\n";
@@ -674,6 +678,8 @@ sub sync {
 
            ($dest->{old_snap}, $dest->{last_snap}) = snapshot_get($source, $dest, $param->{maxsnap}, $param->{name}, $param->{dest_user});
 
+           prepare_prepended_target($source, $dest, $param->{dest_user}) if defined($dest->{prepend});
+
            snapshot_add($source, $dest, $param->{name}, $date, $param->{source_user}, $param->{dest_user});
 
            send_image($source, $dest, $param);
@@ -693,6 +699,10 @@ sub sync {
                    $source->{pool} = $disks->{$disk}->{pool};
                    $source->{path} = $disks->{$disk}->{path} if $disks->{$disk}->{path};
                    $source->{last_part} = $disks->{$disk}->{last_part};
+
+                   $dest->{prepend} = $disks->{$disk}->{storage_id}
+                       if $param->{prepend_storage_id};
+
                    &$sync_path($source, $dest, $job, $param, $date);
                }
                if ($param->{method} eq "ssh" && ($source->{ip} || $dest->{ip})) {
@@ -921,12 +931,30 @@ sub target_dataset {
     my ($source, $dest) = @_;
 
     my $target = "$dest->{all}";
+    $target .= "/$dest->{prepend}" if defined($dest->{prepend});
     $target .= "/$source->{last_part}" if $source->{last_part};
     $target =~ s!/+!/!g;
 
     return $target;
 }
 
+# create the parent dataset for the actual target
+sub prepare_prepended_target {
+    my ($source, $dest, $dest_user) = @_;
+
+    die "internal error - not a prepended target\n" if !defined($dest->{prepend});
+
+    # The parent dataset shouldn't be the actual target.
+    die "internal error - no last_part for source\n" if !$source->{last_part};
+
+    my $target = "$dest->{all}/$dest->{prepend}";
+    $target =~ s!/+!/!g;
+
+    return if check_dataset_exists($target, $dest->{ip}, $dest_user);
+
+    create_file_system($target, $dest->{ip}, $dest_user);
+}
+
 sub snapshot_destroy {
     my ($source, $dest, $method, $snap, $source_user, $dest_user) = @_;
 
@@ -1138,6 +1166,10 @@ $PROGNAME create -dest <string> -source <string> [OPTIONS]
 
                name of the sync job, if not set it is default
 
+        -prepend-storage-id
+
+               If specified, prepend the storage ID to the destination's path(s).
+
         -skip
 
                If specified, skip the first sync.
@@ -1184,6 +1216,10 @@ $PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
                name of the sync job, if not set it is default.
                It is only necessary if scheduler allready contains this source.
 
+        -prepend-storage-id
+
+               If specified, prepend the storage ID to the destination's path(s).
+
         -source    string
 
                the source can be an <VMID> or [IP:]<ZFSPool>[/Path]