From 82230f248c5220da903e97df4183e2ac41ab0459 Mon Sep 17 00:00:00 2001 From: Christoph Klaffl Date: Fri, 14 Jun 2019 07:53:40 +0200 Subject: [PATCH] improved dst handling to only suffix the duplicate hourly snapshot on DST change --- sanoid | 71 ++++++++++++++++++++++++++++++------- tests/1_one_year/run.sh | 2 +- tests/2_dst_handling/run.sh | 2 +- tests/common/lib.sh | 4 +-- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/sanoid b/sanoid index e54800f..224e91a 100755 --- a/sanoid +++ b/sanoid @@ -15,6 +15,7 @@ use File::Path; # for rmtree command in use_prune use Getopt::Long qw(:config auto_version auto_help); use Pod::Usage; # pod2usage use Time::Local; # to parse dates in reverse +use Capture::Tiny ':all'; my %args = ("configdir" => "/etc/sanoid"); GetOptions(\%args, "verbose", "debug", "cron", "readonly", "quiet", @@ -394,7 +395,7 @@ sub take_snapshots { my $lastpreferred; # to avoid duplicates with DST - my $dateSuffix = ""; + my $handleDst = 0; if ($type eq 'frequently') { my $frequentslice = int($datestamp{'min'} / $config{$section}{'frequent_period'}); @@ -420,7 +421,7 @@ sub take_snapshots { # timelocal doesn't take DST into account $lastpreferred += $dstOffset; # DST ended, avoid duplicates - $dateSuffix = "_y"; + $handleDst = 1; } if ($lastpreferred > time()) { $lastpreferred -= 60*60; } # preferred time is later this hour - so look at last hour's } elsif ($type eq 'daily') { @@ -503,11 +504,19 @@ sub take_snapshots { %datestamp = get_date(); # print "we should have had a $type snapshot of $path $maxage seconds ago; most recent is $newestage seconds old.\n"; + my $flags = ""; # use zfs (atomic) recursion if specified in config if ($config{$section}{'zfs_recursion'}) { - push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}${dateSuffix}_$type\@"); + $flags .= "r"; + } + if ($handleDst) { + $flags .= "d"; + } + + if ($flags ne "") { + push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}_$type\@$flags"); } else { - push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}${dateSuffix}_$type"); + push(@newsnaps, "$path\@autosnap_$datestamp{'sortable'}_$type"); } } } @@ -519,9 +528,18 @@ sub take_snapshots { my $extraMessage = ""; my @split = split '@', $snap, -1; my $recursiveFlag = 0; + my $dstHandling = 0; if (scalar(@split) == 3) { - $recursiveFlag = 1; - $extraMessage = " (zfs recursive)"; + my $flags = $split[2]; + if (index($flags, "r") != -1) { + $recursiveFlag = 1; + $extraMessage = " (zfs recursive)"; + chop $snap; + } + if (index($flags, "d") != -1) { + $dstHandling = 1; + chop $snap; + } chop $snap; } my $dataset = $split[0]; @@ -548,13 +566,40 @@ sub take_snapshots { } if ($args{'verbose'}) { print "taking snapshot $snap$extraMessage\n"; } if (!$args{'readonly'}) { - if ($recursiveFlag) { - system($zfs, "snapshot", "-r", "$snap") == 0 - or warn "CRITICAL ERROR: $zfs snapshot -r $snap failed, $?"; - } else { - system($zfs, "snapshot", "$snap") == 0 - or warn "CRITICAL ERROR: $zfs snapshot $snap failed, $?"; - } + my $stderr; + my $exit; + ($stderr, $exit) = tee_stderr { + if ($recursiveFlag) { + system($zfs, "snapshot", "-r", "$snap"); + } else { + system($zfs, "snapshot", "$snap"); + } + }; + + $exit == 0 or do { + if ($dstHandling) { + if ($stderr =~ /already exists/) { + $exit = 0; + $snap =~ s/_([a-z]+)$/dst_$1/g; + if ($args{'verbose'}) { print "taking dst snapshot $snap$extraMessage\n"; } + if ($recursiveFlag) { + system($zfs, "snapshot", "-r", "$snap") == 0 + or warn "CRITICAL ERROR: $zfs snapshot -r $snap failed, $?"; + } else { + system($zfs, "snapshot", "$snap") == 0 + or warn "CRITICAL ERROR: $zfs snapshot $snap failed, $?"; + } + } + } + }; + + $exit == 0 or do { + if ($recursiveFlag) { + warn "CRITICAL ERROR: $zfs snapshot -r $snap failed, $?"; + } else { + warn "CRITICAL ERROR: $zfs snapshot $snap failed, $?"; + } + }; } if ($config{$dataset}{'post_snapshot_script'}) { if (!$presnapshotfailure or $config{$dataset}{'force_post_snapshot_script'}) { diff --git a/tests/1_one_year/run.sh b/tests/1_one_year/run.sh index 88100da..fe76946 100755 --- a/tests/1_one_year/run.sh +++ b/tests/1_one_year/run.sh @@ -10,7 +10,7 @@ set -x POOL_NAME="sanoid-test-1" POOL_TARGET="" # root RESULT="/tmp/sanoid_test_result" -RESULT_CHECKSUM="68c67161a59d0e248094a66061972f53613067c9db52ad981030f36bc081fed7" +RESULT_CHECKSUM="92f2c7afba94b59e8a6f6681705f0aa3f1c61e4aededaa38281e0b7653856935" # UTC timestamp of start and end START="1483225200" diff --git a/tests/2_dst_handling/run.sh b/tests/2_dst_handling/run.sh index 7d7774e..3231631 100755 --- a/tests/2_dst_handling/run.sh +++ b/tests/2_dst_handling/run.sh @@ -13,7 +13,7 @@ set -x POOL_NAME="sanoid-test-2" POOL_TARGET="" # root RESULT="/tmp/sanoid_test_result" -RESULT_CHECKSUM="a916d9cd46f4b80f285d069f3497d02671bbb1bfd12b43ef93531cbdaf89d55c" +RESULT_CHECKSUM="846372ef238f2182b382c77a73ecddf99aa82f28cc9995bcc95592cc78305463" # UTC timestamp of start and end START="1509141600" diff --git a/tests/common/lib.sh b/tests/common/lib.sh index b070da2..904c98f 100644 --- a/tests/common/lib.sh +++ b/tests/common/lib.sh @@ -61,9 +61,9 @@ function saveSnapshotList { # clear the seconds for comparing if [ "$unamestr" == 'FreeBSD' ]; then - sed -i '' 's/\(autosnap_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]:[0-9][0-9]:\)[0-9][0-9]_/\100_/g' "${RESULT}" + sed -i '' 's/\(autosnap_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]:[0-9][0-9]:\)[0-9][0-9]/\100/g' "${RESULT}" else - sed -i 's/\(autosnap_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]:[0-9][0-9]:\)[0-9][0-9]_/\100_/g' "${RESULT}" + sed -i 's/\(autosnap_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]:[0-9][0-9]:\)[0-9][0-9]/\100/g' "${RESULT}" fi }