# 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');
+my $command = $ARGV[0];
+
+if (defined($command) && $command ne 'help' && $command ne 'printpod') {
+ check_bin ('cstream');
+ check_bin ('zfs');
+ check_bin ('ssh');
+ check_bin ('scp');
+}
$SIG{TERM} = $SIG{QUIT} = $SIG{PIPE} = $SIG{HUP} = $SIG{KILL} = $SIG{INT} =
sub {
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);
}
sub get_disks {
- my ($target, $get_err) = @_;
+ my ($target) = @_;
my $cmd = [];
push @$cmd, 'ssh', "root\@$target->{ip}", '--', if $target->{ip};
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;
}
}
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 =~ /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
- $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;
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;
}
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"]);
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);};
my $bwl = $param->{limit}*1024;
push @$cmd, \'|', 'cstream', '-t', $bwl;
}
- my $target = "$dest->{all}/$source->{last_part}";
+ my $target = "$dest->{all}";
+ $target .= "/$source->{last_part}" if $source->{last_part};
$target =~ s!/+!/!g;
push @$cmd, \'|';
- push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip};
- push @$cmd, 'zfs', 'recv', '-F', '--';
- push @$cmd, "$target";
+ push @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@$dest->{ip}", '--' if $dest->{ip};
+ push @$cmd, 'zfs', 'recv', '-F', '--';
+ push @$cmd, "$target";
- eval {
- run_cmd($cmd)
- };
+ eval {
+ run_cmd($cmd)
+ };
- if (my $erro = $@) {
- snapshot_destroy($source, undef, $param->{method}, $source->{new_snap});
- die $erro;
- };
- }
+ if (my $erro = $@) {
+ snapshot_destroy($source, undef, $param->{method}, $source->{new_snap});
+ die $erro;
+ };
+}
- sub send_config{
- my ($source, $dest, $method) = @_;
+sub send_config{
+ my ($source, $dest, $method) = @_;
- 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 $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;
+ my $config_dir = $dest->{last_part} ? "${CONFIG_PATH}/$dest->{last_part}" : $CONFIG_PATH;
- $dest_target_new = $config_dir.'/'.$dest_target_new;
+ $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_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]);
- }
+ 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]);
+ }
- 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]);
- }
+ 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]);
}
+ } elsif ($method eq 'local') {
+ run_cmd(['mkdir', '-p', '--', $config_dir]);
+ run_cmd(['cp', $source_target, $dest_target_new]);
}
+}
- 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);
+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;
- }
+ return $datestamp;
+}
- sub status {
- my $cfg = read_cron();
+sub status {
+ my $cfg = read_cron();
- my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS");
+ my $status_list = sprintf("%-25s%-25s%-10s\n", "SOURCE", "NAME", "STATUS");
- my $states = read_state();
+ 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("%-25s", cut_target_width($sync_name, 25));
- $status_list .= "$states->{$source}->{$sync_name}->{state}\n";
- }
+ 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";
}
-
- return $status_list;
}
- sub enable_job {
- my ($param) = @_;
+ return $status_list;
+}
- my $job = get_job($param);
- $job->{state} = "ok";
- update_state($job);
- update_cron($job);
- }
+sub enable_job {
+ my ($param) = @_;
- sub disable_job {
- my ($param) = @_;
+ my $job = get_job($param);
+ $job->{state} = "ok";
+ update_state($job);
+ update_cron($job);
+}
- my $job = get_job($param);
- $job->{state} = "stopped";
- update_state($job);
- update_cron($job);
- }
+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 $cmd_help = {
+ destroy => qq{
+$PROGNAME destroy -source <string> [OPTIONS]
- if (!$command || !$commands->{$command}) {
- usage();
- die "\n";
- }
+ remove a sync Job from the scheduler
- my $help_sync = "$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
-\twill sync one time\n
-\t-dest\tstring\n
-\t\tthe destination target is like [IP:]<Pool>[/Path]\n
-\t-limit\tinteger\n
-\t\tmax sync speed in kBytes/s, default unlimited\n
-\t-maxsnap\tinteger\n
-\t\thow much snapshots will be kept before get erased, default 1/n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default.
-\tIt is only necessary if scheduler allready contains this source.\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
- my $help_create = "$PROGNAME create -dest <string> -source <string> [OPTIONS]/n
-\tCreate a sync Job\n
-\t-dest\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\tstring\n
-\t\thow much snapshots will be kept before get erased, default 1\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-skip\tboolean\n
-\t\tif this flag is set it will skip the first sync\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
- my $help_destroy = "$PROGNAME destroy -source <string> [OPTIONS]\n
-\tremove a sync Job from the scheduler\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
- my $help_help = "$PROGNAME help <cmd> [OPTIONS]\n
-\tGet help about specified command.\n
-\t<cmd>\tstring\n
-\t\tCommand name\n
-\t-verbose\tboolean\n
-\t\tVerbose output format.\n";
-
- my $help_list = "$PROGNAME list\n
-\tGet a List of all scheduled Sync Jobs\n";
-
- my $help_status = "$PROGNAME status\n
-\tGet the status of all scheduled Sync Jobs\n";
-
- my $help_enable = "$PROGNAME enable -source <string> [OPTIONS]\n
-\tenable a syncjob and reset error\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
- my $help_disable = "$PROGNAME disable -source <string> [OPTIONS]\n
-\tpause a syncjob\n
-\t-name\tstring\n
-\t\tname of the sync job, if not set it is default\n
-\t-source\tstring\n
-\t\tthe source can be an <VMID> or [IP:]<ZFSPool>[/Path]\n";
-
- sub help {
- my ($command) = @_;
-
- 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";
+ -name string
- }
+ name of the sync job, if not set it is default
- }
+ -source string
- my @arg = @ARGV;
- my $param = parse_argv(@arg);
+ the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+ },
+ create => qq{
+$PROGNAME create -dest <string> -source <string> [OPTIONS]
- if ($command eq 'destroy') {
- die "$help_destroy\n" if !$param->{source};
+ Create a sync Job
- check_target($param->{source});
- destroy_job($param);
+ -dest string
- } elsif ($command eq 'sync') {
- die "$help_sync\n" if !$param->{source} || !$param->{dest};
+ the destination target is like [IP]:<Pool>[/Path]
- check_target($param->{source});
- check_target($param->{dest});
- sync($param);
+ -limit integer
- } elsif ($command eq 'create') {
- die "$help_create\n" if !$param->{source} || !$param->{dest};
+ max sync speed in kBytes/s, default unlimited
- check_target($param->{source});
- check_target($param->{dest});
- init($param);
+ -maxsnap string
- } elsif ($command eq 'status') {
- print status();
+ how much snapshots will be kept before get erased, default 1
- } elsif ($command eq 'list') {
- print list();
+ -name string
- } elsif ($command eq 'help') {
- my $help_command = $ARGV[1];
+ name of the sync job, if not set it is default
- if ($help_command && $commands->{$help_command}) {
- print help($help_command);
+ -skip boolean
- }
- if ($param->{verbose} == 1){
- exec("man $PROGNAME");
+ if this flag is set it will skip the first sync
- } else {
- usage(1);
+ -source string
- }
+ the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+ },
+ sync => qq{
+$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
- } elsif ($command eq 'enable') {
- die "$help_enable\n" if !$param->{source};
+ will sync one time
- check_target($param->{source});
- enable_job($param);
+ -dest string
- } elsif ($command eq 'disable') {
- die "$help_disable\n" if !$param->{source};
+ the destination target is like [IP:]<Pool>[/Path]
- check_target($param->{source});
- disable_job($param);
+ -limit integer
- }
+ max sync speed in kBytes/s, default unlimited
- sub usage {
- my ($help) = @_;
-
- print("ERROR:\tno command specified\n") if !$help;
- print("USAGE:\t$PROGNAME <COMMAND> [ARGS] [OPTIONS]\n");
- print("\t$PROGNAME help [<cmd>] [OPTIONS]\n\n");
- print("\t$PROGNAME create -dest <string> -source <string> [OPTIONS]\n");
- print("\t$PROGNAME destroy -source <string> [OPTIONS]\n");
- print("\t$PROGNAME disable -source <string> [OPTIONS]\n");
- print("\t$PROGNAME enable -source <string> [OPTIONS]\n");
- print("\t$PROGNAME list\n");
- print("\t$PROGNAME status\n");
- print("\t$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n");
- }
+ -maxsnap integer
- sub check_target {
- my ($target) = @_;
- parse_target($target);
- }
+ how much snapshots will be kept before get erased, default 1
-__END__
+ -name string
-=head1 NAME
+ name of the sync job, if not set it is default.
+ It is only necessary if scheduler allready contains this source.
-pve-zsync - PVE ZFS Replication Manager
+ -source string
-=head1 SYNOPSIS
+ the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-pve-zsync <COMMAND> [ARGS] [OPTIONS]
+ -verbose boolean
+
+ print out the sync progress.
+ },
+ list => qq{
+$PROGNAME list
-pve-zsync help <cmd> [OPTIONS]
+ Get a List of all scheduled Sync Jobs
+ },
+ status => qq{
+$PROGNAME status
+
+ Get the status of all scheduled Sync Jobs
+ },
+ help => qq{
+$PROGNAME help <cmd> [OPTIONS]
Get help about specified command.
-verbose boolean
Verbose output format.
+ },
+ enable => qq{
+$PROGNAME enable -source <string> [OPTIONS]
-pve-zsync create -dest <string> -source <string> [OPTIONS]
-
- Create a sync Job
-
- -dest string
+ enable a syncjob and reset error
- the destination target is like [IP]:<Pool>[/Path]
+ -name string
- -limit integer
+ name of the sync job, if not set it is default
- max sync speed in kBytes/s, default unlimited
+ -source string
- -maxsnap string
+ the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+ },
+ disable => qq{
+$PROGNAME disable -source <string> [OPTIONS]
- how much snapshots will be kept before get erased, default 1
+ pause a sync job
-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]
-
-pve-zsync destroy -source <string> [OPTIONS]
+ the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+ },
+ printpod => 'internal command',
- remove a sync Job from the scheduler
+};
- -name string
+if (!$command) {
+ usage(); die "\n";
+} elsif (!$cmd_help->{$command}) {
+ print "ERROR: unknown command '$command'";
+ usage(1); die "\n";
+}
- name of the sync job, if not set it is default
+my @arg = @ARGV;
+my $param = parse_argv(@arg);
- -source string
+sub check_params {
+ for (@_) {
+ die "$cmd_help->{$command}\n" if !$param->{$_};
+ }
+}
- the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+if ($command eq 'destroy') {
+ check_params(qw(source));
-pve-zsync disable -source <string> [OPTIONS]
+ check_target($param->{source});
+ destroy_job($param);
- pause a sync job
+} elsif ($command eq 'sync') {
+ check_params(qw(source dest));
- -name string
+ check_target($param->{source});
+ check_target($param->{dest});
+ sync($param);
- name of the sync job, if not set it is default
+} elsif ($command eq 'create') {
+ check_params(qw(source dest));
- -source string
+ check_target($param->{source});
+ check_target($param->{dest});
+ init($param);
- the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+} elsif ($command eq 'status') {
+ print status();
-pve-zsync enable -source <string> [OPTIONS]
+} elsif ($command eq 'list') {
+ print list();
- enable a syncjob and reset error
+} elsif ($command eq 'help') {
+ my $help_command = $ARGV[1];
- -name string
+ if ($help_command && $cmd_help->{$help_command}) {
+ die "$cmd_help->{$command}\n";
- name of the sync job, if not set it is default
-
- -source string
+ }
+ if ($param->{verbose}) {
+ exec("man $PROGNAME");
- the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-pve-zsync list
+ } else {
+ usage(1);
- Get a List of all scheduled Sync Jobs
+ }
-pve-zsync status
+} elsif ($command eq 'enable') {
+ check_params(qw(source));
- Get the status of all scheduled Sync Jobs
+ check_target($param->{source});
+ enable_job($param);
-pve-zsync sync -dest <string> -source <string> [OPTIONS]
+} elsif ($command eq 'disable') {
+ check_params(qw(source));
- will sync one time
+ check_target($param->{source});
+ disable_job($param);
- -dest string
+} elsif ($command eq 'printpod') {
+ print_pod();
+}
- the destination target is like [IP:]<Pool>[/Path]
+sub usage {
+ my ($help) = @_;
+
+ print("ERROR:\tno command specified\n") if !$help;
+ print("USAGE:\t$PROGNAME <COMMAND> [ARGS] [OPTIONS]\n");
+ print("\t$PROGNAME help [<cmd>] [OPTIONS]\n\n");
+ print("\t$PROGNAME create -dest <string> -source <string> [OPTIONS]\n");
+ print("\t$PROGNAME destroy -source <string> [OPTIONS]\n");
+ print("\t$PROGNAME disable -source <string> [OPTIONS]\n");
+ print("\t$PROGNAME enable -source <string> [OPTIONS]\n");
+ print("\t$PROGNAME list\n");
+ print("\t$PROGNAME status\n");
+ print("\t$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n");
+}
- -limit integer
+sub check_target {
+ my ($target) = @_;
+ parse_target($target);
+}
- max sync speed in kBytes/s, default unlimited
+sub print_pod {
- -maxsnap integer
+ my $synopsis = join("\n", sort values %$cmd_help);
- how much snapshots will be kept before get erased, default 1
+ print <<EOF;
+=head1 NAME
- -name string
+pve-zsync - PVE ZFS Replication Manager
- name of the sync job, if not set it is default.
- It is only necessary if scheduler allready contains this source.
+=head1 SYNOPSIS
- -source string
+pve-zsync <COMMAND> [ARGS] [OPTIONS]
- the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
+$synopsis
=head1 DESCRIPTION
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
+
+EOF
+}