]> git.proxmox.com Git - pve-zsync.git/blobdiff - pve-zsync
fix #1301 skip if mp has no backup flag.
[pve-zsync.git] / pve-zsync
index 283b55c31d9fa9b3611ffae13d825857ecdd974c..bc06ae12cddfd433523306318275f8f5658dfddd 100644 (file)
--- a/pve-zsync
+++ b/pve-zsync
@@ -7,7 +7,6 @@ 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';
@@ -52,6 +51,11 @@ 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)  = @_;
 
@@ -66,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;
 
-    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)
+    }
 
-    return "$spl[0]".substr($target, length($spl[0]), $rest)."..\/".$spl[@spl-1];
+    $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 "../"
+    }
+
+    substr($path, ($remaining/2), (length($path)-$remaining), '..');
+    return "$head/" . $path . "/$tail";
 }
 
 sub lock {
@@ -107,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 {
@@ -404,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";
@@ -415,16 +435,16 @@ sub list {
 
     my $cfg = read_cron();
 
-    my $list = sprintf("%-25s%-10s%-7s%-20s%-5s%-5s\n" , "SOURCE", "NAME", "STATE", "LAST SYNC", "TYPE", "CON");
+    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("%-10s", cut_target_width($name, 10));
-           $list .= sprintf("%-7s", $states->{$source}->{$name}->{state});
+           $list .= sprintf("%-25s", cut_target_width($name, 25));
+           $list .= sprintf("%-10s", $states->{$source}->{$name}->{state});
            $list .= sprintf("%-20s", $states->{$source}->{$name}->{lsync});
-           $list .= sprintf("%-5s", $states->{$source}->{$name}->{vm_type});
+           $list .= sprintf("%-6s", defined($states->{$source}->{$name}->{vm_type}) ? $states->{$source}->{$name}->{vm_type} : "undef");
            $list .= sprintf("%-5s\n", $cfg->{$source}->{$name}->{method});
        }
     }
@@ -439,6 +459,8 @@ sub vm_exists {
 
     my $res = undef;
 
+    return undef if !defined($target->{vmid});
+
     eval { $res = run_cmd([@cmd, 'ls',  "$QEMU_CONF/$target->{vmid}.conf"]) };
 
     return "qemu" if $res;
@@ -471,19 +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);
-
-    my $check = check_pool_exists($source->{path}, $source->{ip}) if !$source->{vmid} && $source->{path};
+    die "Pool $dest->{all} does not exists\n" if !check_pool_exists($dest);
 
-    die "Pool $source->{path} does not exists\n" if undef($check);
+    if (!defined($source->{vmid})) {
+       die "Pool $source->{all} does not exists\n" if !check_pool_exists($source);
+    }
 
     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_type;
+    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, 1) if $source->{vmid};
+
     update_cron($job);
     update_state($job);
 
@@ -622,7 +648,7 @@ 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;
@@ -698,7 +724,7 @@ sub write_cron {
 }
 
 sub get_disks {
-    my ($target) = @_;
+    my ($target, $get_err) = @_;
 
     my $cmd = [];
     push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
@@ -713,7 +739,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}, $get_err);
 
     return $disks;
 }
@@ -736,24 +762,40 @@ sub run_cmd {
 }
 
 sub parse_disks {
-    my ($text, $ip, $vm_type) = @_;
+    my ($text, $ip, $vm_type, $get_err) = @_;
 
     my $disks;
 
     my $num = 0;
     while ($text && $text =~ s/^(.*?)(\n|$)//) {
        my $line = $1;
+       my $error = $vm_type eq 'qemu' ? 1 : 0 ;
 
        next if $line =~ /cdrom|none/;
        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|mp)\d+)|rootfs): (.+:)([A-Za-z0-9\-]+),(.*)$/) {
-           $disk = $3;
-           $stor = $2;
+       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;
+               }
+           }
+
        } else {
-           die "disk is not on ZFS Storage\n";
+           print "Disk: \"$line\" will not include in pve-sync\n" if $get_err || $error;
+           next;
        }
 
        my $cmd = [];
@@ -796,6 +838,7 @@ sub parse_disks {
        }
     }
 
+    die "Vm include no disk on zfs.\n" if !$disks->{0};
     return $disks;
 }
 
@@ -855,7 +898,7 @@ 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};
 
@@ -872,7 +915,7 @@ sub send_image {
     $target =~ s!/+!/!g;
 
     push @$cmd, \'|';
-       push @$cmd, 'ssh', "root\@$dest->{ip}", '--' if $dest->{ip};
+       push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip};
        push @$cmd, 'zfs', 'recv', '-F', '--';
        push @$cmd, "$target";
 
@@ -933,14 +976,14 @@ sub send_image {
     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";
            }
        }
@@ -1048,39 +1091,30 @@ sub send_image {
     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";
+
        }
 
     }
@@ -1088,60 +1122,59 @@ sub send_image {
     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} == 1){
+           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 {