]> git.proxmox.com Git - pve-zsync.git/blobdiff - pve-zsync
replace File::Copy::move with built-in rename
[pve-zsync.git] / pve-zsync
old mode 100644 (file)
new mode 100755 (executable)
index 8d083ba..e1ac1e2
--- a/pve-zsync
+++ b/pve-zsync
@@ -5,7 +5,6 @@ use warnings;
 use Data::Dumper qw(Dumper);
 use Fcntl qw(:flock SEEK_END);
 use Getopt::Long qw(GetOptionsFromArray);
-use File::Copy qw(move);
 use File::Path qw(make_path);
 use JSON;
 use IO::File;
@@ -46,10 +45,14 @@ my $HOSTRE = "(?:$HOSTv4RE1|\\[$IPV6RE\\])";       # ipv6 must always be in brac
 # targets are either a VMID, or a 'host:zpool/path' with 'host:' being optional
 my $TARGETRE = qr!^(?:($HOSTRE):)?(\d+|(?:[\w\-_]+)(/.+)?)$!;
 
-check_bin ('cstream');
-check_bin ('zfs');
-check_bin ('ssh');
-check_bin ('scp');
+my $command = $ARGV[0];
+
+if (defined($command) && $command ne 'help' && $command ne 'printpod') {
+    check_bin ('cstream');
+    check_bin ('zfs');
+    check_bin ('ssh');
+    check_bin ('scp');
+}
 
 $SIG{TERM} = $SIG{QUIT} = $SIG{PIPE} = $SIG{HUP} = $SIG{KILL} = $SIG{INT} =
     sub {
@@ -120,12 +123,12 @@ sub get_status {
 }
 
 sub check_pool_exists {
-    my ($target) = @_;
+    my ($target, $user) = @_;
 
     my $cmd = [];
 
     if ($target->{ip}) {
-       push @$cmd, 'ssh', "root\@$target->{ip}", '--';
+       push @$cmd, 'ssh', "$user\@$target->{ip}", '--';
     }
     push @$cmd, 'zfs', 'list', '-H', '--', $target->{all};
     eval {
@@ -197,33 +200,42 @@ sub read_cron {
 sub parse_argv {
     my (@arg) = @_;
 
-    my $param = {};
-    $param->{dest} = undef;
-    $param->{source} = undef;
-    $param->{verbose} = undef;
-    $param->{limit} = undef;
-    $param->{maxsnap} = undef;
-    $param->{name} = undef;
-    $param->{skip} = undef;
-    $param->{method} = undef;
-
-    my ($ret, $ar) = GetOptionsFromArray(\@arg,
-                                        'dest=s' => \$param->{dest},
-                                        'source=s' => \$param->{source},
-                                        'verbose' => \$param->{verbose},
-                                        'limit=i' => \$param->{limit},
-                                        'maxsnap=i' => \$param->{maxsnap},
-                                        'name=s' => \$param->{name},
-                                        'skip' => \$param->{skip},
-                                        'method=s' => \$param->{method});
-
-    if ($ret == 0) {
-       die "can't parse options\n";
-    }
+    my $param = {
+       dest => undef,
+       source => undef,
+       verbose => undef,
+       limit => undef,
+       maxsnap => undef,
+       name => undef,
+       skip => undef,
+       method => undef,
+       source_user => undef,
+       dest_user => undef,
+       properties => undef,
+    };
 
-    $param->{name} = "default" if !$param->{name};
-    $param->{maxsnap} = 1 if !$param->{maxsnap};
-    $param->{method} = "ssh" if !$param->{method};
+    my ($ret) = GetOptionsFromArray(
+       \@arg,
+       'dest=s' => \$param->{dest},
+       'source=s' => \$param->{source},
+       'verbose' => \$param->{verbose},
+       'limit=i' => \$param->{limit},
+       'maxsnap=i' => \$param->{maxsnap},
+       'name=s' => \$param->{name},
+       'skip' => \$param->{skip},
+       'method=s' => \$param->{method},
+       'source-user=s' => \$param->{source_user},
+       'dest-user=s' => \$param->{dest_user},
+       'properties' => \$param->{properties},
+    );
+
+    die "can't parse options\n" if $ret == 0;
+
+    $param->{name} //= "default";
+    $param->{maxsnap} //= 1;
+    $param->{method} //= "ssh";
+    $param->{source_user} //= "root";
+    $param->{dest_user} //= "root";
 
     return $param;
 }
@@ -256,12 +268,10 @@ sub encode_cron {
        my $param = parse_argv(@arg);
 
        if ($param->{source} && $param->{dest}) {
-           $cfg->{$param->{source}}->{$param->{name}}->{dest} = $param->{dest};
-           $cfg->{$param->{source}}->{$param->{name}}->{verbose} = $param->{verbose};
-           $cfg->{$param->{source}}->{$param->{name}}->{limit} = $param->{limit};
-           $cfg->{$param->{source}}->{$param->{name}}->{maxsnap} = $param->{maxsnap};
-           $cfg->{$param->{source}}->{$param->{name}}->{skip} = $param->{skip};
-           $cfg->{$param->{source}}->{$param->{name}}->{method} = $param->{method};
+           my $source = delete $param->{source};
+           my $name = delete $param->{name};
+
+           $cfg->{$source}->{$name} = $param;
        }
     }
 
@@ -283,6 +293,9 @@ sub param_to_job {
     $job->{limit} = $param->{limit};
     $job->{maxsnap} = $param->{maxsnap} if $param->{maxsnap};
     $job->{source} = $param->{source};
+    $job->{source_user} = $param->{source_user};
+    $job->{dest_user} = $param->{dest_user};
+    $job->{properties} = !!$param->{properties};
 
     return $job;
 }
@@ -351,7 +364,7 @@ sub update_state {
     print $out_fh $text;
 
     close($out_fh);
-    move("$STATE.new", $STATE);
+    rename "$STATE.new", $STATE;
     eval {
        close($in_fh);
     };
@@ -403,7 +416,7 @@ sub update_cron {
     die "can't write to $CRONJOBS.new\n" if !print($new_fh $text);
     close ($new_fh);
 
-    die "can't move $CRONJOBS.new: $!\n" if !move("${CRONJOBS}.new", "$CRONJOBS");
+    die "can't move $CRONJOBS.new: $!\n" if !rename "${CRONJOBS}.new", $CRONJOBS;
     close ($fh);
 }
 
@@ -415,7 +428,7 @@ sub format_job {
        $text = "#";
     }
     if ($line) {
-       $line =~ /^#*(.+) root/;
+       $line =~ /^#*\s*((?:\S+\s+){4}\S+)\s+root/;
        $text .= $1;
     } else {
        $text .= "*/$INTERVAL * * * *";
@@ -426,6 +439,9 @@ sub format_job {
     $text .= " --limit $job->{limit}" if $job->{limit};
     $text .= " --method $job->{method}";
     $text .= " --verbose" if $job->{verbose};
+    $text .= " --source-user $job->{source_user}";
+    $text .= " --dest-user $job->{dest_user}";
+    $text .= " --properties" if $job->{properties};
     $text .= "\n";
 
     return $text;
@@ -453,21 +469,20 @@ sub list {
 }
 
 sub vm_exists {
-    my ($target) = @_;
-
-    my @cmd = ('ssh', "root\@$target->{ip}", '--') if $target->{ip};
-
-    my $res = undef;
+    my ($target, $user) = @_;
 
     return undef if !defined($target->{vmid});
 
-    eval { $res = run_cmd([@cmd, 'ls',  "$QEMU_CONF/$target->{vmid}.conf"]) };
+    my $conf_fn = "$target->{vmid}.conf";
 
-    return "qemu" if $res;
-
-    eval { $res = run_cmd([@cmd, 'ls',  "$LXC_CONF/$target->{vmid}.conf"]) };
-
-    return "lxc" if $res;
+    if ($target->{ip}) {
+       my @cmd = ('ssh', "$user\@$target->{ip}", '--', '/bin/ls');
+       return "qemu" if eval { run_cmd([@cmd, "$QEMU_CONF/$conf_fn"]) };
+       return "lxc" if  eval { run_cmd([@cmd, "$LXC_CONF/$conf_fn"]) };
+    } else {
+       return "qemu" if -f "$QEMU_CONF/$conf_fn";
+       return "lxc" if -f "$LXC_CONF/$conf_fn";
+    }
 
     return undef;
 }
@@ -486,20 +501,20 @@ sub init {
     my $dest = parse_target($param->{dest});
 
     if (my $ip =  $dest->{ip}) {
-       run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "root\@$ip"]);
+       run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "$param->{dest_user}\@$ip"]);
     }
 
     if (my $ip =  $source->{ip}) {
-       run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "root\@$ip"]);
+       run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "$param->{source_user}\@$ip"]);
     }
 
-    die "Pool $dest->{all} does not exists\n" if !check_pool_exists($dest);
+    die "Pool $dest->{all} does not exists\n" if !check_pool_exists($dest, $param->{dest_user});
 
     if (!defined($source->{vmid})) {
-       die "Pool $source->{all} does not exists\n" if !check_pool_exists($source);
+       die "Pool $source->{all} does not exists\n" if !check_pool_exists($source, $param->{source_user});
     }
 
-    my $vm_type = vm_exists($source);
+    my $vm_type = vm_exists($source, $param->{source_user});
     $job->{vm_type} = $vm_type;
     $source->{vm_type} = $vm_type;
 
@@ -508,7 +523,7 @@ sub init {
     die "Config already exists\n" if $cfg->{$job->{source}}->{$job->{name}};
 
     #check if vm has zfs disks if not die;
-    get_disks($source) if $source->{vmid};
+    get_disks($source, $param->{source_user}) if $source->{vmid};
 
     update_cron($job);
     update_state($job);
@@ -561,7 +576,7 @@ sub sync {
        $job = get_job($param);
     };
 
-    if ($job && $job->{state} eq "syncing") {
+    if ($job && defined($job->{state}) && $job->{state} eq "syncing") {
        die "Job --source $param->{source} --name $param->{name} is syncing at the moment";
     }
 
@@ -571,17 +586,17 @@ sub sync {
     my $sync_path = sub {
        my ($source, $dest, $job, $param, $date) = @_;
 
-       ($source->{old_snap}, $source->{last_snap}) = snapshot_get($source, $dest, $param->{maxsnap}, $param->{name});
+       ($source->{old_snap}, $source->{last_snap}) = snapshot_get($source, $dest, $param->{maxsnap}, $param->{name}, $param->{source_user});
 
-       snapshot_add($source, $dest, $param->{name}, $date);
+       snapshot_add($source, $dest, $param->{name}, $date, $param->{source_user}, $param->{dest_user});
 
        send_image($source, $dest, $param);
 
-       snapshot_destroy($source, $dest, $param->{method}, $source->{old_snap}) if ($source->{destroy} && $source->{old_snap});
+       snapshot_destroy($source, $dest, $param->{method}, $source->{old_snap}, $param->{source_user}, $param->{dest_user}) if ($source->{destroy} && $source->{old_snap});
 
     };
 
-    my $vm_type = vm_exists($source);
+    my $vm_type = vm_exists($source, $param->{source_user});
     $source->{vm_type} = $vm_type;
 
     if ($job) {
@@ -593,7 +608,8 @@ sub sync {
     eval{
        if ($source->{vmid}) {
            die "VM $source->{vmid} doesn't exist\n" if !$vm_type;
-           my $disks = get_disks($source);
+           die "source-user has to be root for syncing VMs\n" if ($param->{source_user} ne "root");
+           my $disks = get_disks($source, $param->{source_user});
 
            foreach my $disk (sort keys %{$disks}) {
                $source->{all} = $disks->{$disk}->{all};
@@ -603,9 +619,9 @@ sub sync {
                &$sync_path($source, $dest, $job, $param, $date);
            }
            if ($param->{method} eq "ssh" && ($source->{ip} || $dest->{ip})) {
-               send_config($source, $dest,'ssh');
+               send_config($source, $dest,'ssh', $param->{source_user}, $param->{dest_user});
            } else {
-               send_config($source, $dest,'local');
+               send_config($source, $dest,'local', $param->{source_user}, $param->{dest_user});
            }
        } else {
            &$sync_path($source, $dest, $job, $param, $date);
@@ -633,10 +649,10 @@ sub sync {
 }
 
 sub snapshot_get{
-    my ($source, $dest, $max_snap, $name) = @_;
+    my ($source, $dest, $max_snap, $name, $source_user) = @_;
 
     my $cmd = [];
-    push @$cmd, 'ssh', "root\@$source->{ip}", '--', if $source->{ip};
+    push @$cmd, 'ssh', "$source_user\@$source->{ip}", '--', if $source->{ip};
     push @$cmd, 'zfs', 'list', '-r', '-t', 'snapshot', '-Ho', 'name', '-S', 'creation';
     push @$cmd, $source->{all};
 
@@ -666,7 +682,7 @@ sub snapshot_get{
 }
 
 sub snapshot_add {
-    my ($source, $dest, $name, $date) = @_;
+    my ($source, $dest, $name, $date, $source_user, $dest_user) = @_;
 
     my $snap_name = "rep_$name\_".$date;
 
@@ -675,14 +691,14 @@ sub snapshot_add {
     my $path = "$source->{all}\@$snap_name";
 
     my $cmd = [];
-    push @$cmd, 'ssh', "root\@$source->{ip}", '--', if $source->{ip};
+    push @$cmd, 'ssh', "$source_user\@$source->{ip}", '--', if $source->{ip};
     push @$cmd, 'zfs', 'snapshot', $path;
     eval{
        run_cmd($cmd);
     };
 
     if (my $err = $@) {
-       snapshot_destroy($source, $dest, 'ssh', $snap_name);
+       snapshot_destroy($source, $dest, 'ssh', $snap_name, $source_user, $dest_user);
        die "$err\n";
     }
 }
@@ -724,10 +740,10 @@ sub write_cron {
 }
 
 sub get_disks {
-    my ($target) = @_;
+    my ($target, $user) = @_;
 
     my $cmd = [];
-    push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
+    push @$cmd, 'ssh', "$user\@$target->{ip}", '--', if $target->{ip};
 
     if ($target->{vm_type} eq 'qemu') {
        push @$cmd, 'qm', 'config', $target->{vmid};
@@ -739,7 +755,7 @@ sub get_disks {
 
     my $res = run_cmd($cmd);
 
-    my $disks = parse_disks($res, $target->{ip}, $target->{vm_type});
+    my $disks = parse_disks($res, $target->{ip}, $target->{vm_type}, $user);
 
     return $disks;
 }
@@ -762,7 +778,7 @@ sub run_cmd {
 }
 
 sub parse_disks {
-    my ($text, $ip, $vm_type) = @_;
+    my ($text, $ip, $vm_type, $user) = @_;
 
     my $disks;
 
@@ -770,7 +786,7 @@ sub parse_disks {
     while ($text && $text =~ s/^(.*?)(\n|$)//) {
        my $line = $1;
 
-       next if $line =~ /cdrom|none/;
+       next if $line =~ /media=cdrom/;
        next if $line !~ m/^(?:((?:virtio|ide|scsi|sata|mp)\d+)|rootfs): /;
 
        #QEMU if backup is not set include in  sync
@@ -791,17 +807,14 @@ sub parse_disks {
                    last;
                }
            }
-           if (!defined($disk) || !defined($stor)) {
-               print "Disk: \"$line\" has no valid zfs dataset format and will not include in pve-sync\n";
-               next;
-           }
-       } else {
-           print "Disk: \"$line\" has no valid zfs dataset format and will not include in pve-sync\n";
+       }
+       if (!defined($disk) || !defined($stor)) {
+           print "Disk: \"$line\" has no valid zfs dataset format and will be skipped\n";
            next;
        }
 
        my $cmd = [];
-       push @$cmd, 'ssh', "root\@$ip", '--' if $ip;
+       push @$cmd, 'ssh', "$user\@$ip", '--' if $ip;
        push @$cmd, 'pvesm', 'path', "$stor$disk";
        my $path = run_cmd($cmd);
 
@@ -845,14 +858,14 @@ sub parse_disks {
 }
 
 sub snapshot_destroy {
-    my ($source, $dest, $method, $snap) = @_;
+    my ($source, $dest, $method, $snap, $source_user, $dest_user) = @_;
 
     my @zfscmd = ('zfs', 'destroy');
     my $snapshot = "$source->{all}\@$snap";
 
     eval {
        if($source->{ip} && $method eq 'ssh'){
-           run_cmd(['ssh', "root\@$source->{ip}", '--', @zfscmd, $snapshot]);
+           run_cmd(['ssh', "$source_user\@$source->{ip}", '--', @zfscmd, $snapshot]);
        } else {
            run_cmd([@zfscmd, $snapshot]);
        }
@@ -861,9 +874,10 @@ sub snapshot_destroy {
        warn "WARN: $erro";
     }
     if ($dest) {
-       my @ssh = $dest->{ip} ? ('ssh', "root\@$dest->{ip}", '--') : ();
+       my @ssh = $dest->{ip} ? ('ssh', "$dest_user\@$dest->{ip}", '--') : ();
 
-       my $path = "$dest->{all}\/$source->{last_part}";
+       my $path = "$dest->{all}";
+       $path .= "/$source->{last_part}" if $source->{last_part};
 
        eval {
            run_cmd([@ssh, @zfscmd, "$path\@$snap"]);
@@ -875,12 +889,18 @@ sub snapshot_destroy {
 }
 
 sub snapshot_exist {
-    my ($source , $dest, $method) = @_;
+    my ($source , $dest, $method, $dest_user) = @_;
 
     my $cmd = [];
-    push @$cmd, 'ssh', "root\@$dest->{ip}", '--' if $dest->{ip};
+    push @$cmd, 'ssh', "$dest_user\@$dest->{ip}", '--' if $dest->{ip};
     push @$cmd, 'zfs', 'list', '-rt', 'snapshot', '-Ho', 'name';
-    push @$cmd, "$dest->{all}/$source->{last_part}\@$source->{old_snap}";
+
+    my $path = $dest->{all};
+    $path .= "/$source->{last_part}" if $source->{last_part};
+    $path .= "\@$source->{old_snap}";
+
+    push @$cmd, $path;
+
 
     my $text = "";
     eval {$text =run_cmd($cmd);};
@@ -900,11 +920,12 @@ sub send_image {
 
     my $cmd = [];
 
-    push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$source->{ip}", '--' if $source->{ip};
+    push @$cmd, 'ssh', '-o', 'BatchMode=yes', "$param->{source_user}\@$source->{ip}", '--' if $source->{ip};
     push @$cmd, 'zfs', 'send';
+    push @$cmd, '-p', if $param->{properties};
     push @$cmd, '-v' if $param->{verbose};
 
-    if($source->{last_snap} && snapshot_exist($source , $dest, $param->{method})) {
+    if($source->{last_snap} && snapshot_exist($source , $dest, $param->{method}, $param->{dest_user})) {
        push @$cmd, '-i', "$source->{all}\@$source->{last_snap}";
     }
     push @$cmd, '--', "$source->{all}\@$source->{new_snap}";
@@ -913,345 +934,229 @@ sub send_image {
        my $bwl = $param->{limit}*1024;
        push @$cmd, \'|', 'cstream', '-t', $bwl;
     }
-    my $target = "$dest->{all}/$source->{last_part}";
+    my $target = "$dest->{all}";
+    $target .= "/$source->{last_part}" if $source->{last_part};
     $target =~ s!/+!/!g;
 
     push @$cmd, \'|';
-       push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip};
-       push @$cmd, 'zfs', 'recv', '-F', '--';
-       push @$cmd, "$target";
+    push @$cmd, 'ssh', '-o', 'BatchMode=yes', "$param->{dest_user}\@$dest->{ip}", '--' if $dest->{ip};
+    push @$cmd, 'zfs', 'recv', '-F', '--';
+    push @$cmd, "$target";
 
-       eval {
-           run_cmd($cmd)
-       };
+    eval {
+       run_cmd($cmd)
+    };
 
-       if (my $erro = $@) {
-           snapshot_destroy($source, undef, $param->{method}, $source->{new_snap});
-           die $erro;
-       };
-    }
+    if (my $erro = $@) {
+       snapshot_destroy($source, undef, $param->{method}, $source->{new_snap}, $param->{source_user}, $param->{dest_user});
+       die $erro;
+    };
+}
 
 
-    sub send_config{
-       my ($source, $dest, $method) = @_;
+sub send_config{
+    my ($source, $dest, $method, $source_user, $dest_user) = @_;
 
-       my $source_target = $source->{vm_type} eq 'qemu' ? "$QEMU_CONF/$source->{vmid}.conf": "$LXC_CONF/$source->{vmid}.conf";
-       my $dest_target_new ="$source->{vmid}.conf.$source->{vm_type}.$source->{new_snap}";
+    my $source_target = $source->{vm_type} eq 'qemu' ? "$QEMU_CONF/$source->{vmid}.conf": "$LXC_CONF/$source->{vmid}.conf";
+    my $dest_target_new ="$source->{vmid}.conf.$source->{vm_type}.$source->{new_snap}";
 
-       my $config_dir = $dest->{last_part} ?  "${CONFIG_PATH}/$dest->{last_part}" : $CONFIG_PATH;
+    my $config_dir = $dest->{last_part} ?  "${CONFIG_PATH}/$dest->{last_part}" : $CONFIG_PATH;
 
-       $dest_target_new = $config_dir.'/'.$dest_target_new;
+    $dest_target_new = $config_dir.'/'.$dest_target_new;
 
-       if ($method eq 'ssh'){
-           if ($dest->{ip} && $source->{ip}) {
-               run_cmd(['ssh', "root\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]);
-               run_cmd(['scp', '--', "root\@[$source->{ip}]:$source_target", "root\@[$dest->{ip}]:$dest_target_new"]);
-           } elsif ($dest->{ip}) {
-               run_cmd(['ssh', "root\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]);
-               run_cmd(['scp', '--', $source_target, "root\@[$dest->{ip}]:$dest_target_new"]);
-           } elsif ($source->{ip}) {
-               run_cmd(['mkdir', '-p', '--', $config_dir]);
-               run_cmd(['scp', '--', "root\@$source->{ip}:$source_target", $dest_target_new]);
-           }
+    if ($method eq 'ssh'){
+       if ($dest->{ip} && $source->{ip}) {
+           run_cmd(['ssh', "$dest_user\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]);
+           run_cmd(['scp', '--', "$source_user\@[$source->{ip}]:$source_target", "$dest_user\@[$dest->{ip}]:$dest_target_new"]);
+       } elsif ($dest->{ip}) {
+           run_cmd(['ssh', "$dest_user\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]);
+           run_cmd(['scp', '--', $source_target, "$dest_user\@[$dest->{ip}]:$dest_target_new"]);
+       } elsif ($source->{ip}) {
+           run_cmd(['mkdir', '-p', '--', $config_dir]);
+           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}.$source->{old_snap}";
-               if($dest->{ip}){
-                   run_cmd(['ssh', "root\@$dest->{ip}", '--', 'rm', '-f', '--', $dest_target_old]);
-               } else {
-                   run_cmd(['rm', '-f', '--', $dest_target_old]);
-               }
+       if ($source->{destroy}){
+           my $dest_target_old ="${config_dir}/$source->{vmid}.conf.$source->{vm_type}.$source->{old_snap}";
+           if($dest->{ip}){
+               run_cmd(['ssh', "$dest_user\@$dest->{ip}", '--', 'rm', '-f', '--', $dest_target_old]);
+           } else {
+               run_cmd(['rm', '-f', '--', $dest_target_old]);
            }
-       } elsif ($method eq 'local') {
-           run_cmd(['mkdir', '-p', '--', $config_dir]);
-           run_cmd(['cp', $source_target, $dest_target_new]);
        }
+    } elsif ($method eq 'local') {
+       run_cmd(['mkdir', '-p', '--', $config_dir]);
+       run_cmd(['cp', $source_target, $dest_target_new]);
     }
+}
 
-    sub get_date {
-       my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
-       my $datestamp = sprintf ("%04d-%02d-%02d_%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
+sub get_date {
+    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
+    my $datestamp = sprintf ("%04d-%02d-%02d_%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
 
-       return $datestamp;
-    }
+    return $datestamp;
+}
 
-    sub status {
-       my $cfg = read_cron();
+sub status {
+    my $cfg = read_cron();
 
-       my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS");
+    my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS");
 
-       my $states = read_state();
+    my $states = read_state();
 
-       foreach my $source (sort keys%{$cfg}) {
-           foreach my $sync_name (sort keys%{$cfg->{$source}}) {
-               $status_list .= sprintf("%-25s", cut_target_width($source, 25));
-               $status_list .= sprintf("%-25s", cut_target_width($sync_name, 25));
-               $status_list .= "$states->{$source}->{$sync_name}->{state}\n";
-           }
+    foreach my $source (sort keys%{$cfg}) {
+       foreach my $sync_name (sort keys%{$cfg->{$source}}) {
+           $status_list .= sprintf("%-25s", cut_target_width($source, 25));
+           $status_list .= sprintf("%-25s", cut_target_width($sync_name, 25));
+           $status_list .= "$states->{$source}->{$sync_name}->{state}\n";
        }
-
-       return $status_list;
     }
 
-    sub enable_job {
-       my ($param) = @_;
-
-       my $job = get_job($param);
-       $job->{state} = "ok";
-       update_state($job);
-       update_cron($job);
-    }
+    return $status_list;
+}
 
-    sub disable_job {
-       my ($param) = @_;
+sub enable_job {
+    my ($param) = @_;
 
-       my $job = get_job($param);
-       $job->{state} = "stopped";
-       update_state($job);
-       update_cron($job);
-    }
+    my $job = get_job($param);
+    $job->{state} = "ok";
+    update_state($job);
+    update_cron($job);
+}
 
-    my $command = $ARGV[0];
+sub disable_job {
+    my ($param) = @_;
 
-    my $commands = {'destroy' => 1,
-                   'create' => 1,
-                   'sync' => 1,
-                   'list' => 1,
-                   'status' => 1,
-                   'help' => 1,
-                   'enable' => 1,
-                   'disable' => 1};
+    my $job = get_job($param);
+    $job->{state} = "stopped";
+    update_state($job);
+    update_cron($job);
+}
 
-    if (!$command || !$commands->{$command}) {
-       usage();
-       die "\n";
-    }
+my $cmd_help = {
+    destroy => qq{
+$PROGNAME destroy -source <string> [OPTIONS]
 
-    my $help_sync = "$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
-\twill sync one time\n
-\t-dest\tstring\n
-\t\tthe destination target is like [IP:]<Pool>[/Path]\n
-\t-limit\tinteger\n
-\t\tmax sync speed in kBytes/s, default unlimited\n
-\t-maxsnap\tinteger\n
-\t\thow much snapshots will be kept before get erased, default 1/n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default.
-\tIt is only necessary if scheduler allready contains this source.\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
-    my $help_create = "$PROGNAME create -dest <string> -source <string> [OPTIONS]/n
-\tCreate a sync Job\n
-\t-dest\tstring\n
-\t\tthe destination target is like [IP]:<Pool>[/Path]\n
-\t-limit\tinteger\n
-\t\tmax sync speed in kBytes/s, default unlimited\n
-\t-maxsnap\tstring\n
-\t\thow much snapshots will be kept before get erased, default 1\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-skip\tboolean\n
-\t\tif this flag is set it will skip the first sync\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
-    my $help_destroy = "$PROGNAME destroy -source <string> [OPTIONS]\n
-\tremove a sync Job from the scheduler\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an  <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
-    my $help_help = "$PROGNAME help <cmd> [OPTIONS]\n
-\tGet help about specified command.\n
-\t<cmd>\tstring\n
-\t\tCommand name\n
-\t-verbose\tboolean\n
-\t\tVerbose output format.\n";
-
-    my $help_list = "$PROGNAME list\n
-\tGet a List of all scheduled Sync Jobs\n";
-
-    my $help_status = "$PROGNAME status\n
-\tGet the status of all scheduled Sync Jobs\n";
-
-    my $help_enable = "$PROGNAME enable -source <string> [OPTIONS]\n
-\tenable a syncjob and reset error\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an  <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
-    my $help_disable = "$PROGNAME disable -source <string> [OPTIONS]\n
-\tpause a syncjob\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an  <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
-    sub help {
-       my ($command) = @_;
-
-       if ($command eq 'help') {
-           die "$help_help\n";
-
-       } elsif ($command eq 'sync') {
-           die "$help_sync\n";
-
-       } elsif ($command eq 'destroy') {
-           die "$help_destroy\n";
-
-       } elsif ($command eq 'create') {
-           die "$help_create\n";
-
-       } elsif ($command eq 'list') {
-           die "$help_list\n";
-
-       } elsif ($command eq 'status') {
-           die "$help_status\n";
-
-       } elsif ($command eq 'enable') {
-           die "$help_enable\n";
-
-       } elsif ($command eq 'disable') {
-           die "$help_disable\n";
+        remove a sync Job from the scheduler
 
-       }
+        -name      string
 
-    }
+               name of the sync job, if not set it is default
 
-    my @arg = @ARGV;
-    my $param = parse_argv(@arg);
+        -source    string
 
-    if ($command eq 'destroy') {
-       die "$help_destroy\n" if !$param->{source};
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+    },
+    create => qq{
+$PROGNAME create -dest <string> -source <string> [OPTIONS]
 
-       check_target($param->{source});
-       destroy_job($param);
+        Create a sync Job
 
-    } elsif ($command eq 'sync') {
-       die "$help_sync\n" if !$param->{source} || !$param->{dest};
+        -dest      string
 
-       check_target($param->{source});
-       check_target($param->{dest});
-       sync($param);
+               the destination target is like [IP]:<Pool>[/Path]
 
-    } elsif ($command eq 'create') {
-       die "$help_create\n" if !$param->{source} || !$param->{dest};
+        -dest-user string
 
-       check_target($param->{source});
-       check_target($param->{dest});
-       init($param);
+               name of the user on the destination target, root by default
 
-    } elsif ($command eq 'status') {
-       print status();
+        -limit     integer
 
-    } elsif ($command eq 'list') {
-       print list();
+               max sync speed in kBytes/s, default unlimited
 
-    } elsif ($command eq 'help') {
-       my $help_command = $ARGV[1];
+        -maxsnap   string
 
-       if ($help_command && $commands->{$help_command}) {
-           print help($help_command);
+               how much snapshots will be kept before get erased, default 1
 
-       }
-       if ($param->{verbose} == 1){
-           exec("man $PROGNAME");
+        -name      string
 
-       } else {
-           usage(1);
+               name of the sync job, if not set it is default
 
-       }
+        -skip      boolean
 
-    } elsif ($command eq 'enable') {
-       die "$help_enable\n" if !$param->{source};
+               if this flag is set it will skip the first sync
 
-       check_target($param->{source});
-       enable_job($param);
+        -source    string
 
-    } elsif ($command eq 'disable') {
-       die "$help_disable\n" if !$param->{source};
+               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
 
-       check_target($param->{source});
-       disable_job($param);
+        -source-user    string
 
-    }
+               name of the user on the source target, root by default
 
-    sub usage {
-       my ($help) = @_;
-
-       print("ERROR:\tno command specified\n") if !$help;
-       print("USAGE:\t$PROGNAME <COMMAND> [ARGS] [OPTIONS]\n");
-       print("\t$PROGNAME help [<cmd>] [OPTIONS]\n\n");
-       print("\t$PROGNAME create -dest <string> -source <string> [OPTIONS]\n");
-       print("\t$PROGNAME destroy -source <string> [OPTIONS]\n");
-       print("\t$PROGNAME disable -source <string> [OPTIONS]\n");
-       print("\t$PROGNAME enable -source <string> [OPTIONS]\n");
-       print("\t$PROGNAME list\n");
-       print("\t$PROGNAME status\n");
-       print("\t$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n");
-    }
+       -properties     boolean
 
-    sub check_target {
-       my ($target) = @_;
-       parse_target($target);
-    }
+               Include the dataset's properties in the stream.
+    },
+    sync => qq{
+$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
 
-__END__
+       will sync one time
 
-=head1 NAME
+        -dest      string
 
-pve-zsync - PVE ZFS Replication Manager
+               the destination target is like [IP:]<Pool>[/Path]
 
-=head1 SYNOPSIS
+        -dest-user string
 
-pve-zsync <COMMAND> [ARGS] [OPTIONS]
+               name of the user on the destination target, root by default
 
-pve-zsync help <cmd> [OPTIONS]
+       -limit     integer
 
-       Get help about specified command.
+               max sync speed in kBytes/s, default unlimited
 
-        <cmd>      string
+        -maxsnap   integer
 
-               Command name
+               how much snapshots will be kept before get erased, default 1
 
-       -verbose   boolean
+        -name      string
 
-               Verbose output format.
+               name of the sync job, if not set it is default.
+               It is only necessary if scheduler allready contains this source.
 
-pve-zsync create -dest <string> -source <string> [OPTIONS]
+        -source    string
 
-        Create a sync Job
+               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
 
-        -dest      string
+        -source-user    string
 
-               the destination target is like [IP]:<Pool>[/Path]
+               name of the user on the source target, root by default
 
-        -limit     integer
+       -verbose   boolean
 
-               max sync speed in kBytes/s, default unlimited
+               print out the sync progress.
 
-        -maxsnap   string
+       -properties     boolean
 
-               how much snapshots will be kept before get erased, default 1
+               Include the dataset's properties in the stream.
+    },
+    list => qq{
+$PROGNAME list
 
-        -name      string
+       Get a List of all scheduled Sync Jobs
+    },
+    status => qq{
+$PROGNAME status
 
-               name of the sync job, if not set it is default
+       Get the status of all scheduled Sync Jobs
+    },
+    help => qq{
+$PROGNAME help <cmd> [OPTIONS]
 
-        -skip      boolean
+       Get help about specified command.
 
-               if this flag is set it will skip the first sync
+        <cmd>      string
 
-        -source    string
+               Command name
 
-               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+       -verbose   boolean
 
-pve-zsync destroy -source <string> [OPTIONS]
+               Verbose output format.
+    },
+    enable => qq{
+$PROGNAME enable -source <string> [OPTIONS]
 
-        remove a sync Job from the scheduler
+        enable a syncjob and reset error
 
         -name      string
 
@@ -1260,8 +1165,9 @@ pve-zsync destroy -source <string> [OPTIONS]
         -source    string
 
                 the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-
-pve-zsync disable -source <string> [OPTIONS]
+    },
+    disable => qq{
+$PROGNAME disable -source <string> [OPTIONS]
 
         pause a sync job
 
@@ -1271,51 +1177,119 @@ pve-zsync disable -source <string> [OPTIONS]
 
         -source    string
 
-                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path] 
+    },
+    printpod => 'internal command',
 
-pve-zsync enable -source <string> [OPTIONS]
+};
 
-        enable a syncjob and reset error
+if (!$command) {
+    usage(); die "\n";
+} elsif (!$cmd_help->{$command}) {
+    print "ERROR: unknown command '$command'";
+    usage(1); die "\n";
+}
 
-        -name      string
+my @arg = @ARGV;
+my $param = parse_argv(@arg);
 
-               name of the sync job, if not set it is default
+sub check_params {
+    for (@_) {
+       die "$cmd_help->{$command}\n" if !$param->{$_};
+    }
+}
 
-        -source    string
+if ($command eq 'destroy') {
+    check_params(qw(source));
 
-                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-pve-zsync list
+    check_target($param->{source});
+    destroy_job($param);
 
-       Get a List of all scheduled Sync Jobs
+} elsif ($command eq 'sync') {
+    check_params(qw(source dest));
 
-pve-zsync status
+    check_target($param->{source});
+    check_target($param->{dest});
+    sync($param);
 
-       Get the status of all scheduled Sync Jobs
+} elsif ($command eq 'create') {
+    check_params(qw(source dest));
 
-pve-zsync sync -dest <string> -source <string> [OPTIONS]
+    check_target($param->{source});
+    check_target($param->{dest});
+    init($param);
 
-       will sync one time
+} elsif ($command eq 'status') {
+    print status();
 
-        -dest      string
+} elsif ($command eq 'list') {
+    print list();
 
-               the destination target is like [IP:]<Pool>[/Path]
+} elsif ($command eq 'help') {
+    my $help_command = $ARGV[1];
 
-       -limit     integer
+    if ($help_command && $cmd_help->{$help_command}) {
+       die "$cmd_help->{$help_command}\n";
 
-               max sync speed in kBytes/s, default unlimited
+    }
+    if ($param->{verbose}) {
+       exec("man $PROGNAME");
 
-        -maxsnap   integer
+    } else {
+       usage(1);
 
-               how much snapshots will be kept before get erased, default 1
+    }
 
-        -name      string
+} elsif ($command eq 'enable') {
+    check_params(qw(source));
 
-               name of the sync job, if not set it is default.
-               It is only necessary if scheduler allready contains this source.
+    check_target($param->{source});
+    enable_job($param);
 
-        -source    string
+} elsif ($command eq 'disable') {
+    check_params(qw(source));
 
-               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+    check_target($param->{source});
+    disable_job($param);
+
+} elsif ($command eq 'printpod') {
+    print_pod();
+}
+
+sub usage {
+    my ($help) = @_;
+
+    print("ERROR:\tno command specified\n") if !$help;
+    print("USAGE:\t$PROGNAME <COMMAND> [ARGS] [OPTIONS]\n");
+    print("\t$PROGNAME help [<cmd>] [OPTIONS]\n\n");
+    print("\t$PROGNAME create -dest <string> -source <string> [OPTIONS]\n");
+    print("\t$PROGNAME destroy -source <string> [OPTIONS]\n");
+    print("\t$PROGNAME disable -source <string> [OPTIONS]\n");
+    print("\t$PROGNAME enable -source <string> [OPTIONS]\n");
+    print("\t$PROGNAME list\n");
+    print("\t$PROGNAME status\n");
+    print("\t$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n");
+}
+
+sub check_target {
+    my ($target) = @_;
+    parse_target($target);
+}
+
+sub print_pod {
+
+    my $synopsis = join("\n", sort values %$cmd_help);
+
+    print <<EOF;
+=head1 NAME
+
+pve-zsync - PVE ZFS Replication Manager
+
+=head1 SYNOPSIS
+
+pve-zsync <COMMAND> [ARGS] [OPTIONS]
+
+$synopsis
 
 =head1 DESCRIPTION
 
@@ -1356,3 +1330,6 @@ Affero General Public License for more details.
 You should have received a copy of the GNU Affero General Public
 License along with this program.  If not, see
 <http://www.gnu.org/licenses/>.
+
+EOF
+}