]> git.proxmox.com Git - pve-zsync.git/blobdiff - pve-zsync
cleanup: minor code deduplication
[pve-zsync.git] / pve-zsync
index f40971c42ab3bd47b83ce3514677dc3f2e01b521..bf7189484c39aed79a895eae67157a3a9e2882a0 100644 (file)
--- a/pve-zsync
+++ b/pve-zsync
@@ -51,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)  = @_;
 
@@ -65,21 +70,33 @@ sub check_bin {
 }
 
 sub cut_target_width {
-    my ($target, $max) = @_;
+    my ($path, $maxlen) = @_;
+    $path =~ s@/+@/@g;
+
+    return $path if length($path) <= $maxlen;
 
-    return  $target if (length($target) <= $max);
-    my @spl = split('/', $target);
+    return '..'.substr($path, -$maxlen+2) if $path !~ m@/@;
 
-    my $count = length($spl[@spl-1]);
-    return "..\/".substr($spl[@spl-1],($count-$max)+3 , $count) 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)
+    }
 
-    $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 $head = $1;
+    my $both = length($head) + length($tail);
+    my $remaining = $maxlen-$both-4; # -4 for "/../"
 
-    my $rest = 1;
-    $rest = $max-$count if ($max-$count > 0);
+    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 {
@@ -491,7 +508,7 @@ sub init {
     die "Config already exists\n" if $cfg->{$job->{source}}->{$job->{name}};
 
     #check if vm has zfs disks if not die;
-    get_disks($source, 1) if $source->{vmid};
+    get_disks($source) if $source->{vmid};
 
     update_cron($job);
     update_state($job);
@@ -631,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;
@@ -707,7 +724,7 @@ sub write_cron {
 }
 
 sub get_disks {
-    my ($target, $get_err) = @_;
+    my ($target) = @_;
 
     my $cmd = [];
     push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
@@ -722,7 +739,7 @@ sub get_disks {
 
     my $res = run_cmd($cmd);
 
-    my $disks = parse_disks($res, $target->{ip}, $target->{vm_type}, $get_err);
+    my $disks = parse_disks($res, $target->{ip}, $target->{vm_type});
 
     return $disks;
 }
@@ -745,23 +762,22 @@ sub run_cmd {
 }
 
 sub parse_disks {
-    my ($text, $ip, $vm_type, $get_err) = @_;
+    my ($text, $ip, $vm_type) = @_;
 
     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)/)';
+       next if $vm_type eq 'qemu' && ($line =~ m/backup=(?i:0|no|off|false)/);
 
        #LXC if backup is not set do no in sync
-       $error = ($line =~ m/backup=(?i:1|yes|on|true)/) if $vm_type eq 'lxc';
+       next if $vm_type eq 'lxc' && ($line =~ m/^mp\d:/) && ($line !~ m/backup=(?i:1|yes|on|true)/);
 
        my $disk = undef;
        my $stor = undef;
@@ -775,9 +791,9 @@ sub parse_disks {
                    last;
                }
            }
-
-       } else {
-           print "Disk: \"$line\" will not include in pve-sync\n" if $get_err || $error;
+       }
+       if (!defined($disk) || !defined($stor)) {
+           print "Disk: \"$line\" has no valid zfs dataset format and will be skipped\n";
            next;
        }