diff options
-rw-r--r-- | hosts/surtr/zfs.nix | 59 | ||||
-rw-r--r-- | 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 @@ | |||
1 | { pkgs, config, ... }: | 1 | { pkgs, config, ... }: |
2 | let | 2 | { |
3 | snapshotNames = ["frequent" "hourly" "daily" "monthly" "yearly"]; | ||
4 | snapshotCount = { | ||
5 | frequent = 24; | ||
6 | hourly = 24; | ||
7 | daily = 30; | ||
8 | monthly = 12; | ||
9 | yearly = 5; | ||
10 | }; | ||
11 | snapshotTimerConfig = { | ||
12 | frequent = { OnCalendar = "*:0/5 UTC"; Persistent = true; }; | ||
13 | hourly = { OnCalendar = "hourly UTC"; Persistent = true; }; | ||
14 | daily = { OnCalendar = "daily UTC"; Persistent = true; }; | ||
15 | monthly = { OnCalendar = "monthly UTC"; Persistent = true; }; | ||
16 | yearly = { OnCalendar = "yearly UTC"; Persistent = true; }; | ||
17 | }; | ||
18 | snapshotDescr = { | ||
19 | frequent = "few minutes"; | ||
20 | hourly = "hour"; | ||
21 | daily = "day"; | ||
22 | monthly = "month"; | ||
23 | yearly = "year"; | ||
24 | }; | ||
25 | |||
26 | zfs = config.boot.zfs.package; | ||
27 | |||
28 | autosnapPackage = pkgs.zfstools.override { inherit zfs; }; | ||
29 | in { | ||
30 | config = { | 3 | config = { |
31 | fileSystems = { | 4 | fileSystems = { |
32 | "/nix" = | 5 | "/nix" = |
@@ -69,35 +42,7 @@ in { | |||
69 | }; | 42 | }; |
70 | }; | 43 | }; |
71 | 44 | ||
72 | systemd.services = | 45 | services.zfssnap.enable = true; |
73 | let mkSnapService = snapName: { | ||
74 | name = "zfs-snapshot-${snapName}"; | ||
75 | value = { | ||
76 | description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; | ||
77 | after = [ "zfs-import.target" ]; | ||
78 | serviceConfig = { | ||
79 | Type = "oneshot"; | ||
80 | ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; | ||
81 | }; | ||
82 | restartIfChanged = false; | ||
83 | |||
84 | preStart = '' | ||
85 | ${zfs}/bin/zfs set com.sun:auto-snapshot=true surtr/safe | ||
86 | ''; | ||
87 | }; | ||
88 | }; | ||
89 | in builtins.listToAttrs (map mkSnapService snapshotNames); | ||
90 | |||
91 | systemd.timers = | ||
92 | let mkSnapTimer = snapName: { | ||
93 | name = "zfs-snapshot-${snapName}"; | ||
94 | value = { | ||
95 | wantedBy = [ "timers.target" ]; | ||
96 | timerConfig = snapshotTimerConfig.${snapName}; | ||
97 | }; | ||
98 | }; | ||
99 | in builtins.listToAttrs (map mkSnapTimer snapshotNames); | ||
100 | |||
101 | services.zfs.trim.enable = false; | 46 | services.zfs.trim.enable = false; |
102 | services.zfs.autoScrub = { | 47 | services.zfs.autoScrub = { |
103 | enable = true; | 48 | 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): | |||
83 | keep.add(snap) | 83 | keep.add(snap) |
84 | 84 | ||
85 | within = config.gettimedelta('KEEP', 'within') | 85 | within = config.gettimedelta('KEEP', 'within') |
86 | within_cutoff = _now() - within | 86 | if within > timedelta(seconds=0): |
87 | for base, snaps in items.items(): | ||
88 | time_ref = max(snaps, key=lambda snap: snap['creation'], default=None) | ||
89 | if not time_ref: | ||
90 | logger.warn('Nothing to keep for ‘{base}’') | ||
91 | continue | ||
87 | 92 | ||
88 | for base, snap in [(base, snap) for base, snaps in items.items() for snap in snaps]: | 93 | logger.info('Using ‘{time_ref["name"]}’ as time reference for ‘{base}’') |
89 | if snap['creation'] >= within_cutoff: | 94 | within_cutoff = time_ref['creation'] - within |
90 | keep_because(base, snap['name'], 'within') | 95 | |
96 | for snap in snaps: | ||
97 | if snap['creation'] >= within_cutoff: | ||
98 | keep_because(base, snap['name'], 'within') | ||
99 | else: | ||
100 | logger.warn('Skipping rule ‘within’ since retention period is zero') | ||
91 | 101 | ||
92 | prune_timezone = config.gettimezone('KEEP', 'timezone', fallback=tzlocal) | 102 | prune_timezone = config.gettimezone('KEEP', 'timezone', fallback=tzlocal) |
93 | 103 | ||