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";