2023-07-19 14:16:56 +03:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-07-29 15:39:54 +03:00
|
|
|
}:
|
|
|
|
let
|
2023-09-30 17:06:41 +03:00
|
|
|
mkPreHook = zfs_name: i: ''
|
2023-09-15 09:55:26 +03:00
|
|
|
set -x
|
2023-09-18 10:06:44 +03:00
|
|
|
sleep ${toString i}
|
2023-09-18 07:51:47 +03:00
|
|
|
mkdir "$RUNTIME_DIRECTORY/snapshot"
|
2023-09-15 11:04:20 +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) \
|
2023-09-18 07:51:47 +03:00
|
|
|
"$RUNTIME_DIRECTORY/snapshot"
|
|
|
|
cd "$RUNTIME_DIRECTORY/snapshot"
|
2023-07-19 14:16:56 +03:00
|
|
|
'';
|
2024-07-29 15:39:54 +03:00
|
|
|
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";
|
|
|
|
|
2024-07-29 15:39:54 +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
|
|
|
|
2023-09-11 17:25:12 +03:00
|
|
|
dirs = lib.mkOption {
|
2024-07-29 15:39:54 +03:00
|
|
|
default = { };
|
2023-10-01 23:14:05 +03:00
|
|
|
type = listOf (submodule {
|
|
|
|
options = {
|
2024-07-29 15:39:54 +03:00
|
|
|
mountpoint = lib.mkOption { type = path; };
|
|
|
|
repo = lib.mkOption { type = str; };
|
|
|
|
paths = lib.mkOption { type = listOf str; };
|
2023-10-01 23:14:05 +03:00
|
|
|
patterns = lib.mkOption {
|
|
|
|
type = listOf str;
|
2024-07-29 15:39:54 +03:00
|
|
|
default = [ ];
|
2023-07-20 06:58:47 +03:00
|
|
|
};
|
2024-05-02 17:41:13 +03:00
|
|
|
prune = lib.mkOption {
|
|
|
|
type = anything;
|
2024-07-29 15:39:54 +03:00
|
|
|
default = { };
|
2024-05-02 17:41:13 +03:00
|
|
|
};
|
2024-07-29 15:39:54 +03:00
|
|
|
backup_at = lib.mkOption { type = str; };
|
2023-10-01 23:14:05 +03:00
|
|
|
};
|
|
|
|
});
|
2023-07-19 14:16:56 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
config =
|
|
|
|
with config.mj.base.zfsborg;
|
2023-09-11 17:25:12 +03:00
|
|
|
lib.mkIf enable {
|
2024-07-29 15:39:54 +03:00
|
|
|
systemd.services = lib.listToAttrs (
|
|
|
|
lib.imap0 (
|
|
|
|
i: attr:
|
|
|
|
let
|
2023-09-18 07:51:47 +03:00
|
|
|
svcName = "borgbackup-job-${lib.strings.sanitizeDerivationName attr.mountpoint}-${toString i}";
|
|
|
|
in
|
2024-07-29 15:39:54 +03:00
|
|
|
lib.nameValuePair svcName { serviceConfig.RuntimeDirectory = svcName; }
|
|
|
|
) dirs
|
|
|
|
);
|
2023-07-19 14:16:56 +03:00
|
|
|
|
2023-09-11 17:25:12 +03:00
|
|
|
services.borgbackup.jobs = builtins.listToAttrs (
|
2023-09-11 17:33:16 +03:00
|
|
|
lib.imap0 (
|
2024-07-29 15:39:54 +03:00
|
|
|
i: attrs:
|
|
|
|
let
|
2023-09-11 17:33:16 +03:00
|
|
|
mountpoint = builtins.getAttr "mountpoint" attrs;
|
|
|
|
fs = builtins.getAttr mountpoint config.fileSystems;
|
|
|
|
in
|
2024-07-29 15:39:54 +03:00
|
|
|
assert fs.fsType == "zfs";
|
|
|
|
assert lib.assertMsg config.mj.base.unitstatus.enable
|
2023-09-11 17:33:16 +03:00
|
|
|
"config.mj.base.unitstatus.enable must be true";
|
2024-07-29 15:39:54 +03:00
|
|
|
lib.nameValuePair "${lib.strings.sanitizeDerivationName mountpoint}-${toString i}" (
|
|
|
|
{
|
|
|
|
inherit (attrs) repo paths;
|
2023-10-01 23:14:05 +03:00
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
doInit = true;
|
|
|
|
encryption = {
|
|
|
|
mode = "repokey-blake2";
|
|
|
|
passCommand = "cat ${config.mj.base.zfsborg.passwordPath}";
|
|
|
|
};
|
|
|
|
extraArgs = "--remote-path=borg1";
|
|
|
|
compression = "auto,zstd,10";
|
|
|
|
extraCreateArgs = "--chunker-params buzhash,10,23,16,4095";
|
|
|
|
startAt = attrs.backup_at;
|
|
|
|
preHook = mkPreHook fs.device i;
|
|
|
|
prune.keep = {
|
|
|
|
within = "1d";
|
|
|
|
daily = 7;
|
|
|
|
weekly = 4;
|
|
|
|
monthly = 3;
|
|
|
|
};
|
|
|
|
environment =
|
|
|
|
{
|
|
|
|
BORG_HOST_ID =
|
|
|
|
let
|
|
|
|
h = config.networking;
|
|
|
|
in
|
|
|
|
"${h.hostName}.${h.domain}@${h.hostId}";
|
2023-09-11 17:48:03 +03:00
|
|
|
}
|
2024-07-29 15:39:54 +03:00
|
|
|
// lib.optionalAttrs (sshKeyPath != null) {
|
|
|
|
BORG_RSH = ''ssh -i "${config.mj.base.zfsborg.sshKeyPath}"'';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (attrs ? patterns) { inherit (attrs) patterns; }
|
|
|
|
// lib.optionalAttrs (attrs ? prune) { inherit (attrs) prune; }
|
|
|
|
)
|
|
|
|
) dirs
|
2023-09-11 17:25:12 +03:00
|
|
|
);
|
2023-07-20 09:31:26 +03:00
|
|
|
|
2024-07-29 15:39:54 +03:00
|
|
|
mj.base.unitstatus.units =
|
|
|
|
let
|
|
|
|
sanitized = map lib.strings.sanitizeDerivationName (lib.catAttrs "mountpoint" dirs);
|
|
|
|
in
|
2023-09-11 17:33:16 +03:00
|
|
|
lib.imap0 (i: name: "borgbackup-job-${name}-${toString i}") sanitized;
|
2023-09-11 17:25:12 +03:00
|
|
|
};
|
2023-07-19 14:16:56 +03:00
|
|
|
}
|