]> git.proxmox.com Git - pve-zsync.git/blobdiff - pve-zsync
bump version to 1.7-4
[pve-zsync.git] / pve-zsync
old mode 100644 (file)
new mode 100755 (executable)
index dfb383b..425ffa2
--- a/pve-zsync
+++ b/pve-zsync
@@ -2,38 +2,78 @@
 
 use strict;
 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 Switch;
 use JSON;
 use IO::File;
+use String::ShellQuote 'shell_quote';
 
 my $PROGNAME = "pve-zsync";
-my $CONFIG_PATH = "/var/lib/${PROGNAME}/";
-my $STATE = "${CONFIG_PATH}sync_state";
+my $CONFIG_PATH = "/var/lib/${PROGNAME}";
+my $STATE = "${CONFIG_PATH}/sync_state";
 my $CRONJOBS = "/etc/cron.d/$PROGNAME";
-my $PATH = "/usr/sbin/";
-my $QEMU_CONF = "/etc/pve/local/qemu-server/";
-my $LOCKFILE = "$CONFIG_PATH${PROGNAME}.lock";
-my $PROG_PATH = "$PATH${PROGNAME}";
+my $PATH = "/usr/sbin";
+my $PVE_DIR = "/etc/pve/local";
+my $QEMU_CONF = "${PVE_DIR}/qemu-server";
+my $LXC_CONF = "${PVE_DIR}/lxc";
+my $LOCKFILE = "$CONFIG_PATH/${PROGNAME}.lock";
+my $PROG_PATH = "$PATH/${PROGNAME}";
 my $INTERVAL = 15;
-my $DEBUG = 0;
+my $DEBUG;
+
+BEGIN {
+    $DEBUG = 0; # change default here. not above on declaration!
+    $DEBUG ||= $ENV{ZSYNC_DEBUG};
+    if ($DEBUG) {
+       require Data::Dumper;
+       Data::Dumper->import();
+    }
+}
+
+my $IPV4OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
+my $IPV4RE = "(?:(?:$IPV4OCTET\\.){3}$IPV4OCTET)";
+my $IPV6H16 = "(?:[0-9a-fA-F]{1,4})";
+my $IPV6LS32 = "(?:(?:$IPV4RE|$IPV6H16:$IPV6H16))";
+
+my $IPV6RE = "(?:" .
+    "(?:(?:" .                             "(?:$IPV6H16:){6})$IPV6LS32)|" .
+    "(?:(?:" .                           "::(?:$IPV6H16:){5})$IPV6LS32)|" .
+    "(?:(?:(?:" .              "$IPV6H16)?::(?:$IPV6H16:){4})$IPV6LS32)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,1}$IPV6H16)?::(?:$IPV6H16:){3})$IPV6LS32)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,2}$IPV6H16)?::(?:$IPV6H16:){2})$IPV6LS32)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,3}$IPV6H16)?::(?:$IPV6H16:){1})$IPV6LS32)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,4}$IPV6H16)?::" .           ")$IPV6LS32)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,5}$IPV6H16)?::" .            ")$IPV6H16)|" .
+    "(?:(?:(?:(?:$IPV6H16:){0,6}$IPV6H16)?::" .                    ")))";
+
+my $HOSTv4RE0 = "(?:[\\w\\.\\-_]+|$IPV4RE)";       # hostname or ipv4 address
+my $HOSTv4RE1 = "(?:$HOSTv4RE0|\\[$HOSTv4RE0\\])"; # these may be in brackets, too
+my $HOSTRE = "(?:$HOSTv4RE1|\\[$IPV6RE\\])";       # ipv6 must always be in brackets
+# targets are either a VMID, or a 'host:zpool/path' with 'host:' being optional
+my $TARGETRE = qr!^(?:($HOSTRE):)?(\d+|(?:[\w\-_]+)(/.+)?)$!;
+
+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');
+}
 
-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 {
+    die "Signaled, aborting sync: $!\n";
+};
 
 sub check_bin {
     my ($bin)  = @_;
 
     foreach my $p (split (/:/, $ENV{PATH})) {
-        my $fn = "$p/$bin";
-        if (-x $fn) {
-            return $fn;
+       my $fn = "$p/$bin";
+       if (-x $fn) {
+           return $fn;
        }
     }
 
@@ -41,21 +81,33 @@ sub check_bin {
 }
 
 sub cut_target_width {
-    my ($target, $max) = @_;
+    my ($path, $maxlen) = @_;
+    $path =~ s@/+@/@g;
 
-    return  $target if (length($target) <= $max);
-    my @spl = split('/', $target);
+    return $path if length($path) <= $maxlen;
 
-    my $count = length($spl[@spl-1]);
-    return "..\/".substr($spl[@spl-1],($count-$max)+3 ,$count) if $count > $max;
+    return '..'.substr($path, -$maxlen+2) if $path !~ m@/@;
 
-    $count +=  length($spl[0]) if @spl > 1;
-    return substr($spl[0], 0, $max-4-length($spl[@spl-1]))."\/..\/".$spl[@spl-1] if $count > $max;
+    $path =~ s@/([^/]+/?)$@@;
+    my $tail = $1;
 
-    my $rest = 1;
-    $rest = $max-$count if ($max-$count > 0);
+    if (length($tail)+3 == $maxlen) {
+       return "../$tail";
+    } elsif (length($tail)+2 >= $maxlen) {
+       return '..'.substr($tail, -$maxlen+2)
+    }
+
+    $path =~ s@(/[^/]+)(?:/|$)@@;
+    my $head = $1;
+    my $both = length($head) + length($tail);
+    my $remaining = $maxlen-$both-4; # -4 for "/../"
+
+    if ($remaining < 0) {
+       return substr($head, 0, $maxlen - length($tail) - 3) . "../$tail"; # -3 for "../"
+    }
 
-    return "$spl[0]".substr($target, length($spl[0]), $rest)."..\/".$spl[@spl-1];
+    substr($path, ($remaining/2), (length($path)-$remaining), '..');
+    return "$head/" . $path . "/$tail";
 }
 
 sub lock {
@@ -79,19 +131,22 @@ sub get_status {
 }
 
 sub check_pool_exists {
-    my ($target) = @_;
+    my ($target, $user) = @_;
+
+    my $cmd = [];
 
-    my $cmd = '';
-    $cmd = "ssh root\@$target->{ip} " if $target->{ip};
-    $cmd .= "zfs list $target->{all} -H";
+    if ($target->{ip}) {
+       push @$cmd, 'ssh', "$user\@$target->{ip}", '--';
+    }
+    push @$cmd, 'zfs', 'list', '-H', '--', $target->{all};
     eval {
        run_cmd($cmd);
     };
 
     if ($@) {
-       return 1;
+       return 0;
     }
-    return undef;
+    return 1;
 }
 
 sub parse_target {
@@ -100,20 +155,20 @@ sub parse_target {
     my $errstr = "$text : is not a valid input! Use [IP:]<VMID> or [IP:]<ZFSPool>[/Path]";
     my $target = {};
 
-    $text =~ m/^((\d+.\d+.\d+.\d+):)?(.*)$/;
-
-    $target->{all} = $3;
-
-    if ($2) {
-       $target->{ip} = $2;
+    if ($text !~ $TARGETRE) {
+       die "$errstr\n";
     }
-    my @parts = split('/', $3);
+    $target->{all} = $2;
+    $target->{ip} = $1 if $1;
+    my @parts = split('/', $2);
+
+    $target->{ip} =~ s/^\[(.*)\]$/$1/ if $target->{ip};
 
-    die "$text $errstr\n" if !($target->{pool} = shift(@parts));
-    die "$text $errstr\n" if $target->{pool} =~ /^(\d+.\d+.\d+.\d+)$/;
+    my $pool = $target->{pool} = shift(@parts);
+    die "$errstr\n" if !$pool;
 
-    if ($target->{pool} =~ m/^\d+$/) {
-       $target->{vmid} = $target->{pool};
+    if ($pool =~ m/^\d+$/) {
+       $target->{vmid} = $pool;
        delete $target->{pool};
     }
 
@@ -153,33 +208,44 @@ 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,
+       dest_config_path => 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},
+       'dest-config-path=s' => \$param->{dest_config_path},
+    );
+
+    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;
 }
@@ -192,6 +258,7 @@ sub add_state_to_job {
 
     $job->{state} = $state->{state};
     $job->{lsync} = $state->{lsync};
+    $job->{vm_type} = $state->{vm_type};
 
     for (my $i = 0; $state->{"snap$i"}; $i++) {
        $job->{"snap$i"} = $state->{"snap$i"};
@@ -211,12 +278,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;
        }
     }
 
@@ -238,6 +303,10 @@ 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};
+    $job->{dest_config_path} = $param->{dest_config_path} if $param->{dest_config_path};
 
     return $job;
 }
@@ -290,6 +359,7 @@ sub update_state {
     if ($job->{state} ne "del") {
        $state->{state} = $job->{state};
        $state->{lsync} = $job->{lsync};
+       $state->{vm_type} = $job->{vm_type};
 
        for (my $i = 0; $job->{"snap$i"} ; $i++) {
            $state->{"snap$i"} = $job->{"snap$i"};
@@ -305,7 +375,7 @@ sub update_state {
     print $out_fh $text;
 
     close($out_fh);
-    move("$STATE.new", $STATE);
+    rename "$STATE.new", $STATE;
     eval {
        close($in_fh);
     };
@@ -349,7 +419,7 @@ sub update_cron {
     }
 
     if (!$updated) {
-        $text .= format_job($job);
+       $text .= format_job($job);
     }
     my $new_fh = IO::File->new("> ${CRONJOBS}.new");
     die "Could not open file ${CRONJOBS}.new: $!\n" if !$new_fh;
@@ -357,7 +427,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);
 }
 
@@ -369,7 +439,7 @@ sub format_job {
        $text = "#";
     }
     if ($line) {
-       $line =~ /^#*(.+) root/;
+       $line =~ /^#*\s*((?:\S+\s+){4}\S+)\s+root/;
        $text .= $1;
     } else {
        $text .= "*/$INTERVAL * * * *";
@@ -377,8 +447,13 @@ sub format_job {
     $text .= " root";
     $text .= " $PROGNAME sync --source $job->{source} --dest $job->{dest}";
     $text .= " --name $job->{name} --maxsnap $job->{maxsnap}";
+    $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 .= " --dest-config-path $job->{dest_config_path}" if $job->{dest_config_path};
     $text .= "\n";
 
     return $text;
@@ -388,16 +463,17 @@ sub list {
 
     my $cfg = read_cron();
 
-    my $list = sprintf("%-25s%-15s%-7s%-20s%-5s\n" , "SOURCE", "NAME", "STATE", "LAST SYNC", "TYPE");
+    my $list = sprintf("%-25s%-25s%-10s%-20s%-6s%-5s\n" , "SOURCE", "NAME", "STATE", "LAST SYNC", "TYPE", "CON");
 
     my $states = read_state();
     foreach my $source (sort keys%{$cfg}) {
        foreach my $name (sort keys%{$cfg->{$source}}) {
            $list .= sprintf("%-25s", cut_target_width($source, 25));
-           $list .= sprintf("%-15s", cut_target_width($name, 15));
-           $list .= sprintf("%-7s", $states->{$source}->{$name}->{state});
-           $list .= sprintf("%-20s",$states->{$source}->{$name}->{lsync});
-           $list .= sprintf("%-5s\n",$cfg->{$source}->{$name}->{method});
+           $list .= sprintf("%-25s", cut_target_width($name, 25));
+           $list .= sprintf("%-10s", $states->{$source}->{$name}->{state});
+           $list .= sprintf("%-20s", $states->{$source}->{$name}->{lsync});
+           $list .= sprintf("%-6s", defined($states->{$source}->{$name}->{vm_type}) ? $states->{$source}->{$name}->{vm_type} : "undef");
+           $list .= sprintf("%-5s\n", $cfg->{$source}->{$name}->{method});
        }
     }
 
@@ -405,15 +481,21 @@ sub list {
 }
 
 sub vm_exists {
-    my ($target) = @_;
+    my ($target, $user) = @_;
 
-    my $cmd = "";
-    $cmd = "ssh root\@$target->{ip} " if ($target->{ip});
-    $cmd .= "qm status $target->{vmid}";
+    return undef if !defined($target->{vmid});
 
-    my $res = run_cmd($cmd);
+    my $conf_fn = "$target->{vmid}.conf";
+
+    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 1 if ($res =~ m/^status.*$/);
     return undef;
 }
 
@@ -431,23 +513,30 @@ 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});
 
-    my $check = check_pool_exists($source->{path}, $source->{ip}) if !$source->{vmid} && $source->{path};
+    if (!defined($source->{vmid})) {
+       die "Pool $source->{all} does not exists\n" if !check_pool_exists($source, $param->{source_user});
+    }
 
-    die "Pool $source->{path} does not exists\n" if undef($check);
+    my $vm_type = vm_exists($source, $param->{source_user});
+    $job->{vm_type} = $vm_type;
+    $source->{vm_type} = $vm_type;
 
-    die "VM $source->{vmid} doesn't exist\n" if $param->{vmid} && !vm_exists($source);
+    die "VM $source->{vmid} doesn't exist\n" if $source->{vmid} && !$vm_type;
 
     die "Config already exists\n" if $cfg->{$job->{source}}->{$job->{name}};
 
+    #check if vm has zfs disks if not die;
+    get_disks($source, $param->{source_user}) if $source->{vmid};
+
     update_cron($job);
     update_state($job);
 
@@ -499,7 +588,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";
     }
 
@@ -509,25 +598,30 @@ 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, $param->{source_user});
+    $source->{vm_type} = $vm_type;
+
     if ($job) {
        $job->{state} = "syncing";
+       $job->{vm_type} = $vm_type if !$job->{vm_type};
        update_state($job);
     }
 
     eval{
        if ($source->{vmid}) {
-           die "VM $source->{vmid} doesn't exist\n" if !vm_exists($source);
-           my $disks = get_disks($source);
+           die "VM $source->{vmid} doesn't exist\n" if !$vm_type;
+           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};
@@ -536,8 +630,10 @@ sub sync {
                $source->{last_part} = $disks->{$disk}->{last_part};
                &$sync_path($source, $dest, $job, $param, $date);
            }
-           if ($param->{method} eq "ssh") {
-               send_config($source, $dest,'ssh');
+           if ($param->{method} eq "ssh" && ($source->{ip} || $dest->{ip})) {
+               send_config($source, $dest,'ssh', $param->{source_user}, $param->{dest_user}, $param->{dest_config_path});
+           } else {
+               send_config($source, $dest,'local', $param->{source_user}, $param->{dest_user}, $param->{dest_config_path});
            }
        } else {
            &$sync_path($source, $dest, $job, $param, $date);
@@ -565,12 +661,12 @@ sub sync {
 }
 
 sub snapshot_get{
-    my ($source, $dest, $max_snap, $name) = @_;
+    my ($source, $dest, $max_snap, $name, $source_user) = @_;
 
-    my $cmd = "zfs list -r -t snapshot -Ho name, -S creation ";
-
-    $cmd .= $source->{all};
-    $cmd = "ssh root\@$source->{ip} ".$cmd if $source->{ip};
+    my $cmd = [];
+    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};
 
     my $raw = run_cmd($cmd);
     my $index = 0;
@@ -580,7 +676,8 @@ sub snapshot_get{
 
     while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
        $line = $1;
-       if ($line =~ m/(rep_$name.*)$/) {
+       if ($line =~ m/(rep_\Q${name}\E_\d{4}-\d{2}-\d{2}_\d{2}:\d{2}:\d{2})$/) {
+
            $last_snap = $1 if (!$last_snap);
            $old_snap = $1;
            $index++;
@@ -597,7 +694,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;
 
@@ -605,15 +702,15 @@ sub snapshot_add {
 
     my $path = "$source->{all}\@$snap_name";
 
-    my $cmd = "zfs snapshot $path";
-    $cmd = "ssh root\@$source->{ip}  $cmd" if $source->{ip};
-
+    my $cmd = [];
+    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";
     }
 }
@@ -630,24 +727,24 @@ sub write_cron {
     foreach my $source (sort keys%{$cfg}) {
        foreach my $sync_name (sort keys%{$cfg->{$source}}) {
            next if $cfg->{$source}->{$sync_name}->{status} ne 'ok';
-       $text .= "$PROG_PATH sync";
-       $text .= " -source  ";
-       if ($cfg->{$source}->{$sync_name}->{vmid}) {
-           $text .= "$cfg->{$source}->{$sync_name}->{source_ip}:" if $cfg->{$source}->{$sync_name}->{source_ip};
-           $text .= "$cfg->{$source}->{$sync_name}->{vmid} ";
-       } else {
-           $text .= "$cfg->{$source}->{$sync_name}->{source_ip}:" if $cfg->{$source}->{$sync_name}->{source_ip};
-           $text .= "$cfg->{$source}->{$sync_name}->{source_pool}";
-           $text .= "$cfg->{$source}->{$sync_name}->{source_path}" if $cfg->{$source}->{$sync_name}->{source_path};
-       }
-       $text .= " -dest  ";
-       $text .= "$cfg->{$source}->{$sync_name}->{dest_ip}:" if $cfg->{$source}->{$sync_name}->{dest_ip};
-       $text .= "$cfg->{$source}->{$sync_name}->{dest_pool}";
-       $text .= "$cfg->{$source}->{$sync_name}->{dest_path}" if $cfg->{$source}->{$sync_name}->{dest_path};
-       $text .= " -name $sync_name ";
-       $text .= " -limit $cfg->{$source}->{$sync_name}->{limit}" if $cfg->{$source}->{$sync_name}->{limit};
-       $text .= " -maxsnap $cfg->{$source}->{$sync_name}->{maxsnap}" if $cfg->{$source}->{$sync_name}->{maxsnap};
-       $text .= "\n";
+           $text .= "$PROG_PATH sync";
+           $text .= " -source  ";
+           if ($cfg->{$source}->{$sync_name}->{vmid}) {
+               $text .= "$cfg->{$source}->{$sync_name}->{source_ip}:" if $cfg->{$source}->{$sync_name}->{source_ip};
+               $text .= "$cfg->{$source}->{$sync_name}->{vmid} ";
+           } else {
+               $text .= "$cfg->{$source}->{$sync_name}->{source_ip}:" if $cfg->{$source}->{$sync_name}->{source_ip};
+               $text .= "$cfg->{$source}->{$sync_name}->{source_pool}";
+               $text .= "$cfg->{$source}->{$sync_name}->{source_path}" if $cfg->{$source}->{$sync_name}->{source_path};
+           }
+           $text .= " -dest  ";
+           $text .= "$cfg->{$source}->{$sync_name}->{dest_ip}:" if $cfg->{$source}->{$sync_name}->{dest_ip};
+           $text .= "$cfg->{$source}->{$sync_name}->{dest_pool}";
+           $text .= "$cfg->{$source}->{$sync_name}->{dest_path}" if $cfg->{$source}->{$sync_name}->{dest_path};
+           $text .= " -name $sync_name ";
+           $text .= " -limit $cfg->{$source}->{$sync_name}->{limit}" if $cfg->{$source}->{$sync_name}->{limit};
+           $text .= " -maxsnap $cfg->{$source}->{$sync_name}->{maxsnap}" if $cfg->{$source}->{$sync_name}->{maxsnap};
+           $text .= "\n";
        }
     }
     die "Can't write to cron\n" if (!print($fh $text));
@@ -655,15 +752,22 @@ sub write_cron {
 }
 
 sub get_disks {
-    my ($target) = @_;
+    my ($target, $user) = @_;
 
-    my $cmd = "";
-    $cmd = "ssh root\@$target->{ip} " if $target->{ip};
-    $cmd .= "qm config $target->{vmid}";
+    my $cmd = [];
+    push @$cmd, 'ssh', "$user\@$target->{ip}", '--', if $target->{ip};
+
+    if ($target->{vm_type} eq 'qemu') {
+       push @$cmd, 'qm', 'config', $target->{vmid};
+    } elsif ($target->{vm_type} eq 'lxc') {
+       push @$cmd, 'pct', 'config', $target->{vmid};
+    } else {
+       die "VM Type unknown\n";
+    }
 
     my $res = run_cmd($cmd);
 
-    my $disks = parse_disks($res, $target->{ip});
+    my $disks = parse_disks($res, $target->{ip}, $target->{vm_type}, $user);
 
     return $disks;
 }
@@ -672,6 +776,9 @@ sub run_cmd {
     my ($cmd) = @_;
     print "Start CMD\n" if $DEBUG;
     print Dumper $cmd if $DEBUG;
+    if (ref($cmd) eq 'ARRAY') {
+       $cmd = join(' ', map { ref($_) ? $$_ : shell_quote($_) } @$cmd);
+    }
     my $output = `$cmd 2>&1`;
 
     die "COMMAND:\n\t$cmd\nGET ERROR:\n\t$output" if 0 != $?;
@@ -683,7 +790,7 @@ sub run_cmd {
 }
 
 sub parse_disks {
-    my ($text, $ip) = @_;
+    my ($text, $ip, $vm_type, $user) = @_;
 
     my $disks;
 
@@ -691,79 +798,101 @@ sub parse_disks {
     while ($text && $text =~ s/^(.*?)(\n|$)//) {
        my $line = $1;
 
-       next if $line =~ /cdrom|none/;
-       next if $line !~ m/^(?:virtio|ide|scsi|sata)\d+: /;
+       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
+       next if $vm_type eq 'qemu' && ($line =~ m/backup=(?i:0|no|off|false)/);
+
+       #LXC if backup is not set do no in sync
+       next if $vm_type eq 'lxc' && ($line =~ m/^mp\d:/) && ($line !~ m/backup=(?i:1|yes|on|true)/);
 
        my $disk = undef;
        my $stor = undef;
-       if($line =~ m/^(virtio\d+: )(.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $3;
-           $stor = $2;
-       } elsif($line =~ m/^(ide\d+: )(.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $3;
-           $stor = $2;
-       } elsif($line =~ m/^(scsi\d+: )(.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $3;
-           $stor = $2;
-       } elsif($line =~ m/^(sata\d+: )(.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $3;
-           $stor = $2;
-       } else {
-           die "disk is not on ZFS Storage\n";
+       if($line =~ m/^(?:(?:(?:virtio|ide|scsi|sata|mp)\d+)|rootfs): (.*)$/) {
+           my @parameter = split(/,/,$1);
+
+           foreach my $opt (@parameter) {
+               if ($opt =~ m/^(?:file=|volume=)?([^:]+:)([A-Za-z0-9\-]+)$/){
+                   $disk = $2;
+                   $stor = $1;
+                   last;
+               }
+           }
+       }
+       if (!defined($disk) || !defined($stor)) {
+           print "Disk: \"$line\" has no valid zfs dataset format and will be skipped\n";
+           next;
        }
 
-       if($disk) {
-           my $cmd = "";
-           $cmd .= "ssh root\@$ip " if $ip;
-           $cmd .= "pvesm path $stor$disk";
-           my $path = run_cmd($cmd);
-
-           if ($path =~ m/^\/dev\/zvol\/(\w+).*(\/$disk)$/) {
+       my $cmd = [];
+       push @$cmd, 'ssh', "$user\@$ip", '--' if $ip;
+       push @$cmd, 'pvesm', 'path', "$stor$disk";
+       my $path = run_cmd($cmd);
+
+       die "Get no path from pvesm path $stor$disk\n" if !$path;
+       if ($vm_type eq 'qemu' && $path =~ m/^\/dev\/zvol\/(\w+.*)(\/$disk)$/) {
+
+           my @array = split('/', $1);
+           $disks->{$num}->{pool} = shift(@array);
+           $disks->{$num}->{all} = $disks->{$num}->{pool};
+           if (0 < @array) {
+               $disks->{$num}->{path} = join('/', @array);
+               $disks->{$num}->{all} .= "\/$disks->{$num}->{path}";
+           }
+           $disks->{$num}->{last_part} = $disk;
+           $disks->{$num}->{all} .= "\/$disk";
 
-               my @array = split('/', $1);
-               $disks->{$num}->{pool} = pop(@array);
-               $disks->{$num}->{all} = $disks->{$num}->{pool};
-               if (0 < @array) {
-                   $disks->{$num}->{path} = join('/', @array);
-                   $disks->{$num}->{all} .= "\/$disks->{$num}->{path}";
-               }
-               $disks->{$num}->{last_part} = $disk;
-               $disks->{$num}->{all} .= "\/$disk";
+           $num++;
+       } elsif ($vm_type eq 'lxc' && $path =~ m/^\/(\w+.+)(\/(\w+.*))*(\/$disk)$/) {
 
-               $num++;
+           $disks->{$num}->{pool} = $1;
+           $disks->{$num}->{all} = $disks->{$num}->{pool};
 
-           } else {
-               die "ERROR: in path\n";
+           if ($2) {
+               $disks->{$num}->{path} = $3;
+               $disks->{$num}->{all} .= "\/$disks->{$num}->{path}";
            }
+
+           $disks->{$num}->{last_part} = $disk;
+           $disks->{$num}->{all} .= "\/$disk";
+
+           $num++;
+
+       } else {
+           die "ERROR: in path\n";
        }
     }
 
+    die "Vm include no disk on zfs.\n" if !$disks->{0};
     return $disks;
 }
 
 sub snapshot_destroy {
-    my ($source, $dest, $method, $snap) = @_;
+    my ($source, $dest, $method, $snap, $source_user, $dest_user) = @_;
 
-    my $zfscmd = "zfs destroy ";
+    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");
+           run_cmd([@zfscmd, $snapshot]);
        }
     };
     if (my $erro = $@) {
        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 ");
+           run_cmd([@ssh, @zfscmd, "$path\@$snap"]);
        };
        if (my $erro = $@) {
            warn "WARN: $erro";
@@ -772,22 +901,28 @@ sub snapshot_destroy {
 }
 
 sub snapshot_exist {
-    my ($source ,$dest, $method) = @_;
+    my ($source , $dest, $method, $dest_user) = @_;
+
+    my $cmd = [];
+    push @$cmd, 'ssh', "$dest_user\@$dest->{ip}", '--' if $dest->{ip};
+    push @$cmd, 'zfs', 'list', '-rt', 'snapshot', '-Ho', 'name';
+
+    my $path = $dest->{all};
+    $path .= "/$source->{last_part}" if $source->{last_part};
+    $path .= "\@$source->{old_snap}";
+
+    push @$cmd, $path;
 
-    my $cmd = "";
-    $cmd = "ssh root\@$dest->{ip} " if $dest->{ip};
-    $cmd .= "zfs list -rt snapshot -Ho name $dest->{all}";
-    $cmd .= "\/$source->{last_part}\@$source->{old_snap}";
 
     my $text = "";
     eval {$text =run_cmd($cmd);};
-    if (my $erro = $@) {
+    if (my $erro =$@) {
        warn "WARN: $erro";
        return undef;
     }
 
     while ($text && $text =~ s/^(.*?)(\n|$)//) {
-       my $line = $1;
+       my $line =$1;
        return 1 if $line =~ m/^.*$source->{old_snap}$/;
     }
 }
@@ -795,64 +930,76 @@ sub snapshot_exist {
 sub send_image {
     my ($source, $dest, $param) = @_;
 
-    my $cmd = "";
+    my $cmd = [];
 
-    $cmd .= "ssh root\@$source->{ip} " if $source->{ip};
-    $cmd .= "zfs send ";
-    $cmd .= "-v " if $param->{verbose};
+    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})) {
-       $cmd .= "-i $source->{all}\@$source->{last_snap} $source->{all}\@$source->{new_snap} ";
-    } else {
-       $cmd .= "$source->{all}\@$source->{new_snap} ";
+    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}";
 
     if ($param->{limit}){
        my $bwl = $param->{limit}*1024;
-       $cmd .= "| cstream  -t $bwl";
+       push @$cmd, \'|', 'cstream', '-t', $bwl;
     }
-    $cmd .= "| ";
-    $cmd .= "ssh root\@$dest->{ip} " if $dest->{ip};
-    $cmd .= "zfs recv $dest->{all}";
-    $cmd .= "\/$source->{last_part}\@$source->{new_snap}";
+    my $target = "$dest->{all}";
+    $target .= "/$source->{last_part}" if $source->{last_part};
+    $target =~ s!/+!/!g;
+
+    push @$cmd, \'|';
+    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)
     };
 
     if (my $erro = $@) {
-       snapshot_destroy($source, undef, $param->{method}, $source->{new_snap});
+       snapshot_destroy($source, undef, $param->{method}, $source->{new_snap}, $param->{source_user}, $param->{dest_user});
        die $erro;
     };
 }
 
 
 sub send_config{
-    my ($source, $dest, $method) = @_;
+    my ($source, $dest, $method, $source_user, $dest_user, $dest_config_path) = @_;
 
-    my $source_target ="$QEMU_CONF$source->{vmid}.conf";
-    my $dest_target_new ="$CONFIG_PATH$source->{vmid}.conf.$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_config_path // $CONFIG_PATH;
+    $config_dir .= "/$dest->{last_part}" if $dest->{last_part};
+
+    $dest_target_new = $config_dir.'/'.$dest_target_new;
 
     if ($method eq 'ssh'){
        if ($dest->{ip} && $source->{ip}) {
-           run_cmd("ssh root\@$dest->{ip} mkdir $CONFIG_PATH -p");
-           run_cmd("scp root\@$source->{ip}:$source_target root\@$dest->{ip}:$dest_target_new");
+           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 root\@$dest->{ip} mkdir $CONFIG_PATH -p");
-           run_cmd("scp $source_target root\@$dest->{ip}:$dest_target_new");
+           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 $CONFIG_PATH -p");
-           run_cmd("scp root\@$source->{ip}:$source_target $dest_target_new");
+           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_PATH$source->{vmid}.conf.$source->{old_snap}";
+           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");
+               run_cmd(['ssh', "$dest_user\@$dest->{ip}", '--', 'rm', '-f', '--', $dest_target_old]);
            } else {
-               run_cmd("rm -f $dest_target_old");
+               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]);
     }
 }
 
@@ -866,14 +1013,14 @@ sub get_date {
 sub status {
     my $cfg = read_cron();
 
-    my $status_list = sprintf("%-25s%-15s%-10s\n", "SOURCE", "NAME", "STATUS");
+    my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS");
 
     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("%-15s", cut_target_width($sync_name, 25));
+           $status_list .= sprintf("%-25s", cut_target_width($sync_name, 25));
            $status_list .= "$states->{$source}->{$sync_name}->{state}\n";
        }
     }
@@ -899,264 +1046,138 @@ sub disable_job {
     update_cron($job);
 }
 
-my $command = $ARGV[0];
+my $cmd_help = {
+    destroy => qq{
+$PROGNAME destroy -source <string> [OPTIONS]
 
-my $commands = {'destroy' => 1,
-              'create' => 1,
-              'sync' => 1,
-              'list' => 1,
-              'status' => 1,
-              'help' => 1,
-              'enable' => 1,
-              'disable' => 1};
-
-if (!$command || !$commands->{$command}) {
-    usage();
-    die "\n";
-}
+        remove a sync Job from the scheduler
 
-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\tstringn\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) = @_;
-
-    switch($command){
-       case 'help'
-       {
-           die "$help_help\n";
-       }
-       case 'sync'
-       {
-           die "$help_sync\n";
-       }
-       case 'destroy'
-       {
-           die "$help_destroy\n";
-       }
-       case 'create'
-       {
-           die "$help_create\n";
-       }
-       case 'list'
-       {
-           die "$help_list\n";
-       }
-       case 'status'
-       {
-           die "$help_status\n";
-       }
-       case 'enable'
-       {
-            die "$help_enable\n";
-       }
-       case 'disable'
-       {
-           die "$help_enable\n";
-       }
-    }
+        -name      string
 
-}
+               name of the sync job, if not set it is default
 
-my @arg = @ARGV;
-my $param = parse_argv(@arg);
+        -source    string
 
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+    },
+    create => qq{
+$PROGNAME create -dest <string> -source <string> [OPTIONS]
 
-switch($command) {
-    case "destroy"
-    {
-       die "$help_destroy\n" if !$param->{source};
-       check_target($param->{source});
-       destroy_job($param);
-    }
-    case "sync"
-    {
-       die "$help_sync\n" if !$param->{source} || !$param->{dest};
-       check_target($param->{source});
-       check_target($param->{dest});
-       sync($param);
-    }
-    case "create"
-    {
-       die "$help_create\n" if !$param->{source} || !$param->{dest};
-       check_target($param->{source});
-       check_target($param->{dest});
-       init($param);
-    }
-    case "status"
-    {
-       print status();
-    }
-    case "list"
-    {
-       print list();
-    }
-    case "help"
-    {
-       my $help_command = $ARGV[1];
-       if ($help_command && $commands->{$help_command}) {
-           print help($help_command);
-       }
-       if ($param->{verbose} == 1){
-           exec("man $PROGNAME");
-       } else {
-           usage(1);
-       }
-    }
-    case "enable"
-    {
-       die "$help_enable\n" if !$param->{source};
-       check_target($param->{source});
-       enable_job($param);
-    }
-    case "disable"
-    {
-       die "$help_disable\n" if !$param->{source};
-       check_target($param->{source});
-       disable_job($param);
-    }
-}
+        Create a sync Job
 
-sub usage {
-    my ($help) = @_;
+        -dest      string
 
-    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");
-}
+               the destination target is like [IP]:<Pool>[/Path]
 
-sub check_target {
-    my ($target) = @_;
+        -dest-user string
 
-    chomp($target);
+               name of the user on the destination target, root by default
 
-    if($target !~ m/(\d+.\d+.\d+.\d+:)?([\w\-\_\/]+)(\/.+)?/) {
-       print("ERROR:\t$target is not valid.\n\tUse [IP:]<ZFSPool>[/Path]!\n");
-       return 1;
-    }
-    return undef;
-}
+        -limit     integer
 
-__END__
+               max sync speed in kBytes/s, default unlimited
 
-=head1 NAME
+        -maxsnap   string
 
-pve-zsync - PVE ZFS Replication Manager
+               how much snapshots will be kept before get erased, default 1
 
-=head1 SYNOPSIS
+        -name      string
 
-pve-zsync <COMMAND> [ARGS] [OPTIONS]
+               name of the sync job, if not set it is default
 
-pve-zsync 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
+        -source-user    string
 
-               Verbose output format.
+               name of the user on the source target, root by default
 
-pve-zsync create -dest <string> -source <string> [OPTIONS]
+       -properties     boolean
 
-        Create a sync Job
+               Include the dataset's properties in the stream.
+
+       -dest-config-path    string
+
+               specify a custom config path on the destination target. default is /var/lib/pve-zsync
+    },
+    sync => qq{
+$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
+
+       will sync one time
 
         -dest      string
 
-               the destination target is like [IP]:<Pool>[/Path]
+               the destination target is like [IP:]<Pool>[/Path]
 
-        -limit     integer
+        -dest-user string
+
+               name of the user on the destination target, root by default
+
+       -limit     integer
 
                max sync speed in kBytes/s, default unlimited
 
-        -maxsnap   string
+        -maxsnap   integer
 
                how much snapshots will be kept before get erased, default 1
 
         -name      string
 
-               name of the sync job, if not set it is default
-
-        -skip      boolean
-
-               if this flag is set it will skip the first sync
+               name of the sync job, if not set it is default.
+               It is only necessary if scheduler allready contains this source.
 
         -source    string
 
                the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
 
-pve-zsync destroy -source <string> [OPTIONS]
+        -source-user    string
 
-        remove a sync Job from the scheduler
+               name of the user on the source target, root by default
+
+       -verbose   boolean
+
+               print out the sync progress.
+
+       -properties     boolean
+
+               Include the dataset's properties in the stream.
+
+       -dest-config-path    string
+
+               specify a custom config path on the destination target. default is /var/lib/pve-zsync
+    },
+    list => qq{
+$PROGNAME list
+
+       Get a List of all scheduled Sync Jobs
+    },
+    status => qq{
+$PROGNAME status
+
+       Get the status of all scheduled Sync Jobs
+    },
+    help => qq{
+$PROGNAME help <cmd> [OPTIONS]
+
+       Get help about specified command.
+
+        <cmd>      string
+
+               Command name
+
+       -verbose   boolean
+
+               Verbose output format.
+    },
+    enable => qq{
+$PROGNAME enable -source <string> [OPTIONS]
+
+        enable a syncjob and reset error
 
         -name      string
 
@@ -1165,8 +1186,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
 
@@ -1176,51 +1198,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
 
@@ -1261,3 +1351,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
+}