X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=pve-zsync;h=4993bedf8ff16b2821780715e6fb9920c371a851;hb=d79cb93dd5bbea33d4134032a601c6500639c925;hp=8dd9ed240ec77b8b0e662d9ee960729d94d4aa7b;hpb=c85692fa38c0098b4ccc816533f19bcbf0c718c0;p=pve-zsync.git diff --git a/pve-zsync b/pve-zsync index 8dd9ed2..4993bed 100644 --- a/pve-zsync +++ b/pve-zsync @@ -4,280 +4,448 @@ use strict; use warnings; use Data::Dumper qw(Dumper); use Fcntl qw(:flock SEEK_END); -use Getopt::Long; -use Switch; +use Getopt::Long qw(GetOptionsFromArray); +use File::Copy qw(move); +use File::Path qw(make_path); +use JSON; +use IO::File; +use String::ShellQuote 'shell_quote'; my $PROGNAME = "pve-zsync"; -my $CONFIG_PATH = "/var/lib/$PROGNAME/"; -my $CONFIG = "$CONFIG_PATH$PROGNAME.cfg"; +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 $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 $LOCKFILE = "$CONFIG_PATH$PROGNAME.lock"; -my $PROG_PATH = "$PATH${PROGNAME}"; + +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\-_]+)(/.+)?)$!; 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 "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; } } - warn "unable to find command '$bin'\n"; + die "unable to find command '$bin'\n"; } sub cut_target_width { - my ($target, $max) = @_; + my ($path, $maxlen) = @_; + $path =~ s@/+@/@g; + + return $path if length($path) <= $maxlen; + + return '..'.substr($path, -$maxlen+2) if $path !~ m@/@; - return $target if (length($target) <= $max); - my @spl = split('/', $target); + $path =~ s@/([^/]+/?)$@@; + my $tail = $1; - my $count = length($spl[@spl-1]); - return "..\/".substr($spl[@spl-1],($count-$max)+3 ,$count) if $count > $max; + 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 { my ($fh) = @_; - flock($fh, LOCK_EX) || die "Cannot lock config - $!\n"; - - seek($fh, 0, SEEK_END) || die "Cannot seek - $!\n"; + flock($fh, LOCK_EX) || die "Can't lock config - $!\n"; } sub unlock { my ($fh) = @_; - flock($fh, LOCK_UN) || die "Cannot unlock config- $!\n"; + flock($fh, LOCK_UN) || die "Can't unlock config- $!\n"; } -sub check_config { - my ($source, $name, $cfg) = @_; +sub get_status { + my ($source, $name, $status) = @_; - my $id = $source->{vmid} ? $source->{vmid} : $source->{abs_path}; - my $status = $cfg->{$id}->{$name}->{status}; - - if ($cfg->{$id}->{$name}->{status}) { + if ($status->{$source->{all}}->{$name}->{status}) { return $status; } return undef; } +sub check_pool_exists { + my ($target) = @_; -sub check_pool_exsits { - my ($pool, $ip) = @_; + my $cmd = []; - my $cmd = ''; - $cmd = "ssh root\@$ip " if $ip; - $cmd .= "zfs list $pool -H"; + if ($target->{ip}) { + push @$cmd, 'ssh', "root\@$target->{ip}", '--'; + } + push @$cmd, 'zfs', 'list', '-H', '--', $target->{all}; eval { run_cmd($cmd); }; - if($@) { - return 1; + if ($@) { + return 0; } - return undef; + return 1; } -sub write_config { - my ($cfg) = @_; +sub parse_target { + my ($text) = @_; - open(my $fh, ">", "$CONFIG") || die "cannot open > $CONFIG: $!\n"; + my $errstr = "$text : is not a valid input! Use [IP:] or [IP:][/Path]"; + my $target = {}; - my $text = encode_config($cfg); + if ($text !~ $TARGETRE) { + die "$errstr\n"; + } + $target->{all} = $2; + $target->{ip} = $1 if $1; + my @parts = split('/', $2); - print($fh $text); + $target->{ip} =~ s/^\[(.*)\]$/$1/ if $target->{ip}; - close($fh); + my $pool = $target->{pool} = shift(@parts); + die "$errstr\n" if !$pool; + + if ($pool =~ m/^\d+$/) { + $target->{vmid} = $pool; + delete $target->{pool}; + } + + return $target if (@parts == 0); + $target->{last_part} = pop(@parts); + + if ($target->{ip}) { + pop(@parts); + } + if (@parts > 0) { + $target->{path} = join('/', @parts); + } + + return $target; } -sub read_config { +sub read_cron { - if(!-e "$CONFIG") { + #This is for the first use to init file; + if (!-e $CRONJOBS) { + my $new_fh = IO::File->new("> $CRONJOBS"); + die "Could not create $CRONJOBS: $!\n" if !$new_fh; + close($new_fh); return undef; } - open(my $fh, "<", "$CONFIG") || die "cannot open > $CONFIG: $!\n"; - - $/ = undef; + my $fh = IO::File->new("< $CRONJOBS"); + die "Could not open file $CRONJOBS: $!\n" if !$fh; - my $text = <$fh>; + my @text = <$fh>; close($fh); - my $cfg = decode_config($text); + return encode_cron(@text); +} - return $cfg; +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"; + } + + $param->{name} = "default" if !$param->{name}; + $param->{maxsnap} = 1 if !$param->{maxsnap}; + $param->{method} = "ssh" if !$param->{method}; + + return $param; } -sub encode_config { - my ($cfg) = @_; - my $raw = ''; +sub add_state_to_job { + my ($job) = @_; - foreach my $source (sort keys%{$cfg}) { - foreach my $sync_name (sort keys%{$cfg->{$source}}) { - $raw .= "$source: $sync_name\n"; - foreach my $parameter (sort keys%{$cfg->{$source}->{$sync_name}}) { - $raw .= "\t$parameter: $cfg->{$source}->{$sync_name}->{$parameter}\n"; - } - } + my $states = read_state(); + my $state = $states->{$job->{source}}->{$job->{name}}; + + $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"}; } - return $raw; + return $job; } -sub decode_config { - my ($raw) = @_; +sub encode_cron { + my (@text) = @_; + my $cfg = {}; - my $source; - my $check = 0; - my $sync_name; - while ($raw && $raw =~ s/^(.*?)(\n|$)//) { - my $line = $1; - next if $line =~ m/^\#/; - next if $line =~ m/^\s*$/; - - if ($line =~ m/^(\t| )(\w+): (.+)/) { - my $par = $2; - my $value = $3; - - if ($par eq 'source_pool') { - $cfg->{$source}->{$sync_name}->{$par} = $value; - die "error in Config: SourcePool value doubled\n" if ($check & 1); - $check += 1; - } elsif ($par eq 'source_ip') { - $cfg->{$source}->{$sync_name}->{$par} = $value; - die "error in Config: SourceIP value doubled\n" if ($check & 2); - $check += 2; - } elsif ($par eq 'status') { - $cfg->{$source}->{$sync_name}->{$par} = $value; - die "error in Config: Status value doubled\n" if ($check & 4); - $check += 4; - } elsif ($par eq 'method') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: Method value doubled\n" if ($check & 8); - $check += 8; - } elsif ($par eq 'interval') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: Iterval value doubled\n" if ($check & 16); - $check += 16; - } elsif ($par eq 'limit') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: Limit value doubled\n" if ($check & 32); - $check += 32; - } elsif ($par eq 'dest_pool') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: DestPool value doubled\n" if ($check & 64); - $check += 64; - } elsif ($par eq 'dest_ip') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: DestIp value doubled\n" if ($check & 128); - $check += 128; - } elsif ($par eq 'dest_path') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: DestPath value doubled\n" if ($check & 256); - $check += 256; - } elsif ($par eq 'source_path') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: SourcePath value doubled\n" if ($check & 512); - $check += 512; - } elsif ($par eq 'vmid') { - $cfg -> {$source}->{$sync_name}->{$par} = $value; - die "error in Config: Vmid value doubled\n" if ($check & 1024); - $check += 1024; - } elsif ($par =~ 'lsync') { - $cfg->{$source}->{$sync_name}->{$par} = $value; - die "error in Config: lsync value doubled\n" if ($check & 2048); - $check += 2048; - } elsif ($par =~ 'maxsnap') { - $cfg->{$source}->{$sync_name}->{$par} = $value; - die "error in Config: maxsnap value doubled\n" if ($check & 4096); - $check += 4096; - } else { - die "error in Config\n"; - } - } elsif ($line =~ m/^((\d+.\d+.\d+.\d+):)?([\w\-\_\/]+): (.+){0,1}/) { - $source = $3; - $sync_name = $4 ? $4 : 'default'; - $cfg->{$source}->{$sync_name} = undef; - $cfg->{$source}->{$sync_name}->{source_ip} = $2 if $2; - $check = 0; + while (my $line = shift(@text)) { + + my @arg = split('\s', $line); + 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}; } } + return $cfg; } -sub parse_target { - my ($text) = @_; +sub param_to_job { + my ($param) = @_; - my $errstr = "No valid input:"; - if ($text =~ m/^((\d+.\d+.\d+.\d+):)?((\w+)\/?)([\w\/\-\_]*)?$/) { + my $job = {}; - die "$errstr $text\n" if !$3; - my $tmp = $3; - my $target = {}; + my $source = parse_target($param->{source}); + my $dest = parse_target($param->{dest}) if $param->{dest}; + + $job->{name} = !$param->{name} ? "default" : $param->{name}; + $job->{dest} = $param->{dest} if $param->{dest}; + $job->{method} = "local" if !$dest->{ip} && !$source->{ip}; + $job->{method} = "ssh" if !$job->{method}; + $job->{limit} = $param->{limit}; + $job->{maxsnap} = $param->{maxsnap} if $param->{maxsnap}; + $job->{source} = $param->{source}; + + return $job; +} + +sub read_state { - if ($2) { - $target->{ip} = $2; + if (!-e $STATE) { + make_path $CONFIG_PATH; + my $new_fh = IO::File->new("> $STATE"); + die "Could not create $STATE: $!\n" if !$new_fh; + print $new_fh "{}"; + close($new_fh); + return undef; + } + + my $fh = IO::File->new("< $STATE"); + die "Could not open file $STATE: $!\n" if !$fh; + + my $text = <$fh>; + my $states = decode_json($text); + + close($fh); + + return $states; +} + +sub update_state { + my ($job) = @_; + my $text; + my $in_fh; + + eval { + + $in_fh = IO::File->new("< $STATE"); + die "Could not open file $STATE: $!\n" if !$in_fh; + lock($in_fh); + $text = <$in_fh>; + }; + + my $out_fh = IO::File->new("> $STATE.new"); + die "Could not open file ${STATE}.new: $!\n" if !$out_fh; + + my $states = {}; + my $state = {}; + if ($text){ + $states = decode_json($text); + $state = $states->{$job->{source}}->{$job->{name}}; + } + + 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"}; } + $states->{$job->{source}}->{$job->{name}} = $state; + } else { + + delete $states->{$job->{source}}->{$job->{name}}; + delete $states->{$job->{source}} if !keys %{$states->{$job->{source}}}; + } + + $text = encode_json($states); + print $out_fh $text; + + close($out_fh); + move("$STATE.new", $STATE); + eval { + close($in_fh); + }; + + return $states; +} + +sub update_cron { + my ($job) = @_; - if ($tmp =~ m/^(\d\d\d+)$/) { - $target->{vmid} = $tmp; + my $updated; + my $has_header; + my $line_no = 0; + my $text = ""; + my $header = "SHELL=/bin/sh\n"; + $header .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n"; + + my $fh = IO::File->new("< $CRONJOBS"); + die "Could not open file $CRONJOBS: $!\n" if !$fh; + lock($fh); + + my @test = <$fh>; + + while (my $line = shift(@test)) { + chomp($line); + if ($line =~ m/source $job->{source} .*name $job->{name} /) { + $updated = 1; + next if $job->{state} eq "del"; + $text .= format_job($job, $line); } else { - $target->{pool} = $4; - my $abs_path = $4; - if ($5) { - $target->{path} = "\/$5"; - $abs_path .= "\/$5"; + if (($line_no < 3) && ($line =~ /^(PATH|SHELL)/ )) { + $has_header = 1; } - $target->{abs_path} = $abs_path; + $text .= "$line\n"; } + $line_no++; + } - return $target; + if (!$has_header) { + $text = "$header$text"; } - die "$errstr $text\n"; + + if (!$updated) { + $text .= format_job($job); + } + my $new_fh = IO::File->new("> ${CRONJOBS}.new"); + die "Could not open file ${CRONJOBS}.new: $!\n" if !$new_fh; + + 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"); + close ($fh); } -sub list { +sub format_job { + my ($job, $line) = @_; + my $text = ""; - my $cfg = read_config(); + if ($job->{state} eq "stopped") { + $text = "#"; + } + if ($line) { + $line =~ /^#*(.+) root/; + $text .= $1; + } else { + $text .= "*/$INTERVAL * * * *"; + } + $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"; + + return $text; +} - my $list = sprintf("%-25s%-15s%-7s%-20s%-10s%-5s\n" , "SOURCE", "NAME", "ACTIVE", "LAST SYNC", "INTERVAL", "TYPE"); +sub list { - foreach my $source (sort keys%{$cfg}) { - foreach my $sync_name (sort keys%{$cfg->{$source}}) { - my $source_name = $source; - $source_name = $cfg->{$source}->{$sync_name}->{source_ip}.":$source" if $cfg->{$source}->{$sync_name}->{source_ip}; - $list .= sprintf("%-25s%-15s", cut_target_width($source_name,25), cut_target_width($sync_name,15)); + my $cfg = read_cron(); - my $active = ""; - if($cfg->{$source}->{$sync_name}->{status} eq 'syncing') { - $active = "yes"; - } else { - $active = "no"; - } + my $list = sprintf("%-25s%-25s%-10s%-20s%-6s%-5s\n" , "SOURCE", "NAME", "STATE", "LAST SYNC", "TYPE", "CON"); - $list .= sprintf("%-7s", $active); - $list .= sprintf("%-20s",$cfg->{$source}->{$sync_name}->{lsync}); - $list .= sprintf("%-10s",$cfg->{$source}->{$sync_name}->{interval}); - $list .= sprintf("%-5s\n",$cfg->{$source}->{$sync_name}->{method}); + 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("%-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}); } } @@ -287,94 +455,63 @@ sub list { sub vm_exists { my ($target) = @_; - my $cmd = ""; - $cmd = "ssh root\@$target->{ip} " if ($target->{ip}); - $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; } sub init { my ($param) = @_; - open(my $lock_fh, ">", $LOCKFILE) || die "cannot open Lock File: $LOCKFILE\n"; - lock($lock_fh); - my $cfg = read_config(); + my $cfg = read_cron(); - my $vm = {}; + my $job = param_to_job($param); - my $name = $param->{name} ? $param->{name} : "default"; - my $interval = $param->{interval} ? $param->{interval} : 15; + $job->{state} = "ok"; + $job->{lsync} = 0; my $source = parse_target($param->{source}); my $dest = parse_target($param->{dest}); - $vm->{$name}->{dest_pool} = $dest->{pool}; - $vm->{$name}->{dest_ip} = $dest->{ip} if $dest->{ip}; - $vm->{$name}->{dest_path} = $dest->{path} if $dest->{path}; - - $param->{method} = "local" if !$dest->{ip} && !$source->{ip}; - $vm->{$name}->{status} = "ok"; - $vm->{$name}->{interval} = $interval; - $vm->{$name}->{method} = $param->{method} ? $param->{method} : "ssh"; - $vm->{$name}->{limit} = $param->{limit} if $param->{limit}; - $vm->{$name}->{maxsnap} = $param->{maxsnap} if $param->{maxsnap}; - - if (my $ip = $vm->{$name}->{dest_ip}) { - run_cmd("ssh-copy-id -i /root/.ssh/id_rsa.pub root\@$ip"); + if (my $ip = $dest->{ip}) { + run_cmd(['ssh-copy-id', '-i', '/root/.ssh/id_rsa.pub', "root\@$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', "root\@$ip"]); } - die "Pool $dest->{abs_path} does not exists\n" if check_pool_exsits($dest->{abs_path}, $dest->{ip}); - - my $check = check_pool_exsits($source->{abs_path}, $source->{ip}) if !$source->{vmid} && $source->{abs_path}; - - die "Pool $source->{abs_path} does not exists\n" if undef($check); - - my $add_job = sub { - my ($vm, $name) = @_; - my $source = ""; - if ($vm->{$name}->{vmid}) { - $source .= $vm->{$name}->{vmid}; - } else { - $source .= $vm->{$name}->{source_pool}; - $source .= $vm->{$name}->{source_path} if $vm->{$name}->{source_path}; - } - die "Config already exists\n" if $cfg->{$source}->{$name}; - - $cfg->{$source}->{$name} = $vm->{$name}; - }; + die "Pool $dest->{all} does not exists\n" if !check_pool_exists($dest); - if ($source->{vmid}) { - die "VM $source->{vmid} doesn't exist\n" if !vm_exists($source); - my $disks = get_disks($source); - $vm->{$name}->{vmid} = $source->{vmid}; - $vm->{$name}->{lsync} = 0; - $vm->{$name}->{source_ip} = $source->{ip} if $source->{ip}; - - &$add_job($vm, $name); + if (!defined($source->{vmid})) { + die "Pool $source->{all} does not exists\n" if !check_pool_exists($source); + } - } else { - $vm->{$name}->{source_pool} = $source->{pool}; - $vm->{$name}->{source_ip} = $source->{ip} if $source->{ip}; - $vm->{$name}->{source_path} = $source->{path} if $source->{path}; - $vm->{$name}->{lsync} = 0; + my $vm_type = vm_exists($source); + $job->{vm_type} = $vm_type; + $source->{vm_type} = $vm_type; - &$add_job($vm, $name); - } + die "VM $source->{vmid} doesn't exist\n" if $source->{vmid} && !$vm_type; - write_cron($cfg); + die "Config already exists\n" if $cfg->{$job->{source}}->{$job->{name}}; - write_config($cfg); + #check if vm has zfs disks if not die; + get_disks($source, 1) if $source->{vmid}; - unlock($lock_fh); - close($lock_fh); + update_cron($job); + update_state($job); eval { sync($param) if !$param->{skip}; @@ -385,99 +522,112 @@ sub init { } } +sub get_job { + my ($param) = @_; + + my $cfg = read_cron(); + + if (!$cfg->{$param->{source}}->{$param->{name}}) { + die "Job with source $param->{source} and name $param->{name} does not exist\n" ; + } + my $job = $cfg->{$param->{source}}->{$param->{name}}; + $job->{name} = $param->{name}; + $job->{source} = $param->{source}; + $job = add_state_to_job($job); + + return $job; +} + sub destroy_job { my ($param) = @_; - modify_configs($param->{name}, $param->{source}, 1); + my $job = get_job($param); + $job->{state} = "del"; + update_cron($job); + update_state($job); } sub sync { my ($param) = @_; - open(my $lock_fh, ">", $LOCKFILE) || die "cannot open Lock File: $LOCKFILE\n"; + + my $lock_fh = IO::File->new("> $LOCKFILE"); + die "Can't open Lock File: $LOCKFILE $!\n" if !$lock_fh; lock($lock_fh); - my $cfg = read_config(); + my $date = get_date(); + my $job; + eval { + $job = get_job($param); + }; - my $name = $param->{name} ? $param->{name} : "default"; - my $max_snap = $param->{maxsnap} ? $param->{maxsnap} : 1; - my $method = $param->{method} ? $param->{method} : "ssh"; + if ($job && $job->{state} eq "syncing") { + die "Job --source $param->{source} --name $param->{name} is syncing at the moment"; + } my $dest = parse_target($param->{dest}); my $source = parse_target($param->{source}); my $sync_path = sub { - my ($source, $name, $cfg, $max_snap, $dest, $method) = @_; - - ($source->{old_snap},$source->{last_snap}) = snapshot_get($source, $dest, $max_snap, $name); - - my $job_status = check_config($source, $name, $cfg) if $cfg; - die "VM Status: $job_status syncing will not done!\n" if ($job_status && !($job_status eq "ok" || $job_status eq "stoped")); - - if ($job_status) { - my $conf_name = $source->{abs_path}; - $conf_name = $source->{vmid} if $source->{vmid}; - $cfg->{$conf_name}->{$name}->{status} = "syncing"; - write_config($cfg); - } + my ($source, $dest, $job, $param, $date) = @_; - my $date = undef; + ($source->{old_snap}, $source->{last_snap}) = snapshot_get($source, $dest, $param->{maxsnap}, $param->{name}); - eval{ - $date = snapshot_add($source, $dest, $name); + snapshot_add($source, $dest, $param->{name}, $date); - send_image($source, $dest, $method, $param->{verbose}, $param->{limit}); + send_image($source, $dest, $param); - snapshot_destroy($source, $dest, $method, $source->{old_snap}) if ($source->{destroy} && $source->{old_snap}); - }; - if(my $err = $@) { - if ($job_status) { - my $conf_name = $source->{abs_path}; - $conf_name = $source->{vmid} if $source->{vmid}; - $cfg->{$conf_name}->{$name}->{status} = "error"; - write_config($cfg); - - my $source_target = $source->{ip} ? $source->{ip}.":" : ''; - $source_target .= $source->{vmid} ? $source->{vmid} : $source->{abs_path}; - - write_cron($cfg); - unlock($lock_fh); - close($lock_fh); - } - die "$err\n"; - } + snapshot_destroy($source, $dest, $param->{method}, $source->{old_snap}) if ($source->{destroy} && $source->{old_snap}); - if ($job_status) { - my $conf_name = $source->{abs_path}; - $conf_name = $source->{vmid} if $source->{vmid}; - $cfg->{$conf_name}->{$name}->{status} = "ok"; - $cfg->{$conf_name}->{$name}->{lsync} = $date; - - write_config($cfg); - } }; - $param->{method} = "ssh" if !$param->{method}; + my $vm_type = vm_exists($source); + $source->{vm_type} = $vm_type; - if ($source->{vmid}) { - die "VM $source->{vmid} doesn't exist\n" if !vm_exists($source); - my $disks = get_disks($source); - - foreach my $disk (sort keys %{$disks}) { - $source->{abs_path} = $disks->{$disk}->{pool}; - $source->{abs_path} .= "\/$disks->{$disk}->{path}" if $disks->{$disk}->{path}; - - $source->{pool} = $disks->{$disk}->{pool}; - $source->{path} = "\/$disks->{$disk}->{path}"; + if ($job) { + $job->{state} = "syncing"; + $job->{vm_type} = $vm_type if !$job->{vm_type}; + update_state($job); + } - &$sync_path($source, $name, $cfg, $max_snap, $dest, $method); + eval{ + if ($source->{vmid}) { + die "VM $source->{vmid} doesn't exist\n" if !$vm_type; + my $disks = get_disks($source); + + foreach my $disk (sort keys %{$disks}) { + $source->{all} = $disks->{$disk}->{all}; + $source->{pool} = $disks->{$disk}->{pool}; + $source->{path} = $disks->{$disk}->{path} if $disks->{$disk}->{path}; + $source->{last_part} = $disks->{$disk}->{last_part}; + &$sync_path($source, $dest, $job, $param, $date); + } + 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); } - if ($method eq "ssh") { - send_config($source, $dest,'ssh'); + }; + if(my $err = $@) { + if ($job) { + $job->{state} = "error"; + update_state($job); + unlock($lock_fh); + close($lock_fh); + print "Job --source $param->{source} --name $param->{name} got an ERROR!!!\nERROR Message:\n"; } - } else { - &$sync_path($source, $name, $cfg, $max_snap, $dest, $method); + die "$err\n"; + } + + if ($job) { + $job->{state} = "ok"; + $job->{lsync} = $date; + update_state($job); } + unlock($lock_fh); close($lock_fh); } @@ -485,46 +635,48 @@ sub sync { sub snapshot_get{ my ($source, $dest, $max_snap, $name) = @_; - my $cmd = "zfs list -r -t snapshot -Ho name, -S creation "; - - $cmd .= $source->{abs_path}; - $cmd = "ssh root\@$source->{ip} ".$cmd if $source->{ip}; + my $cmd = []; + push @$cmd, 'ssh', "root\@$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 = 1; + my $index = 0; my $line = ""; my $last_snap = undef; + my $old_snap; while ($raw && $raw =~ s/^(.*?)(\n|$)//) { $line = $1; - $last_snap = $line if $index == 1; - if ($index == $max_snap) { - $source->{destroy} = 1; - last; - }; - $index++; + 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++; + if ($index == $max_snap) { + $source->{destroy} = 1; + last; + }; + } } - $line =~ m/^(.+)\@(rep_$name\_.+)(\n|$)/; - return ($2, $last_snap) if $2; + return ($old_snap, $last_snap) if $last_snap; return undef; } sub snapshot_add { - my ($source, $dest, $name) = @_; - - my $date = get_date(); + my ($source, $dest, $name, $date) = @_; my $snap_name = "rep_$name\_".$date; $source->{new_snap} = $snap_name; - my $path = "$source->{abs_path}\@$snap_name"; - - my $cmd = "zfs snapshot $path"; - $cmd = "ssh root\@$source->{ip} $cmd" if $source->{ip}; + my $path = "$source->{all}\@$snap_name"; + my $cmd = []; + push @$cmd, 'ssh', "root\@$source->{ip}", '--', if $source->{ip}; + push @$cmd, 'zfs', 'snapshot', $path; eval{ run_cmd($cmd); }; @@ -533,7 +685,6 @@ sub snapshot_add { snapshot_destroy($source, $dest, 'ssh', $snap_name); die "$err\n"; } - return $date; } sub write_cron { @@ -542,30 +693,30 @@ sub write_cron { my $text = "SHELL=/bin/sh\n"; $text .= "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n"; - open(my $fh, '>', "$CRONJOBS") || die "Could not open file: $!\n"; + my $fh = IO::File->new("> $CRONJOBS"); + die "Could not open file: $!\n" if !$fh; foreach my $source (sort keys%{$cfg}) { foreach my $sync_name (sort keys%{$cfg->{$source}}) { next if $cfg->{$source}->{$sync_name}->{status} ne 'ok'; - $text .= "*/$cfg->{$source}->{$sync_name}->{interval} * * * * root "; - $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)); @@ -573,15 +724,22 @@ sub write_cron { } sub get_disks { - my ($target) = @_; + my ($target, $get_err) = @_; + + my $cmd = []; + push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip}; - my $cmd = ""; - $cmd = "ssh root\@$target->{ip} " if $target->{ip}; - $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}, $get_err); return $disks; } @@ -590,9 +748,12 @@ 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 $output if 0 != $?; + die "COMMAND:\n\t$cmd\nGET ERROR:\n\t$output" if 0 != $?; chomp($output); print Dumper $output if $DEBUG; @@ -601,77 +762,109 @@ sub run_cmd { } sub parse_disks { - my ($text, $ip) = @_; + 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 + $error = ($line =~ m/backup=(?i:1|yes|on|true)/) if $vm_type eq 'lxc'; + my $disk = undef; my $stor = undef; - my $is_disk = $line =~ m/^(virtio|ide|scsi|sata){1}\d+: /; - 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; - } + 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; + } + } - die "disk is not on ZFS Storage\n" if $is_disk && !$disk && $line !~ m/cdrom/; + } else { + print "Disk: \"$line\" will not include in pve-sync\n" if $get_err || $error; + next; + } - if($disk && $line !~ m/none/ && $line !~ m/cdrom/ ) { - my $cmd = ""; - $cmd .= "ssh root\@$ip " if $ip; - $cmd .= "pvesm path $stor$disk"; - my $path = run_cmd($cmd); + my $cmd = []; + push @$cmd, 'ssh', "root\@$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"; - if ($path =~ m/^\/dev\/zvol\/(\w+).*(\/$disk)$/) { + $num++; + } elsif ($vm_type eq 'lxc' && $path =~ m/^\/(\w+.+)(\/(\w+.*))*(\/$disk)$/) { - $disks->{$num}->{pool} = $1; - $disks->{$num}->{path} = $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 $zfscmd = "zfs destroy "; - my $name = "$source->{path}\@$snap"; + my @zfscmd = ('zfs', 'destroy'); + my $snapshot = "$source->{all}\@$snap"; eval { if($source->{ip} && $method eq 'ssh'){ - run_cmd("ssh root\@$source->{ip} $zfscmd $source->{pool}$name"); + run_cmd(['ssh', "root\@$source->{ip}", '--', @zfscmd, $snapshot]); } else { - run_cmd("$zfscmd $source->{pool}$name"); + 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', "root\@$dest->{ip}", '--') : (); - my $path = ""; - $path ="$dest->{path}" if $dest->{path}; + my $path = "$dest->{all}\/$source->{last_part}"; - my @dir = split(/\//, $source->{path}); eval { - run_cmd("$ssh $zfscmd $dest->{pool}$path\/$dir[@dir-1]\@$snap "); + run_cmd([@ssh, @zfscmd, "$path\@$snap"]); }; if (my $erro = $@) { warn "WARN: $erro"; @@ -680,230 +873,159 @@ sub snapshot_destroy { } sub snapshot_exist { - my ($source ,$dest, $method) = @_; + my ($source , $dest, $method) = @_; - my $cmd = ""; - $cmd = "ssh root\@$dest->{ip} " if $dest->{ip}; - $cmd .= "zfs list -rt snapshot -Ho name $dest->{pool}"; - $cmd .= "$dest->{path}" if $dest->{path}; - my @dir = split(/\//, $source->{path}); - $cmd .= "\/$dir[@dir-1]\@$source->{old_snap}"; + 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 $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}$/; } } sub send_image { - my ($source, $dest, $method, $verbose, $limit) = @_; + my ($source, $dest, $param) = @_; - my $cmd = ""; + my $cmd = []; - $cmd .= "ssh root\@$source->{ip} " if $source->{ip}; - $cmd .= "zfs send "; - $cmd .= "-v " if $verbose; + 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, $method)) { - $cmd .= "-i $source->{abs_path}\@$source->{old_snap} $source->{abs_path}\@$source->{new_snap} "; - } else { - $cmd .= "$source->{abs_path}\@$source->{new_snap} "; + 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}"; - if ($limit){ - my $bwl = $limit*1024; - $cmd .= "| cstream -t $bwl"; + if ($param->{limit}){ + my $bwl = $param->{limit}*1024; + push @$cmd, \'|', 'cstream', '-t', $bwl; } - $cmd .= "| "; - $cmd .= "ssh root\@$dest->{ip} " if $dest->{ip}; - $cmd .= "zfs recv $dest->{pool}"; - $cmd .= "$dest->{path}" if $dest->{path}; + my $target = "$dest->{all}/$source->{last_part}"; + $target =~ s!/+!/!g; - my @dir = split(/\//,$source->{path}); - $cmd .= "\/$dir[@dir-1]\@$source->{new_snap}"; + push @$cmd, \'|'; + push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip}; + push @$cmd, 'zfs', 'recv', '-F', '--'; + push @$cmd, "$target"; - eval { - run_cmd($cmd) - }; - - if (my $erro = $@) { - snapshot_destroy($source, undef, $method, $source->{new_snap}); - die $erro; - }; -} - - -sub send_config{ - my ($source, $dest, $method) = @_; - - 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}:$QEMU_CONF$source->{vmid}.conf root\@$dest->{ip}:$CONFIG_PATH$source->{vmid}.conf.$source->{new_snap}"); - } elsif ($dest->{ip}) { - run_cmd("ssh root\@$dest->{ip} mkdir $CONFIG_PATH -p"); - run_cmd("scp $QEMU_CONF$source->{vmid}.conf root\@$dest->{ip}:$CONFIG_PATH$source->{vmid}.conf.$source->{new_snap}"); - } elsif ($source->{ip}) { - run_cmd("mkdir $CONFIG_PATH -p"); - run_cmd("scp root\@$source->{ip}:$QEMU_CONF$source->{vmid}.conf $CONFIG_PATH$source->{vmid}.conf.$source->{new_snap}"); - } + eval { + run_cmd($cmd) + }; - if ($source->{destroy}){ - if($dest->{ip}){ - run_cmd("ssh root\@$dest->{ip} rm -f $CONFIG_PATH$source->{vmid}.conf.$source->{old_snap}"); - } else { - run_cmd("rm -f $CONFIG_PATH$source->{vmid}.conf.$source->{old_snap}"); - } - } + if (my $erro = $@) { + snapshot_destroy($source, undef, $param->{method}, $source->{new_snap}); + die $erro; + }; } -} -sub get_date { - my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time); - my $datestamp = sprintf ("%04d-%02d-%02d_%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec); - return $datestamp; -} + sub send_config{ + my ($source, $dest, $method) = @_; -sub status { - my $cfg = read_config(); + 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 $status_list = sprintf("%-25s%-15s%-10s\n", "SOURCE", "NAME", "STATUS"); + my $config_dir = $dest->{last_part} ? "${CONFIG_PATH}/$dest->{last_part}" : $CONFIG_PATH; - foreach my $source (sort keys%{$cfg}) { - foreach my $sync_name (sort keys%{$cfg->{$source}}) { - my $status; - - my $source_name = $source; - - $source_name = $cfg->{$source}->{$sync_name}->{source_ip}.":$source" if $cfg->{$source}->{$sync_name}->{source_ip}; + $dest_target_new = $config_dir.'/'.$dest_target_new; - $status = sprintf("%-10s", $cfg->{$source}->{$sync_name}->{status}); + if ($method eq 'ssh'){ + if ($dest->{ip} && $source->{ip}) { + run_cmd(['ssh', "root\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]); + run_cmd(['scp', '--', "root\@[$source->{ip}]:$source_target", "root\@[$dest->{ip}]:$dest_target_new"]); + } elsif ($dest->{ip}) { + run_cmd(['ssh', "root\@$dest->{ip}", '--', 'mkdir', '-p', '--', $config_dir]); + run_cmd(['scp', '--', $source_target, "root\@[$dest->{ip}]:$dest_target_new"]); + } elsif ($source->{ip}) { + run_cmd(['mkdir', '-p', '--', $config_dir]); + run_cmd(['scp', '--', "root\@$source->{ip}:$source_target", $dest_target_new]); + } - $status_list .= sprintf("%-25s%-15s", cut_target_width($source_name,25), cut_target_width($sync_name,15)); - $status_list .= "$status\n"; + if ($source->{destroy}){ + my $dest_target_old ="${config_dir}/$source->{vmid}.conf.$source->{vm_type}.$source->{old_snap}"; + if($dest->{ip}){ + run_cmd(['ssh', "root\@$dest->{ip}", '--', 'rm', '-f', '--', $dest_target_old]); + } else { + run_cmd(['rm', '-f', '--', $dest_target_old]); + } + } + } elsif ($method eq 'local') { + run_cmd(['mkdir', '-p', '--', $config_dir]); + run_cmd(['cp', $source_target, $dest_target_new]); } } - return $status_list; -} - -sub enable_job { - my ($param) = @_; - - modify_configs($param->{name}, $param->{source}, 4); -} - -sub disable_job { - my ($param) = @_; - - modify_configs($param->{name}, $param->{source}, 2); -} - -sub modify_configs { - my ($name, $sou, $op) = @_; - - $name = $name ? $name : "default"; - - open(my $lock_fh, ">", $LOCKFILE) || die "cannot open Lock File: $LOCKFILE\n"; - lock($lock_fh); - - my $cfg = read_config(); + sub get_date { + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time); + my $datestamp = sprintf ("%04d-%02d-%02d_%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec); - my $source = parse_target($sou); - - my $change_configs = sub { - my ($path, $name, $cfg, $op) = @_; - - die "Source does not exist!\n" if !$cfg->{$path}; - - die "Sync Name does not exist!\n" if !$cfg->{$path}->{$name}; - - my $source = $cfg->{$path}->{$name}->{source_ip} ? "$cfg->{$path}->{$name}->{source_ip}:" : ''; - - $source .= $cfg->{$path}->{$name}->{source_pool} if $cfg->{$path}->{$name}->{source_pool}; - $source .= $cfg->{$path}->{$name}->{source_path} ? $cfg->{$path}->{$name}->{source_path} :''; - - my $dest = $cfg->{$path}->{$name}->{dest_ip} ? $cfg->{$path}->{$name}->{dest_ip} :""; - $dest .= $cfg->{$path}->{$name}->{dest_pool} if $cfg->{$path}->{$name}->{dest_pool}; - $dest .= $cfg->{$path}->{$name}->{dest_path} ? $cfg->{$path}->{$name}->{dest_path} :''; - - if($op == 1) { - delete $cfg->{$path}->{$name}; - - delete $cfg->{$path} if keys%{$cfg->{$path}} == 0; + return $datestamp; + } - write_config($cfg); - } - if($op == 1 || $op == 2) { + sub status { + my $cfg = read_cron(); - if ($op == 2) { + my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS"); - $cfg->{$path}->{$name}->{status} = "stoped"; + my $states = read_state(); - write_config($cfg); + foreach my $source (sort keys%{$cfg}) { + foreach my $sync_name (sort keys%{$cfg->{$source}}) { + $status_list .= sprintf("%-25s", cut_target_width($source, 25)); + $status_list .= sprintf("%-25s", cut_target_width($sync_name, 25)); + $status_list .= "$states->{$source}->{$sync_name}->{state}\n"; } - write_cron($cfg); - } elsif($op == 4) { - my $job = {}; - - $cfg->{$path}->{$name}->{status} = "ok"; - - write_config($cfg); - - write_cron($cfg); } - }; - - if ($source->{vmid}) { - my $path = $source->{vmid}; + return $status_list; + } - &$change_configs($path, $name, $cfg, $op) - } else { - my $path = $source->{pool}; - $path .= $source->{path} if $source->{path}; + sub enable_job { + my ($param) = @_; - &$change_configs($path, $name, $cfg, $op); + my $job = get_job($param); + $job->{state} = "ok"; + update_state($job); + update_cron($job); } - unlock($lock_fh); - close($lock_fh); -} + sub disable_job { + my ($param) = @_; -my $command = $ARGV[0]; + my $job = get_job($param); + $job->{state} = "stopped"; + update_state($job); + update_cron($job); + } -my $commands = {'destroy' => 1, - 'create' => 1, - 'sync' => 1, - 'list' => 1, - 'status' => 1, - 'help' => 1, - 'enable' => 1, - 'disable' => 1}; + my $command = $ARGV[0]; -if (!$command || !$commands->{$command}) { - usage(); - die "\n"; -} + my $commands = {'destroy' => 1, + 'create' => 1, + 'sync' => 1, + 'list' => 1, + 'status' => 1, + 'help' => 1, + 'enable' => 1, + 'disable' => 1}; -my $dest = ''; -my $source = ''; -my $verbose = ''; -my $interval = ''; -my $limit = ''; -my $maxsnap = ''; -my $name = ''; -my $skip = ''; + if (!$command || !$commands->{$command}) { + usage(); + die "\n"; + } -my $help_sync = "$PROGNAME sync -dest -source [OPTIONS]\n + my $help_sync = "$PROGNAME sync -dest -source [OPTIONS]\n \twill sync one time\n \t-dest\tstring\n \t\tthe destination target is like [IP:][/Path]\n @@ -917,13 +1039,10 @@ my $help_sync = "$PROGNAME sync -dest -source [OPTIONS]\n \t-source\tstring\n \t\tthe source can be an or [IP:][/Path]\n"; -my $help_create = "$PROGNAME create -dest -source [OPTIONS]/n + my $help_create = "$PROGNAME create -dest -source [OPTIONS]/n \tCreate a sync Job\n -\t-dest\tstringn\n +\t-dest\tstring\n \t\tthe destination target is like [IP]:[/Path]\n -\t-interval\tinteger\n -\t\tthe interval in min in witch the zfs will sync, -\t\tdefault is 15 min\n \t-limit\tinteger\n \t\tmax sync speed in kBytes/s, default unlimited\n \t-maxsnap\tstring\n @@ -935,184 +1054,148 @@ my $help_create = "$PROGNAME create -dest -source [OPTIONS]/n \t-source\tstring\n \t\tthe source can be an or [IP:][/Path]\n"; -my $help_destroy = "$PROGNAME destroy -source [OPTIONS]\n + my $help_destroy = "$PROGNAME destroy -source [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 or [IP:][/Path]\n"; -my $help_help = "$PROGNAME help [OPTIONS]\n + my $help_help = "$PROGNAME help [OPTIONS]\n \tGet help about specified command.\n \t\tstring\n \t\tCommand name\n \t-verbose\tboolean\n \t\tVerbose output format.\n"; -my $help_list = "$PROGNAME list\n + my $help_list = "$PROGNAME list\n \tGet a List of all scheduled Sync Jobs\n"; -my $help_status = "$PROGNAME status\n + my $help_status = "$PROGNAME status\n \tGet the status of all scheduled Sync Jobs\n"; -my $help_enable = "$PROGNAME enable -source [OPTIONS]\n + my $help_enable = "$PROGNAME enable -source [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 or [IP:][/Path]\n"; -my $help_disable = "$PROGNAME disable -source [OPTIONS]\n + my $help_disable = "$PROGNAME disable -source [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 or [IP:][/Path]\n"; -sub help { - my ($command) = @_; + sub help { + my ($command) = @_; - switch($command){ - case 'help' - { + if ($command eq 'help') { die "$help_help\n"; - } - case 'sync' - { + + } elsif ($command eq 'sync') { die "$help_sync\n"; - } - case 'destroy' - { + + } elsif ($command eq 'destroy') { die "$help_destroy\n"; - } - case 'create' - { + + } elsif ($command eq 'create') { die "$help_create\n"; - } - case 'list' - { + + } elsif ($command eq 'list') { die "$help_list\n"; - } - case 'status' - { + + } elsif ($command eq 'status') { die "$help_status\n"; - } - case 'enable' - { - die "$help_enable\n"; - } - case 'disable' - { + + } elsif ($command eq 'enable') { die "$help_enable\n"; + + } elsif ($command eq 'disable') { + die "$help_disable\n"; + } + } -} + my @arg = @ARGV; + my $param = parse_argv(@arg); -my $err = GetOptions ('dest=s' => \$dest, - 'source=s' => \$source, - 'verbose' => \$verbose, - 'interval=i' => \$interval, - 'limit=i' => \$limit, - 'maxsnap=i' => \$maxsnap, - 'name=s' => \$name, - 'skip' => \$skip); - -if ($err == 0) { - die "can't parse options\n"; -} + if ($command eq 'destroy') { + die "$help_destroy\n" if !$param->{source}; -my $param; -$param->{dest} = $dest; -$param->{source} = $source; -$param->{verbose} = $verbose; -$param->{interval} = $interval; -$param->{limit} = $limit; -$param->{maxsnap} = $maxsnap; -$param->{name} = $name; -$param->{skip} = $skip; - -switch($command) { - case "destroy" - { - die "$help_destroy\n" if !$source; - check_target($source); + check_target($param->{source}); destroy_job($param); - } - case "sync" - { - die "$help_sync\n" if !$source || !$dest; - check_target($source); - check_target($dest); + + } elsif ($command eq '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 !$source || !$dest; - check_target($source); - check_target($dest); + + } elsif ($command eq 'create') { + die "$help_create\n" if !$param->{source} || !$param->{dest}; + + check_target($param->{source}); + check_target($param->{dest}); init($param); - } - case "status" - { + + } elsif ($command eq 'status') { print status(); - } - case "list" - { + + } elsif ($command eq 'list') { print list(); - } - case "help" - { + + } elsif ($command eq 'help') { my $help_command = $ARGV[1]; + if ($help_command && $commands->{$help_command}) { print help($help_command); + } - if ($verbose){ + if ($param->{verbose} == 1){ exec("man $PROGNAME"); + } else { usage(1); + } - } - case "enable" - { - die "$help_enable\n" if !$source; - check_target($source); + + } elsif ($command eq 'enable') { + die "$help_enable\n" if !$param->{source}; + + check_target($param->{source}); enable_job($param); - } - case "disable" - { - die "$help_disable\n" if !$source; - check_target($source); - disable_job($param); - } -} -sub usage { - my ($help) = @_; - - print("ERROR:\tno command specified\n") if !$help; - print("USAGE:\t$PROGNAME [ARGS] [OPTIONS]\n"); - print("\t$PROGNAME help [] [OPTIONS]\n\n"); - print("\t$PROGNAME create -dest -source [OPTIONS]\n"); - print("\t$PROGNAME destroy -source [OPTIONS]\n"); - print("\t$PROGNAME disable -source [OPTIONS]\n"); - print("\t$PROGNAME enable -source [OPTIONS]\n"); - print("\t$PROGNAME list\n"); - print("\t$PROGNAME status\n"); - print("\t$PROGNAME sync -dest -source [OPTIONS]\n"); -} + } elsif ($command eq 'disable') { + die "$help_disable\n" if !$param->{source}; -sub check_target { - my ($target) = @_; + check_target($param->{source}); + disable_job($param); - chomp($target); + } - if($target !~ m/(\d+.\d+.\d+.\d+:)?([\w\-\_\/]+)(\/.+)?/) { - print("ERROR:\t$target is not valid.\n\tUse [IP:][/Path]!\n"); - return 1; + sub usage { + my ($help) = @_; + + print("ERROR:\tno command specified\n") if !$help; + print("USAGE:\t$PROGNAME [ARGS] [OPTIONS]\n"); + print("\t$PROGNAME help [] [OPTIONS]\n\n"); + print("\t$PROGNAME create -dest -source [OPTIONS]\n"); + print("\t$PROGNAME destroy -source [OPTIONS]\n"); + print("\t$PROGNAME disable -source [OPTIONS]\n"); + print("\t$PROGNAME enable -source [OPTIONS]\n"); + print("\t$PROGNAME list\n"); + print("\t$PROGNAME status\n"); + print("\t$PROGNAME sync -dest -source [OPTIONS]\n"); + } + + sub check_target { + my ($target) = @_; + parse_target($target); } - return undef; -} __END__ @@ -1138,69 +1221,65 @@ pve-zsync help [OPTIONS] pve-zsync create -dest -source [OPTIONS] - Create a sync Job + Create a sync Job - -dest string + -dest string the destination target is like [IP]:[/Path] - -interval integer - - the interval in min in witch the zfs will sync, default is 15 min - - -limit integer + -limit integer max sync speed in kBytes/s, default unlimited - -maxsnap string + -maxsnap string how much snapshots will be kept before get erased, default 1 - -name string + -name string name of the sync job, if not set it is default - -skip boolean + -skip boolean if this flag is set it will skip the first sync - -source string + -source string the source can be an or [IP:][/Path] pve-zsync destroy -source [OPTIONS] - remove a sync Job from the scheduler + remove a sync Job from the scheduler - -name string + -name string name of the sync job, if not set it is default - -source string + -source string the source can be an or [IP:][/Path] pve-zsync disable -source [OPTIONS] - pause a sync job + pause a sync job - -name string + -name string name of the sync job, if not set it is default - -source string + -source string the source can be an or [IP:][/Path] pve-zsync enable -source [OPTIONS] - enable a syncjob and reset error + enable a syncjob and reset error - -name string + -name string name of the sync job, if not set it is default - -source string + -source string the source can be an or [IP:][/Path] pve-zsync list @@ -1223,16 +1302,16 @@ pve-zsync sync -dest -source [OPTIONS] max sync speed in kBytes/s, default unlimited - -maxsnap integer + -maxsnap integer how much snapshots will be kept before get erased, default 1 - -name string + -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 + -source string the source can be an or [IP:][/Path] @@ -1240,6 +1319,8 @@ pve-zsync sync -dest -source [OPTIONS] This Tool helps you to sync your VM or directory which stored on ZFS between 2 servers. This tool also has the capability to add jobs to cron so the sync will be automatically done. +The default syncing interval is set to 15 min, if you want to change this value you can do this in /etc/cron.d/pve-zsync. +To config cron see man crontab. =head2 PVE ZFS Storage sync Tool @@ -1252,9 +1333,11 @@ pve-zsync create -source=100 -dest=192.168.1.2:zfspool =head1 IMPORTANT FILES -Where the cron jobs are stored /etc/cron.d/pve-zsync -Where the VM config get copied on the destination machine /var/pve-zsync/ -Where the config is stored /var/pve-zsync/ +Cron jobs and config are stored at /etc/cron.d/pve-zsync + +The VM config get copied on the destination machine to /var/lib/pve-zsync/ + +=head1 COPYRIGHT AND DISCLAIMER Copyright (C) 2007-2015 Proxmox Server Solutions GmbH