1
0
Fork 0
selfhostblocks/modules/blocks/backup.nix

290 lines
9.9 KiB
Nix
Raw Normal View History

2023-06-23 06:22:34 +02:00
{ config, pkgs, lib, ... }:
let
cfg = config.shb.backup;
instanceOptions = {
2023-10-22 06:41:17 +02:00
enable = lib.mkEnableOption "shb backup instance";
2023-06-23 06:22:34 +02:00
backend = lib.mkOption {
description = "What program to use to make the backups.";
type = lib.types.enum [ "borgmatic" "restic" ];
example = "borgmatic";
};
keySopsFile = lib.mkOption {
description = "Sops file that holds this instance's Borgmatic repository key and passphrase.";
type = lib.types.path;
example = "secrets/backup.yaml";
};
sourceDirectories = lib.mkOption {
description = "Borgmatic source directories.";
type = lib.types.nonEmptyListOf lib.types.str;
};
excludePatterns = lib.mkOption {
description = "Borgmatic exclude patterns.";
type = lib.types.listOf lib.types.str;
default = [];
};
2023-09-26 05:27:35 +02:00
secretName = lib.mkOption {
description = "Secret name, if null use the name of the backup instance.";
type = lib.types.nullOr lib.types.str;
default = null;
};
2023-06-23 06:22:34 +02:00
repositories = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Repositories to back this instance to.";
2023-06-23 06:22:34 +02:00
type = lib.types.nonEmptyListOf lib.types.str;
};
retention = lib.mkOption {
description = "Retention options.";
type = lib.types.attrsOf (lib.types.oneOf [ lib.types.int lib.types.nonEmptyStr ]);
default = {
keep_within = "1d";
keep_hourly = 24;
keep_daily = 7;
keep_weekly = 4;
keep_monthly = 6;
};
};
consistency = lib.mkOption {
description = "Consistency frequency options. Only applicable for borgmatic";
type = lib.types.attrsOf lib.types.nonEmptyStr;
default = {};
example = {
repository = "2 weeks";
archives = "1 month";
};
};
hooks = lib.mkOption {
description = "Borgmatic hooks.";
default = {};
type = lib.types.submodule {
options = {
before_backup = lib.mkOption {
description = "Hooks to run before backup";
type = lib.types.listOf lib.types.str;
default = [];
};
after_backup = lib.mkOption {
description = "Hooks to run after backup";
type = lib.types.listOf lib.types.str;
default = [];
};
};
};
};
environmentFile = lib.mkOption {
type = lib.types.bool;
description = "Add environment file to be read by the systemd service.";
default = false;
example = true;
};
};
2023-09-26 05:27:35 +02:00
repoSlugName = name: builtins.replaceStrings ["/" ":"] ["_" "_"] (lib.strings.removePrefix "/" name);
2023-06-23 06:22:34 +02:00
in
{
options.shb.backup = {
onlyOnAC = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Run backups only if AC power is plugged in.";
2023-06-23 06:22:34 +02:00
default = true;
example = false;
type = lib.types.bool;
};
user = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Unix user doing the backups.";
2023-06-23 06:22:34 +02:00
type = lib.types.str;
default = "backup";
};
group = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Unix group doing the backups.";
2023-06-23 06:22:34 +02:00
type = lib.types.str;
default = "backup";
};
instances = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Each instance is a backup setting";
2023-06-23 06:22:34 +02:00
default = {};
type = lib.types.attrsOf (lib.types.submodule {
options = instanceOptions;
});
};
borgServer = lib.mkOption {
2023-11-30 21:06:41 +01:00
description = "Add borgbackup package so external backups can use this server as a remote.";
2023-06-23 06:22:34 +02:00
default = false;
example = true;
type = lib.types.bool;
};
};
config = lib.mkIf (cfg.instances != {}) (
let
2023-10-22 06:41:17 +02:00
enabledInstances = lib.attrsets.filterAttrs (k: i: i.enable) cfg.instances;
borgmaticInstances = lib.attrsets.filterAttrs (k: i: i.backend == "borgmatic") enabledInstances;
resticInstances = lib.attrsets.filterAttrs (k: i: i.backend == "restic") enabledInstances;
2023-06-23 06:22:34 +02:00
in
{
users.users = {
${cfg.user} = {
name = cfg.user;
group = cfg.group;
home = "/var/lib/backup";
createHome = true;
isSystemUser = true;
2023-09-26 05:27:35 +02:00
extraGroups = [ "keys" ];
2023-06-23 06:22:34 +02:00
};
};
users.groups = {
${cfg.group} = {
name = cfg.group;
};
};
sops.secrets =
let
mkSopsSecret = name: instance: (
[
{
2023-09-26 05:27:35 +02:00
"${instance.backend}/passphrases/${if isNull instance.secretName then name else instance.secretName}" = {
2023-06-23 06:22:34 +02:00
sopsFile = instance.keySopsFile;
mode = "0440";
owner = cfg.user;
group = cfg.group;
};
}
] ++ lib.optional ((lib.filter (lib.strings.hasPrefix "s3") instance.repositories) != []) {
2023-09-26 05:27:35 +02:00
"${instance.backend}/environmentfiles/${if isNull instance.secretName then name else instance.secretName}" = {
2023-06-23 06:22:34 +02:00
sopsFile = instance.keySopsFile;
mode = "0440";
owner = cfg.user;
group = cfg.group;
};
2023-09-26 05:27:35 +02:00
} ++ lib.optionals (instance.backend == "borgmatic") (lib.flatten (map (repository: {
"${instance.backend}/keys/${repoSlugName repository}" = {
key = "${instance.backend}/keys/${if isNull instance.secretName then name else instance.secretName}";
sopsFile = instance.keySopsFile;
mode = "0440";
owner = cfg.user;
group = cfg.group;
};
}) instance.repositories))
2023-06-23 06:22:34 +02:00
);
in
2023-10-22 06:41:17 +02:00
lib.mkMerge (lib.flatten (lib.attrsets.mapAttrsToList mkSopsSecret enabledInstances));
2023-06-23 06:22:34 +02:00
systemd.timers.borgmatic = lib.mkIf (borgmaticInstances != {}) {
timerConfig = {
OnCalendar = "hourly";
};
};
systemd.services.borgmatic = lib.mkIf (borgmaticInstances != {}) {
serviceConfig = {
User = cfg.user;
Group = cfg.group;
2023-09-26 05:27:35 +02:00
ExecStartPre = [ "" ]; # Do not sleep before starting.
2023-06-23 06:22:34 +02:00
ExecStart = [ "" "${pkgs.borgmatic}/bin/borgmatic --verbosity -1 --syslog-verbosity 1" ];
# For borgmatic, since we have only one service, we need to merge all environmentFile
# from all instances.
2023-10-22 06:41:17 +02:00
EnvironmentFile = lib.mapAttrsToList (name: value: value.environmentFile) enabledInstances;
2023-06-23 06:22:34 +02:00
};
};
systemd.packages = lib.mkIf (borgmaticInstances != {}) [ pkgs.borgmatic ];
environment.systemPackages = (
lib.optionals cfg.borgServer [ pkgs.borgbackup ]
++ lib.optionals (borgmaticInstances != {}) [ pkgs.borgbackup pkgs.borgmatic ]
++ lib.optionals (resticInstances != {}) [ pkgs.restic ]
);
services.restic.backups =
let
mkRepositorySettings = name: instance: repository: {
"${name}_${repoSlugName repository}" = {
inherit (cfg) user;
inherit repository;
paths = instance.sourceDirectories;
2023-09-26 05:27:35 +02:00
passwordFile = "/run/secrets/${instance.backend}/passphrases/${name}";
2023-06-23 06:22:34 +02:00
initialize = true;
timerConfig = {
2023-07-23 04:12:15 +02:00
OnCalendar = "00,12:00:00";
2023-06-23 06:22:34 +02:00
RandomizedDelaySec = "5m";
};
pruneOpts = lib.mapAttrsToList (name: value:
"--${builtins.replaceStrings ["_"] ["-"] name} ${builtins.toString value}"
) instance.retention;
backupPrepareCommand = lib.strings.concatStringsSep "\n" instance.hooks.before_backup;
backupCleanupCommand = lib.strings.concatStringsSep "\n" instance.hooks.after_backup;
} // lib.attrsets.optionalAttrs (instance.environmentFile) {
2023-09-26 05:27:35 +02:00
environmentFile = "/run/secrets/${instance.backend}/environmentfiles/${name}";
2023-07-22 18:54:19 +02:00
} // lib.attrsets.optionalAttrs (builtins.length instance.excludePatterns > 0) {
exclude = instance.excludePatterns;
2023-06-23 06:22:34 +02:00
};
};
mkSettings = name: instance: builtins.map (mkRepositorySettings name instance) instance.repositories;
in
lib.mkMerge (lib.flatten (lib.attrsets.mapAttrsToList mkSettings resticInstances));
environment.etc =
let
mkSettings = name: instance: {
"borgmatic.d/${name}.yaml".text = lib.generators.toYAML {} {
location =
{
source_directories = instance.sourceDirectories;
repositories = instance.repositories;
}
// (lib.attrsets.optionalAttrs (builtins.length instance.excludePatterns > 0) {
excludePatterns = instance.excludePatterns;
});
storage = {
2023-09-26 05:27:35 +02:00
encryption_passcommand = "cat /run/secrets/borgmatic/passphrases/${if isNull instance.secretName then name else instance.secretName}";
borg_keys_directory = "/run/secrets/borgmatic/keys";
2023-06-23 06:22:34 +02:00
};
retention = instance.retention;
consistency.checks =
let
mkCheck = name: frequency: {
inherit name frequency;
};
in
lib.attrsets.mapAttrsToList mkCheck instance.consistency;
# hooks = lib.mkMerge [
# lib.optionalAttrs (builtins.length instance.hooks.before_backup > 0) {
# inherit (instance.hooks) before_backup;
# }
# lib.optionalAttrs (builtins.length instance.hooks.after_backup > 0) {
# inherit (instance.hooks) after_backup;
# }
# ];
};
};
in
lib.mkMerge (lib.attrsets.mapAttrsToList mkSettings borgmaticInstances);
});
}