diff options
author | Ricardo M. Correia <rcorreia@wizy.org> | 2015-03-20 03:49:25 +0100 |
---|---|---|
committer | Ricardo M. Correia <rcorreia@wizy.org> | 2015-03-20 04:37:44 +0100 |
commit | 6197fdc02d20763e10beb73c0b6384c255f142b2 (patch) | |
tree | e1b218256602940d28c915a55d382f136862a538 /nixos/modules | |
parent | b0a51de6c10a7a1aba70c0a4aabe33a41bfb4c2c (diff) | |
download | nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar.gz nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar.bz2 nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar.lz nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar.xz nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.tar.zst nixlib-6197fdc02d20763e10beb73c0b6384c255f142b2.zip |
nixos/zfs: Refactor auto-snapshots and make them persistent
If you power off your machine frequently, you could miss the execution of some snapshots. This is more troublesome the more infrequently the snapshots are triggered. For example, monthly snapshots only execute at exactly midnight on the first day of the month. If you only have your machine powered on at that time with probability 50%, then half the snapshots won't be triggered. This means that if you wanted to keep 3 monthly snapshots, then instead of keeping 3 months' worth of snapshotted data as you expected, you would end up with snapshots spanning back 6 months. Adding the "Persistent = yes" option to auto-snapshot timer units makes a missed snapshot execute when booting up the machine.
Diffstat (limited to 'nixos/modules')
-rw-r--r-- | nixos/modules/tasks/filesystems/zfs.nix | 91 |
1 files changed, 37 insertions, 54 deletions
diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index 71054ff238e0..a4b01bc2fd18 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -46,6 +46,8 @@ let dataPools = unique (filter (pool: !(elem pool rootPools)) allPools); + snapshotNames = [ "frequent" "hourly" "daily" "weekly" "monthly" ]; + in { @@ -306,60 +308,41 @@ in }) (mkIf enableAutoSnapshots { - systemd.services."zfs-snapshot-frequent" = { - description = "ZFS auto-snapshotting every 15 mins"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} frequent ${toString cfgSnapshots.frequent}"; - }; - restartIfChanged = false; - startAt = "*:15,30,45"; - }; - - systemd.services."zfs-snapshot-hourly" = { - description = "ZFS auto-snapshotting every hour"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} hourly ${toString cfgSnapshots.hourly}"; - }; - restartIfChanged = false; - startAt = "hourly"; - }; - - systemd.services."zfs-snapshot-daily" = { - description = "ZFS auto-snapshotting every day"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} daily ${toString cfgSnapshots.daily}"; - }; - restartIfChanged = false; - startAt = "daily"; - }; - - systemd.services."zfs-snapshot-weekly" = { - description = "ZFS auto-snapshotting every week"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} weekly ${toString cfgSnapshots.weekly}"; - }; - restartIfChanged = false; - startAt = "weekly"; - }; - - systemd.services."zfs-snapshot-monthly" = { - description = "ZFS auto-snapshotting every month"; - after = [ "zfs-import.target" ]; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} monthly ${toString cfgSnapshots.monthly}"; - }; - restartIfChanged = false; - startAt = "monthly"; - }; + systemd.services = let + descr = name: if name == "frequent" then "15 mins" + else if name == "hourly" then "hour" + else if name == "daily" then "day" + else if name == "weekly" then "week" + else if name == "monthly" then "month" + else throw "unknown snapshot name"; + numSnapshots = name: builtins.getAttr name cfgSnapshots; + in builtins.listToAttrs (map (snapName: + { + name = "zfs-snapshot-${snapName}"; + value = { + description = "ZFS auto-snapshotting every ${descr snapName}"; + after = [ "zfs-import.target" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} ${snapName} ${toString (numSnapshots snapName)}"; + }; + restartIfChanged = false; + }; + }) snapshotNames); + + systemd.timers = let + timer = name: if name == "frequent" then "*:15,30,45" else name; + in builtins.listToAttrs (map (snapName: + { + name = "zfs-snapshot-${snapName}"; + value = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = timer snapName; + Persistent = "yes"; + }; + }; + }) snapshotNames); }) ]; } |