clan-core/clanModules/borgbackup.nix

96 lines
3.0 KiB
Nix
Raw Normal View History

2023-12-06 16:12:38 +00:00
{ config, lib, pkgs, ... }:
let
cfg = config.clan.borgbackup;
in
{
2024-03-08 10:12:34 +00:00
options.clan.borgbackup.destinations = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
options = {
name = lib.mkOption {
type = lib.types.str;
default = name;
description = "the name of the backup job";
};
repo = lib.mkOption {
type = lib.types.str;
description = "the borgbackup repository to backup to";
};
rsh = lib.mkOption {
type = lib.types.str;
default = "ssh -i ${config.clanCore.secrets.borgbackup.secrets."borgbackup.ssh".path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null";
description = "the rsh to use for the backup";
};
2024-03-08 10:12:34 +00:00
};
}));
default = { };
description = ''
destinations where the machine should be backuped to
'';
};
2024-03-08 10:12:34 +00:00
imports = [ (lib.mkRemovedOptionModule [ "clan" "borgbackup" "enable" ] "Just define clan.borgbackup.destinations to enable it") ];
2024-03-12 12:17:04 +00:00
config = lib.mkIf (cfg.destinations != { }) {
services.borgbackup.jobs = lib.mapAttrs
(_: dest: {
2023-12-04 16:05:37 +00:00
paths = lib.flatten (map (state: state.folders) (lib.attrValues config.clanCore.state));
2024-02-22 12:47:09 +00:00
exclude = [ "*.pyc" ];
repo = dest.repo;
environment.BORG_RSH = dest.rsh;
compression = "auto,zstd";
startAt = "*-*-* 01:00:00";
2024-02-22 12:47:09 +00:00
persistentTimer = true;
preHook = ''
set -x
'';
2024-02-22 13:50:07 +00:00
encryption = {
mode = "repokey";
passCommand = "cat ${config.clanCore.secrets.borgbackup.secrets."borgbackup.repokey".path}";
};
prune.keep = {
within = "1d"; # Keep all archives from the last day
daily = 7;
weekly = 4;
monthly = 0;
};
})
cfg.destinations;
2023-12-06 16:12:38 +00:00
clanCore.secrets.borgbackup = {
facts."borgbackup.ssh.pub" = { };
secrets."borgbackup.ssh" = { };
2024-02-22 13:50:07 +00:00
secrets."borgbackup.repokey" = { };
generator.path = [ pkgs.openssh pkgs.coreutils pkgs.xkcdpass ];
2023-12-06 16:12:38 +00:00
generator.script = ''
ssh-keygen -t ed25519 -N "" -f "$secrets"/borgbackup.ssh
mv "$secrets"/borgbackup.ssh.pub "$facts"/borgbackup.ssh.pub
2024-02-22 13:50:07 +00:00
xkcdpass -n 4 -d - > "$secrets"/borgbackup.repokey
2023-12-06 16:12:38 +00:00
'';
};
clanCore.backups.providers.borgbackup = {
2023-12-08 17:40:18 +00:00
# TODO list needs to run locally or on the remote machine
list = ''
# we need yes here to skip the changed url verification
${lib.concatMapStringsSep "\n" (dest: ''yes y | borg-job-${dest.name} list --json | jq -r '. + {"job-name": "${dest.name}"}' '')
(lib.attrValues cfg.destinations)}
'';
2023-12-08 17:40:18 +00:00
create = ''
${lib.concatMapStringsSep "\n" (dest: ''
systemctl start borgbackup-job-${dest.name}
'') (lib.attrValues cfg.destinations)}
'';
2023-12-04 16:05:37 +00:00
restore = ''
2023-12-08 17:40:18 +00:00
set -efu
cd /
IFS=';' read -ra FOLDER <<< "$FOLDERS"
yes y | borg-job-"$JOB" extract --list "$LOCATION"::"$ARCHIVE_ID" "''${FOLDER[@]}"
2023-12-04 16:05:37 +00:00
'';
};
};
}