]> git.proxmox.com Git - pve-zsync.git/blobdiff - pve-zsync
Fix: You can now use the pool as replication source.
[pve-zsync.git] / pve-zsync
index 4941456b664f7c803142e3fa751162a09061f533..e411dff031c3f3f78e091a517835696875ebe9d4 100644 (file)
--- a/pve-zsync
+++ b/pve-zsync
@@ -7,19 +7,20 @@ 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;
 
@@ -29,15 +30,15 @@ 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)?::" .                    ")))";
+    "(?:(?:" .                             "(?:$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
@@ -50,13 +51,18 @@ check_bin ('zfs');
 check_bin ('ssh');
 check_bin ('scp');
 
+$SIG{TERM} = $SIG{QUIT} = $SIG{PIPE} = $SIG{HUP} = $SIG{KILL} = $SIG{INT} =
+    sub {
+       die "Signal 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;
        }
     }
 
@@ -64,21 +70,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;
+
+    if (length($tail)+3 == $maxlen) {
+       return "../$tail";
+    } elsif (length($tail)+2 >= $maxlen) {
+       return '..'.substr($tail, -$maxlen+2)
+    }
 
-    my $rest = 1;
-    $rest = $max-$count if ($max-$count > 0);
+    $path =~ s@(/[^/]+)(?:/|$)@@;
+    my $head = $1;
+    my $both = length($head) + length($tail);
+    my $remaining = $maxlen-$both-4; # -4 for "/../"
 
-    return "$spl[0]".substr($target, length($spl[0]), $rest)."..\/".$spl[@spl-1];
+    if ($remaining < 0) {
+       return substr($head, 0, $maxlen - length($tail) - 3) . "../$tail"; # -3 for "../"
+    }
+
+    substr($path, ($remaining/2), (length($path)-$remaining), '..');
+    return "$head/" . $path . "/$tail";
 }
 
 sub lock {
@@ -105,16 +123,19 @@ sub check_pool_exists {
     my ($target) = @_;
 
     my $cmd = [];
-    push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
+
+    if ($target->{ip}) {
+       push @$cmd, 'ssh', "root\@$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 {
@@ -187,14 +208,14 @@ sub parse_argv {
     $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});
+                                        '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";
@@ -215,6 +236,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"};
@@ -313,6 +335,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"};
@@ -372,7 +395,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;
@@ -400,6 +423,7 @@ 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 .= "\n";
@@ -411,16 +435,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});
        }
     }
 
@@ -430,13 +455,20 @@ sub list {
 sub vm_exists {
     my ($target) = @_;
 
-    my $cmd = [];
-    push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
-    push @$cmd, 'qm', 'status', $target->{vmid};
+    my @cmd = ('ssh', "root\@$target->{ip}", '--') if $target->{ip};
 
-    my $res = run_cmd($cmd);
+    my $res = undef;
+
+    return undef if !defined($target->{vmid});
+
+    eval { $res = run_cmd([@cmd, 'ls',  "$QEMU_CONF/$target->{vmid}.conf"]) };
+
+    return "qemu" if $res;
+
+    eval { $res = run_cmd([@cmd, 'ls',  "$LXC_CONF/$target->{vmid}.conf"]) };
+
+    return "lxc" if $res;
 
-    return 1 if ($res =~ m/^status.*$/);
     return undef;
 }
 
@@ -461,16 +493,23 @@ sub init {
        run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "root\@$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);
 
-    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);
+    }
 
-    die "Pool $source->{path} does not exists\n" if undef($check);
+    my $vm_type = vm_exists($source);
+    $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) if $source->{vmid};
+
     update_cron($job);
     update_state($job);
 
@@ -532,7 +571,7 @@ 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});
 
        snapshot_add($source, $dest, $param->{name}, $date);
 
@@ -542,14 +581,18 @@ sub sync {
 
     };
 
+    my $vm_type = vm_exists($source);
+    $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);
+           die "VM $source->{vmid} doesn't exist\n" if !$vm_type;
            my $disks = get_disks($source);
 
            foreach my $disk (sort keys %{$disks}) {
@@ -559,8 +602,10 @@ sub sync {
                $source->{last_part} = $disks->{$disk}->{last_part};
                &$sync_path($source, $dest, $job, $param, $date);
            }
-           if ($param->{method} eq "ssh") {
+           if ($param->{method} eq "ssh" && ($source->{ip} || $dest->{ip})) {
                send_config($source, $dest,'ssh');
+           } else {
+               send_config($source, $dest,'local');
            }
        } else {
            &$sync_path($source, $dest, $job, $param, $date);
@@ -603,7 +648,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++;
@@ -653,24 +699,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));
@@ -682,11 +728,18 @@ sub get_disks {
 
     my $cmd = [];
     push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
-    push @$cmd, 'qm', 'config', $target->{vmid};
+
+    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});
 
     return $disks;
 }
@@ -709,7 +762,7 @@ sub run_cmd {
 }
 
 sub parse_disks {
-    my ($text, $ip) = @_;
+    my ($text, $ip, $vm_type) = @_;
 
     my $disks;
 
@@ -717,16 +770,31 @@ 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|ide|scsi|sata)\d+: (.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $2;
-           $stor = $1;
-       } 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;
        }
 
        my $cmd = [];
@@ -734,7 +802,9 @@ sub parse_disks {
        push @$cmd, 'pvesm', 'path', "$stor$disk";
        my $path = run_cmd($cmd);
 
-       if ($path =~ m/^\/dev\/zvol\/(\w+.*)(\/$disk)$/) {
+       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);
@@ -746,6 +816,20 @@ sub parse_disks {
            $disks->{$num}->{last_part} = $disk;
            $disks->{$num}->{all} .= "\/$disk";
 
+           $num++;
+       } elsif ($vm_type eq 'lxc' && $path =~ m/^\/(\w+.+)(\/(\w+.*))*(\/$disk)$/) {
+
+           $disks->{$num}->{pool} = $1;
+           $disks->{$num}->{all} = $disks->{$num}->{pool};
+
+           if ($2) {
+               $disks->{$num}->{path} = $3;
+               $disks->{$num}->{all} .= "\/$disks->{$num}->{path}";
+           }
+
+           $disks->{$num}->{last_part} = $disk;
+           $disks->{$num}->{all} .= "\/$disk";
+
            $num++;
 
        } else {
@@ -753,6 +837,7 @@ sub parse_disks {
        }
     }
 
+    die "Vm include no disk on zfs.\n" if !$disks->{0};
     return $disks;
 }
 
@@ -775,7 +860,8 @@ sub snapshot_destroy {
     if ($dest) {
        my @ssh = $dest->{ip} ? ('ssh', "root\@$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"]);
@@ -787,22 +873,28 @@ sub snapshot_destroy {
 }
 
 sub snapshot_exist {
-    my ($source ,$dest, $method) = @_;
+    my ($source , $dest, $method) = @_;
 
     my $cmd = [];
     push @$cmd, 'ssh', "root\@$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);};
-    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}$/;
     }
 }
@@ -812,11 +904,11 @@ sub send_image {
 
     my $cmd = [];
 
-    push @$cmd, 'ssh', "root\@$source->{ip}", '--' if $source->{ip};
+    push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$source->{ip}", '--' if $source->{ip};
     push @$cmd, 'zfs', 'send';
     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})) {
        push @$cmd, '-i', "$source->{all}\@$source->{last_snap}";
     }
     push @$cmd, '--', "$source->{all}\@$source->{new_snap}";
@@ -825,10 +917,14 @@ sub send_image {
        my $bwl = $param->{limit}*1024;
        push @$cmd, \'|', 'cstream', '-t', $bwl;
     }
+    my $target = "$dest->{all}";
+    $target .= "/$source->{last_part}" if $source->{last_part};
+    $target =~ s!/+!/!g;
+
     push @$cmd, \'|';
-    push @$cmd, 'ssh', "root\@$dest->{ip}", '--' if $dest->{ip};
-    push @$cmd, 'zfs', 'recv', '--';
-    push @$cmd, "$dest->{all}/$source->{last_part}\@$source->{new_snap}";
+    push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip};
+    push @$cmd, 'zfs', 'recv', '-F', '--';
+    push @$cmd, "$target";
 
     eval {
        run_cmd($cmd)
@@ -844,29 +940,36 @@ sub send_image {
 sub send_config{
     my ($source, $dest, $method) = @_;
 
-    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->{last_part} ?  "${CONFIG_PATH}/$dest->{last_part}" : $CONFIG_PATH;
+
+    $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_PATH]);
+           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_PATH]);
+           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_PATH]);
-           run_cmd(['scp', '--', "root\@$source->{ip}:$source_target", $dest_target_new]);
+           run_cmd(['mkdir', '-p', '--', $config_dir]);
+           run_cmd(['scp', '--', "root\@[$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]);
            } 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]);
     }
 }
 
@@ -880,14 +983,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";
        }
     }
@@ -916,118 +1019,175 @@ sub disable_job {
 my $command = $ARGV[0];
 
 my $commands = {'destroy' => 1,
-              'create' => 1,
-              'sync' => 1,
-              'list' => 1,
-              'status' => 1,
-              'help' => 1,
-              'enable' => 1,
-              'disable' => 1};
+               'create' => 1,
+               'sync' => 1,
+               'list' => 1,
+               'status' => 1,
+               'help' => 1,
+               'enable' => 1,
+               'disable' => 1};
 
 if (!$command || !$commands->{$command}) {
     usage();
     die "\n";
 }
 
-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";
+my $help_sync = <<EOF;
+$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
+
+       will sync one time
+
+        -dest      string
+
+               the destination target is like [IP:]<Pool>[/Path]
+
+       -limit     integer
+
+               max sync speed in kBytes/s, default unlimited
+
+        -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.
+               It is only necessary if scheduler allready contains this source.
+
+        -source    string
+
+               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+
+       -verbose   boolean
+
+               print out the sync progress.
+EOF
+
+my $help_create = <<EOF;
+$PROGNAME create -dest <string> -source <string> [OPTIONS]
+
+        Create a sync Job
+
+        -dest      string
+
+               the destination target is like [IP]:<Pool>[/Path]
+
+        -limit     integer
+
+               max sync speed in kBytes/s, default unlimited
+
+        -maxsnap   string
+
+               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
+
+        -source    string
+
+               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+EOF
+
+my $help_destroy = <<EOF;
+$PROGNAME destroy -source <string> [OPTIONS]
+
+        remove a sync Job from the scheduler
+
+        -name      string
+
+               name of the sync job, if not set it is default
+
+        -source    string
+
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+EOF
+
+my $help_help = <<EOF;
+$PROGNAME help <cmd> [OPTIONS]
+
+       Get help about specified command.
+
+        <cmd>      string
+
+               Command name
+
+       -verbose   boolean
+
+               Verbose output format.
+EOF
+
+my $help_list = <<EOF;
+$PROGNAME list
+
+       Get a List of all scheduled Sync Jobs
+EOF
+
+my $help_status = <<EOF;
+$PROGNAME status
+
+       Get the status of all scheduled Sync Jobs
+EOF
+
+my $help_enable = <<EOF;
+$PROGNAME enable -source <string> [OPTIONS]
+
+        enable a syncjob and reset error
+
+        -name      string
+
+               name of the sync job, if not set it is default
+
+        -source    string
+
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+EOF
+
+my $help_disable = <<EOF;
+$PROGNAME disable -source <string> [OPTIONS]
+
+        pause a sync job
+
+        -name      string
+
+               name of the sync job, if not set it is default
+
+        -source    string
+
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path] 
+EOF
 
 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";
-       }
+    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";
+
     }
 
 }
@@ -1035,60 +1195,59 @@ sub help {
 my @arg = @ARGV;
 my $param = parse_argv(@arg);
 
+if ($command eq 'destroy') {
+    die "$help_destroy\n" if !$param->{source};
+
+    check_target($param->{source});
+    destroy_job($param);
+
+} elsif ($command eq 'sync') {
+    die "$help_sync\n" if !$param->{source} || !$param->{dest};
+
+    check_target($param->{source});
+    check_target($param->{dest});
+    sync($param);
+
+} elsif ($command eq 'create') {
+    die "$help_create\n" if !$param->{source} || !$param->{dest};
+
+    check_target($param->{source});
+    check_target($param->{dest});
+    init($param);
+
+} elsif ($command eq 'status') {
+    print status();
+
+} elsif ($command eq 'list') {
+    print list();
+
+} elsif ($command eq 'help') {
+    my $help_command = $ARGV[1];
+
+    if ($help_command && $commands->{$help_command}) {
+       print help($help_command);
 
-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);
+    if ($param->{verbose}) {
+       exec("man $PROGNAME");
+
+    } else {
+       usage(1);
+
     }
+
+} elsif ($command eq 'enable') {
+    die "$help_enable\n" if !$param->{source};
+
+    check_target($param->{source});
+    enable_job($param);
+
+} elsif ($command eq 'disable') {
+    die "$help_disable\n" if !$param->{source};
+
+    check_target($param->{source});
+    disable_job($param);
+
 }
 
 sub usage {
@@ -1229,6 +1388,10 @@ pve-zsync sync -dest <string> -source <string> [OPTIONS]
 
                the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
 
+       -verbose   boolean
+
+               print out the sync progress.
+
 =head1 DESCRIPTION
 
 This Tool helps you to sync your VM or directory which stored on ZFS between 2 servers.