From 333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 29 May 2021 15:18:04 +0200 Subject: surtr: zfs snapshots --- hosts/surtr/zfs.nix | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 hosts/surtr/zfs.nix (limited to 'hosts/surtr/zfs.nix') diff --git a/hosts/surtr/zfs.nix b/hosts/surtr/zfs.nix new file mode 100644 index 00000000..72cc79e3 --- /dev/null +++ b/hosts/surtr/zfs.nix @@ -0,0 +1,89 @@ +{ 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"; Persistent = true; }; + hourly = { OnCalendar = "hourly"; Persistent = true; }; + daily = { OnCalendar = "daily"; Persistent = true; }; + monthly = { OnCalendar = "monthly"; Persistent = true; }; + yearly = { OnCalendar = "yearly"; 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" = + { device = "surtr/local/nix"; + fsType = "zfs"; + }; + + "/root" = + { device = "surtr/safe/home-root"; + fsType = "zfs"; + neededForBoot = true; + }; + + "/var/lib/systemd" = + { device = "surtr/local/var-lib-systemd"; + fsType = "zfs"; + neededForBoot = true; + }; + + "/var/log" = + { device = "surtr/local/var-log"; + fsType = "zfs"; + }; + + "/home" = + { device = "surtr/safe/home"; + fsType = "zfs"; + }; + }; + + 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); + }; +} -- cgit v1.2.3