config/modules/base/zfsborg/default.nix

107 lines
3.3 KiB
Nix
Raw Normal View History

2023-07-19 14:16:56 +03:00
{
config,
lib,
pkgs,
...
}: let
mountLatest = mountpoint: zfs_name: ''
set -euo pipefail
${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest &>/dev/null || :
mkdir -p ${mountpoint}/.snapshot-latest
2023-07-26 15:36:11 +03:00
${pkgs.util-linux}/bin/mount -t zfs -o ro $(${pkgs.zfs}/bin/zfs list -H -t snapshot -o name ${zfs_name} | sort | tail -1) ${mountpoint}/.snapshot-latest
2023-07-19 14:16:56 +03:00
'';
umountLatest = mountpoint: ''
exec ${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest
'';
in {
2023-07-20 06:58:47 +03:00
options.mj.base.zfsborg = with lib.types; {
2023-07-19 14:16:56 +03:00
enable = lib.mkEnableOption "backup zfs snapshots with borg";
2023-07-20 15:02:38 +03:00
passwordPath = lib.mkOption {type = str;};
2023-07-26 15:10:39 +03:00
sshKeyPath = lib.mkOption {
type = nullOr path;
default = null;
};
2023-07-19 14:16:56 +03:00
mountpoints = lib.mkOption {
default = {};
2023-07-20 06:58:47 +03:00
type = attrsOf (submodule (
{...}: {
options = {
2023-07-26 12:59:19 +03:00
repo = lib.mkOption {type = str;};
2023-07-20 06:58:47 +03:00
paths = lib.mkOption {type = listOf path;};
patterns = lib.mkOption {
type = listOf str;
default = [];
2023-07-19 14:16:56 +03:00
};
2023-07-20 06:58:47 +03:00
backup_at = lib.mkOption {type = str;};
};
}
));
2023-07-19 14:16:56 +03:00
};
};
config = lib.mkIf config.mj.base.zfsborg.enable {
systemd.services."zfsborg-snapshot-dirs" = let
mountpoints = lib.unique (lib.attrNames config.mj.base.zfsborg.mountpoints);
in {
description = "zfsborg prepare snapshot directories";
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
ExecStart =
builtins.map
(d: "${pkgs.coreutils}/bin/mkdir -p ${d}/.snapshot-latest")
mountpoints;
RemainAfterExit = true;
};
};
services.borgbackup.jobs = lib.mapAttrs' (mountpoint: attrs: let
fs = builtins.getAttr mountpoint config.fileSystems;
in
assert fs.fsType == "zfs";
assert lib.assertMsg
2023-07-20 11:56:08 +03:00
config.mj.base.unitstatus.enable
"config.mj.base.unitstatus.enable must be true"; {
name = lib.strings.sanitizeDerivationName mountpoint;
value =
{
doInit = true;
2023-07-26 12:59:19 +03:00
repo = attrs.repo;
2023-07-20 11:56:08 +03:00
encryption = {
mode = "repokey-blake2";
2023-07-20 15:02:38 +03:00
passCommand = "cat ${config.mj.base.zfsborg.passwordPath}";
2023-07-19 14:16:56 +03:00
};
2023-07-20 11:56:08 +03:00
paths = attrs.paths;
extraArgs = "--remote-path=borg1";
compression = "auto,lzma";
startAt = attrs.backup_at;
readWritePaths = let p = mountpoint + "/.snapshot-latest"; in [p];
preHook = mountLatest mountpoint fs.device;
postHook = umountLatest mountpoint;
prune.keep = {
within = "1d";
daily = 7;
weekly = 4;
monthly = 3;
};
}
// lib.optionalAttrs (attrs ? patterns) {
patterns = attrs.patterns;
2023-07-26 15:10:39 +03:00
}
// lib.optionalAttrs (config.mj.base.zfsborg.sshKeyPath != null) {
2023-07-26 14:39:34 +03:00
environment.BORG_RSH = ''ssh -i "${config.mj.base.zfsborg.sshKeyPath}"'';
2023-07-20 11:56:08 +03:00
};
})
2023-07-19 14:16:56 +03:00
config.mj.base.zfsborg.mountpoints;
mj.base.unitstatus.units = let
mounts = config.mj.base.zfsborg.mountpoints;
sanitized = map lib.strings.sanitizeDerivationName (lib.attrNames mounts);
in
map (n: "borgbackup-job-${n}") sanitized;
2023-07-19 14:16:56 +03:00
};
}