From 9c37ce946826258711e15cd55ee221fa8fe02256 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 19 Feb 2022 15:51:23 +0100 Subject: zfssnap: ... --- hosts/surtr/zfs.nix | 59 ++-------------------------------------------- modules/zfssnap/zfssnap.py | 18 ++++++++++---- 2 files changed, 16 insertions(+), 61 deletions(-) diff --git a/hosts/surtr/zfs.nix b/hosts/surtr/zfs.nix index 3e42ef3e..3a69fd96 100644 --- a/hosts/surtr/zfs.nix +++ b/hosts/surtr/zfs.nix @@ -1,32 +1,5 @@ { pkgs, config, ... }: -let - snapshotNames = ["frequent" "hourly" "daily" "monthly" "yearly"]; - snapshotCount = { - frequent = 24; - hourly = 24; - daily = 30; - monthly = 12; - yearly = 5; - }; - snapshotTimerConfig = { - frequent = { OnCalendar = "*:0/5 UTC"; Persistent = true; }; - hourly = { OnCalendar = "hourly UTC"; Persistent = true; }; - daily = { OnCalendar = "daily UTC"; Persistent = true; }; - monthly = { OnCalendar = "monthly UTC"; Persistent = true; }; - yearly = { OnCalendar = "yearly UTC"; Persistent = true; }; - }; - snapshotDescr = { - frequent = "few minutes"; - hourly = "hour"; - daily = "day"; - monthly = "month"; - yearly = "year"; - }; - - zfs = config.boot.zfs.package; - - autosnapPackage = pkgs.zfstools.override { inherit zfs; }; -in { +{ config = { fileSystems = { "/nix" = @@ -69,35 +42,7 @@ in { }; }; - systemd.services = - let mkSnapService = snapName: { - name = "zfs-snapshot-${snapName}"; - value = { - description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; - }; - restartIfChanged = false; - - preStart = '' - ${zfs}/bin/zfs set com.sun:auto-snapshot=true surtr/safe - ''; - }; - }; - in builtins.listToAttrs (map mkSnapService snapshotNames); - - systemd.timers = - let mkSnapTimer = snapName: { - name = "zfs-snapshot-${snapName}"; - value = { - wantedBy = [ "timers.target" ]; - timerConfig = snapshotTimerConfig.${snapName}; - }; - }; - in builtins.listToAttrs (map mkSnapTimer snapshotNames); - + services.zfssnap.enable = true; services.zfs.trim.enable = false; services.zfs.autoScrub = { enable = true; diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index cb91564b..a05440a7 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -83,11 +83,21 @@ def prune(config, dry_run): keep.add(snap) within = config.gettimedelta('KEEP', 'within') - within_cutoff = _now() - within + if within > timedelta(seconds=0): + for base, snaps in items.items(): + time_ref = max(snaps, key=lambda snap: snap['creation'], default=None) + if not time_ref: + logger.warn('Nothing to keep for ‘{base}’') + continue - for base, snap in [(base, snap) for base, snaps in items.items() for snap in snaps]: - if snap['creation'] >= within_cutoff: - keep_because(base, snap['name'], 'within') + logger.info('Using ‘{time_ref["name"]}’ as time reference for ‘{base}’') + within_cutoff = time_ref['creation'] - within + + for snap in snaps: + if snap['creation'] >= within_cutoff: + keep_because(base, snap['name'], 'within') + else: + logger.warn('Skipping rule ‘within’ since retention period is zero') prune_timezone = config.gettimezone('KEEP', 'timezone', fallback=tzlocal) -- cgit v1.2.3