Add pre and post snapshot scripts

This commit is contained in:
Julien Riou 2018-01-13 14:26:26 +01:00
parent eeedf38d57
commit 0cce7074c7
No known key found for this signature in database
GPG Key ID: BA3E15176E45E85D
3 changed files with 39 additions and 1 deletions

25
sanoid
View File

@ -455,6 +455,18 @@ sub take_snapshots {
if ( (scalar(@newsnaps)) > 0) {
foreach my $snap ( @newsnaps ) {
my $dataset = (split '@', $snap)[0];
my $presnapshotfailure = 0;
if ($config{$dataset}{'pre_snapshot_script'} and !$args{'readonly'}) {
$ENV{'SANOID_TARGET'} = $dataset;
if ($args{'verbose'}) { print "executing pre_snapshot_script '".$config{$dataset}{'pre_snapshot_script'}."' on dataset '$dataset'\n"; }
if (system($config{$dataset}{'pre_snapshot_script'}) != 0) {
warn "WARN: pre_snapshot_script failed, $?";
$config{$dataset}{'no_inconsistent_snapshot'} and next;
$presnapshotfailure = 1;
}
delete $ENV{'SANOID_TARGET'};
}
if ($args{'verbose'}) { print "taking snapshot $snap\n"; }
if (!$args{'readonly'}) {
system($zfs, "snapshot", "$snap") == 0
@ -462,6 +474,17 @@ sub take_snapshots {
# make sure we don't end up with multiple snapshots with the same ctime
sleep 1;
}
if ($config{$dataset}{'post_snapshot_script'} and !$args{'readonly'}) {
if (!$presnapshotfailure or $config{$dataset}{'force_post_snapshot_script'}) {
$ENV{'SANOID_TARGET'} = $dataset;
if ($args{'verbose'}) { print "executing post_snapshot_script '".$config{$dataset}{'post_snapshot_script'}."' on dataset '$dataset'\n"; }
if (system($config{$dataset}{'post_snapshot_script'}) != 0) {
warn "WARN: post_snapshot_script failed, $?";
$config{$dataset}{'no_inconsistent_snapshot'} and next;
}
delete $ENV{'SANOID_TARGET'};
}
}
}
$forcecacheupdate = 1;
%snaps = getsnaps(%config,$cacheTTL,$forcecacheupdate);
@ -661,7 +684,7 @@ sub init {
tie my %ini, 'Config::IniFiles', ( -file => $conf_file ) or die "FATAL: cannot load $conf_file - please create a valid local config file before running sanoid!";
# we'll use these later to normalize potentially true and false values on any toggle keys
my @toggles = ('autosnap','autoprune','monitor_dont_warn','monitor_dont_crit','monitor','recursive','process_children_only');
my @toggles = ('autosnap','autoprune','monitor_dont_warn','monitor_dont_crit','monitor','recursive','process_children_only','no_inconsistent_snapshot','force_post_snapshot_script');
my @istrue=(1,"true","True","TRUE","yes","Yes","YES","on","On","ON");
my @isfalse=(0,"false","False","FALSE","no","No","NO","off","Off","OFF");

View File

@ -67,6 +67,17 @@
daily_warn = 48
daily_crit = 60
[template_scripts]
### run script before snapshot
### dataset name will be supplied as an environment variable $SANOID_TARGET
pre_snapshot_script = /path/to/script.sh
### run script after snapshot
### dataset name will be supplied as an environment variable $SANOID_TARGET
post_snapshot_script = /path/to/script.sh
### don't take an inconsistent snapshot
#no_inconsistent_snapshot = yes
### run post_snapshot_script when pre_snapshot_script is failing
#force_post_snapshot_script = yes
[template_ignore]
autoprune = no

View File

@ -15,6 +15,10 @@ path =
recursive =
use_template =
process_children_only =
pre_snapshot_script =
post_snapshot_script =
no_inconsistent_snapshot =
force_post_snapshot_script =
# If any snapshot type is set to 0, we will not take snapshots for it - and will immediately
# prune any of those type snapshots already present.