From 5b3b4ea2edb23589bc82c64e69a8a452bb5ef9ec Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Fri, 31 Oct 2025 15:40:19 +0000 Subject: [PATCH] Make sure to keep some snapshots around even if they stop coming. --- common/nfs-services-standby.nix | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/common/nfs-services-standby.nix b/common/nfs-services-standby.nix index d6e977c..d312b46 100644 --- a/common/nfs-services-standby.nix +++ b/common/nfs-services-standby.nix @@ -42,14 +42,24 @@ in # Cleanup old snapshots on standby (keep last 4 hours for HA failover) systemd.services.cleanup-services-standby-snapshots = { description = "Cleanup old btrfs snapshots in services-standby"; - path = [ pkgs.btrfs-progs pkgs.findutils ]; - + path = [ pkgs.btrfs-progs pkgs.findutils pkgs.coreutils ]; script = '' set -euo pipefail - # Keep last 4 hours of snapshots (48 snapshots at 5min intervals) - find /persist/services-standby -maxdepth 1 -name 'services@*' -mmin +240 -exec btrfs subvolume delete {} \; || true - ''; + # Keep at least 2 hours of snapshots (24 snapshots at 5min intervals) + MIN_KEEP=24 + + # Count existing snapshots + count=$(find /persist/services-standby -maxdepth 1 -name 'services@*' -type d | wc -l) + + # Only delete old snapshots if we have more than the minimum + if [ $count -gt $MIN_KEEP ]; then + # Delete snapshots older than 4 hours + find /persist/services-standby -maxdepth 1 -name 'services@*' -mmin +240 -exec btrfs subvolume delete {} \; || true + else + echo "Only $count snapshots found, keeping all (minimum: $MIN_KEEP)" + fi + ''; serviceConfig = { Type = "oneshot"; User = "root";