From 92f59df25a5779da2c7d59e535d7963771696d6d Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Wed, 21 Oct 2026 20:38:46 +0800 Subject: [PATCH] fix(youko): date restic store snapshots by name, not mtime On its next run the unit deletes every snapshot whose name stamp is older than 14 days, whatever its mtime. Assisted-by: DeepSeek Harness 0.2.1-rc.2 (claude-opus-5-5) --- .../homelab-services/restic-server-snapshots.nix | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/hosts/12kingdoms-youko/homelab-services/restic-server-snapshots.nix b/hosts/12kingdoms-youko/homelab-services/restic-server-snapshots.nix index bc69bd5f..392b1b4a 100644 --- a/hosts/12kingdoms-youko/homelab-services/restic-server-snapshots.nix +++ b/hosts/12kingdoms-youko/homelab-services/restic-server-snapshots.nix @@ -29,16 +29,23 @@ in path = [ pkgs.btrfs-progs pkgs.coreutils - pkgs.findutils ]; script = '' set -euo pipefail stamp=$(date +%Y%m%d%H%M%S) btrfs subvolume snapshot -r ${source} ${snapshotRoot}/backups.$stamp # Drop snapshots older than ${toString keepDays} days. The desktop cannot - # reach this tree, so it cannot shorten the window. - find ${snapshotRoot} -mindepth 1 -maxdepth 1 -type d -mtime +${toString keepDays} -print0 | - xargs -0 -r -n1 btrfs subvolume delete + # reach this tree, so it cannot shorten the window. Age comes from the + # name: a snapshot's root directory keeps the source's mtime, not the + # time it was taken, so `find -mtime` cannot date it. + cutoff=$(date -d '-${toString keepDays} days' +%Y%m%d%H%M%S) + for snap in ${snapshotRoot}/backups.*; do + [ -e "$snap" ] || continue + taken=''${snap##*/backups.} + if [[ $taken =~ ^[0-9]{14}$ && $taken < $cutoff ]]; then + btrfs subvolume delete "$snap" + fi + done ''; unitConfig.RequiresMountsFor = source; };