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.
This commit is contained in:
Ricardo M. Correia 2015-03-20 03:49:25 +01:00
parent b0a51de6c1
commit 6197fdc02d

@ -46,6 +46,8 @@ let
dataPools = unique (filter (pool: !(elem pool rootPools)) allPools); dataPools = unique (filter (pool: !(elem pool rootPools)) allPools);
snapshotNames = [ "frequent" "hourly" "daily" "weekly" "monthly" ];
in in
{ {
@ -306,60 +308,41 @@ in
}) })
(mkIf enableAutoSnapshots { (mkIf enableAutoSnapshots {
systemd.services."zfs-snapshot-frequent" = { systemd.services = let
description = "ZFS auto-snapshotting every 15 mins"; descr = name: if name == "frequent" then "15 mins"
after = [ "zfs-import.target" ]; else if name == "hourly" then "hour"
serviceConfig = { else if name == "daily" then "day"
Type = "oneshot"; else if name == "weekly" then "week"
ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} frequent ${toString cfgSnapshots.frequent}"; else if name == "monthly" then "month"
}; else throw "unknown snapshot name";
restartIfChanged = false; numSnapshots = name: builtins.getAttr name cfgSnapshots;
startAt = "*:15,30,45"; 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.services."zfs-snapshot-hourly" = { systemd.timers = let
description = "ZFS auto-snapshotting every hour"; timer = name: if name == "frequent" then "*:15,30,45" else name;
after = [ "zfs-import.target" ]; in builtins.listToAttrs (map (snapName:
serviceConfig = { {
Type = "oneshot"; name = "zfs-snapshot-${snapName}";
ExecStart = "${zfsAutoSnap} ${cfgSnapFlags} hourly ${toString cfgSnapshots.hourly}"; value = {
}; wantedBy = [ "timers.target" ];
restartIfChanged = false; timerConfig = {
startAt = "hourly"; OnCalendar = timer snapName;
}; Persistent = "yes";
};
systemd.services."zfs-snapshot-daily" = { };
description = "ZFS auto-snapshotting every day"; }) snapshotNames);
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";
};
}) })
]; ];
} }