Skip to content

Commit

Permalink
znapzendzetup: support toggling or inheriting a dst_N_autocreation pr…
Browse files Browse the repository at this point in the history
…operty

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Feb 8, 2024
1 parent 46e5fc0 commit 3d5a636
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bin/znapzendzetup
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,54 @@ sub main {

last;
};
/^enable-dst-autocreation$/ && do {
$opts->{dst} = pop @ARGV;
$opts->{src} = pop @ARGV;
if (!defined $opts->{src}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: source argument for option $mainOpt was not provided\n");
}
if (!defined $opts->{dst}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: destination argument for option $mainOpt was not provided\n");
}
$zConfig->enableBackupSetDstAutoCreation($opts->{src}, $opts->{dst})
or die "ERROR: cannot enable backup config for $opts->{src} destination $opts->{dst}. Did you create this config?\n";

last;
};
/^disable-dst-autocreation$/ && do {
$opts->{dst} = pop @ARGV;
$opts->{src} = pop @ARGV;
if (!defined $opts->{src}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: source argument for option $mainOpt was not provided\n");
}
if (!defined $opts->{dst}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: destination argument for option $mainOpt was not provided\n");
}
$zConfig->disableBackupSetDstAutoCreation($opts->{src}, $opts->{dst})
or die "ERROR: cannot disable backup config for $opts->{src} destination $opts->{dst}. Did you create this config?\n";

last;
};
/^inherit-dst-autocreation$/ && do {
$opts->{dst} = pop @ARGV;
$opts->{src} = pop @ARGV;
if (!defined $opts->{src}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: source argument for option $mainOpt was not provided\n");
}
if (!defined $opts->{dst}) {
pod2usage(-exitval => 'NOEXIT');
die ("ERROR: destination argument for option $mainOpt was not provided\n");
}
$zConfig->inheritBackupSetDstAutoCreation($opts->{src}, $opts->{dst})
or die "ERROR: cannot disable backup config for $opts->{src} destination $opts->{dst}. Did you create this config?\n";

last;
};
/^list$/ && do {
GetOptions($opts, (@ROOT_EXEC_OPTIONS, qw(recursive|r inherited))) or exit 1;

Expand Down Expand Up @@ -591,6 +639,12 @@ and where 'command' and its unique options is one of the following:
disable-dst <src_dataset> <DST_key>
enable-dst-autocreation <src_dataset> <DST_key>
disable-dst-autocreation <src_dataset> <DST_key>
inherit-dst-autocreation <src_dataset> <DST_key>
list [--recursive] [--inherited] [src_dataset...]
export <src_dataset>
Expand Down
136 changes: 136 additions & 0 deletions lib/ZnapZend/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,142 @@ sub disableBackupSetDst {
return 0;
}

sub enableBackupSetDstAutoCreation {
my $self = shift;
my $dataSet = shift;
my $dest = shift;
my $recurse = shift; # may be undef
my $inherit = shift; # may be undef

$self->zfs->dataSetExists($dataSet) or die "ERROR: dataset $dataSet does not exist\n";

$self->backupSets($self->zfs->getDataSetProperties($dataSet, $recurse, $inherit));

if (@{$self->backupSets}){
my %cfg = %{$self->backupSets->[0]};

if ( !($dest =~ /^dst_[^_]+$/) ) {
if ($cfg{'dst_' . $dest}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $dest;
} elsif ($dest =~ /^DST:/) {
my $desttemp = $dest;
$desttemp =~ s/^DST:// ;
if ($cfg{'dst_' . $desttemp}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $desttemp;
}
}
# TODO: Else search by value of 'dst_N' as a "(remote@)dataset"
}

if ($cfg{$dest}) {
if ($cfg{$dest . '_autocreation'}) {
$cfg{$dest . '_autocreation'} = 'on';
}
} else {
die "ERROR: dataset $dataSet backup plan does not have destination $dest\n";
}
$self->setBackupSet(\%cfg);

return 1;
}

return 0;
}

sub disableBackupSetDstAutoCreation {
my $self = shift;
my $dataSet = shift;
my $dest = shift;
my $recurse = shift; # may be undef
my $inherit = shift; # may be undef

$self->zfs->dataSetExists($dataSet) or die "ERROR: dataset $dataSet does not exist\n";

$self->backupSets($self->zfs->getDataSetProperties($dataSet, $recurse, $inherit));

if (@{$self->backupSets}){
my %cfg = %{$self->backupSets->[0]};

if ( !($dest =~ /^dst_[^_]+$/) ) {
if ($cfg{'dst_' . $dest}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $dest;
} elsif ($dest =~ /^DST:/) {
my $desttemp = $dest;
$desttemp =~ s/^DST:// ;
if ($cfg{'dst_' . $desttemp}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $desttemp;
}
}
# TODO: Else search by value of 'dst_N' as a "(remote@)dataset"
}

if ($cfg{$dest}) {
$cfg{$dest . '_autocreation'} = 'off';
} else {
die "ERROR: dataset $dataSet backup plan does not have destination $dest\n";
}
$self->setBackupSet(\%cfg);

return 1;
}

return 0;
}

sub inheritBackupSetDstAutoCreation {
my $self = shift;
my $dataSet = shift;
my $dest = shift;
my $recurse = shift; # may be undef
my $inherit = shift; # may be undef

$self->zfs->dataSetExists($dataSet) or die "ERROR: dataset $dataSet does not exist\n";

$self->backupSets($self->zfs->getDataSetProperties($dataSet, $recurse, $inherit));

if (@{$self->backupSets}){
my %cfg = %{$self->backupSets->[0]};

if ( !($dest =~ /^dst_[^_]+$/) ) {
if ($cfg{'dst_' . $dest}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $dest;
} elsif ($dest =~ /^DST:/) {
my $desttemp = $dest;
$desttemp =~ s/^DST:// ;
if ($cfg{'dst_' . $desttemp}) {
# User passed valid key of the destination config,
# convert to zfs attribute/perl struct name part
$dest = 'dst_' . $desttemp;
}
}
# TODO: Else search by value of 'dst_N' as a "(remote@)dataset"
}

if ($cfg{$dest}) {
if ($cfg{$dest . '_autocreation'}) {
$cfg{$dest . '_autocreation'} = undef;
}
} else {
die "ERROR: dataset $dataSet backup plan does not have destination $dest\n";
}
$self->setBackupSet(\%cfg);

return 1;
}

return 0;
}

1;

__END__
Expand Down

0 comments on commit 3d5a636

Please sign in to comment.