Make sure to keep some snapshots around even if they stop coming.

This commit is contained in:
2025-10-31 15:40:19 +00:00
parent 5a9d5de5c4
commit 5b3b4ea2ed

View File

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