restic: Refactor

This commit is contained in:
SebastianStork 2026-02-02 21:31:39 +01:00
parent e20ad1398b
commit a322ebb623
Signed by: SebastianStork
SSH key fingerprint: SHA256:tRrGdjYOwgHxpSc/wTOZQZEjxcb15P0tyXRsbAfd+2Q
3 changed files with 94 additions and 86 deletions

View file

@ -1,34 +1,42 @@
{ config, lib, ... }: { config, lib, ... }:
let let
backups = config.custom.services.restic.backups |> lib.filterAttrs (_: backup: backup.enable); backups =
config.custom.services.restic.backups |> lib.attrValues |> lib.filter (backup: backup.enable);
in in
{ {
options.custom.services.restic.backups = lib.mkOption { options.custom.services.restic.backups = lib.mkOption {
type = lib.types.attrsOf ( type = lib.types.attrsOf (
lib.types.submodule { lib.types.submodule (
options = { { name, ... }:
enable = lib.mkEnableOption "" // { {
default = true; options = {
enable = lib.mkEnableOption "" // {
default = true;
};
name = lib.mkOption {
type = lib.types.nonEmptyStr;
default = name;
};
conflictingService = lib.mkOption {
type = lib.types.nullOr lib.types.nonEmptyStr;
default = null;
};
paths = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
}; };
conflictingService = lib.mkOption { }
type = lib.types.nullOr lib.types.nonEmptyStr; )
default = null;
};
paths = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
};
}
); );
default = { }; default = { };
}; };
config = lib.mkIf (backups != { }) { config = lib.mkIf (backups != [ ]) {
sops = { sops = {
secrets = { secrets = {
"backblaze/key-id" = { }; "backblaze/key-id" = { };
@ -43,17 +51,17 @@ in
}; };
systemd.tmpfiles.rules = systemd.tmpfiles.rules =
backups |> lib.attrNames |> lib.map (name: "d /var/cache/restic-backups-${name} 700 - - -"); backups |> lib.map (backup: "d /var/cache/restic-backups-${backup.name} 700 - - -");
services.restic.backups = services.restic.backups =
backups backups
|> lib.mapAttrs ( |> lib.map (backup: {
name: backup: inherit (backup) name;
lib.mkMerge [ value = lib.mkMerge [
{ {
inherit (backup) paths; inherit (backup) paths;
initialize = true; initialize = true;
repository = "s3:https://s3.eu-central-003.backblazeb2.com/stork-atlas/${name}"; repository = "s3:https://s3.eu-central-003.backblazeb2.com/stork-atlas/${backup.name}";
environmentFile = config.sops.templates."restic/environment".path; environmentFile = config.sops.templates."restic/environment".path;
passwordFile = config.sops.secrets."restic/password".path; passwordFile = config.sops.secrets."restic/password".path;
pruneOpts = [ pruneOpts = [
@ -67,21 +75,22 @@ in
}; };
} }
backup.extraConfig backup.extraConfig
] ];
); })
|> lib.listToAttrs;
systemd.services = systemd.services =
backups backups
|> lib.mapAttrs' ( |> lib.filter (backup: backup.conflictingService != null)
name: backup: |> lib.map (backup: {
lib.nameValuePair "restic-backups-${name}" ( name = "restic-backups-${backup.name}";
lib.mkIf (backup.conflictingService != null) { value = {
unitConfig.Conflicts = [ backup.conflictingService ]; unitConfig.Conflicts = [ backup.conflictingService ];
after = [ backup.conflictingService ]; after = [ backup.conflictingService ];
onSuccess = [ backup.conflictingService ]; onSuccess = [ backup.conflictingService ];
onFailure = [ backup.conflictingService ]; onFailure = [ backup.conflictingService ];
} };
) })
); |> lib.listToAttrs;
}; };
} }

View file

@ -7,7 +7,8 @@
let let
backupsWithHealthchecks = backupsWithHealthchecks =
config.custom.services.restic.backups config.custom.services.restic.backups
|> lib.filterAttrs (_: backup: backup.enable && backup.doHealthchecks); |> lib.attrValues
|> lib.filter (backup: backup.enable && backup.doHealthchecks);
in in
{ {
options.custom.services.restic.backups = lib.mkOption { options.custom.services.restic.backups = lib.mkOption {
@ -20,40 +21,38 @@ in
); );
}; };
config = lib.mkIf (backupsWithHealthchecks != { }) { config = lib.mkIf (backupsWithHealthchecks != [ ]) {
sops.secrets."healthchecks/ping-key" = { }; sops.secrets."healthchecks/ping-key" = { };
systemd.services = lib.mkMerge [ systemd.services = {
{ "healthcheck-ping@" = {
"healthcheck-ping@" = { description = "Pings healthcheck (%i)";
description = "Pings healthcheck (%i)"; serviceConfig.Type = "oneshot";
serviceConfig.Type = "oneshot"; scriptArgs = "%i";
scriptArgs = "%i"; script = ''
script = '' ping_key="$(cat ${config.sops.secrets."healthchecks/ping-key".path})"
ping_key="$(cat ${config.sops.secrets."healthchecks/ping-key".path})" slug="$(echo "$1" | tr _ /)"
slug="$(echo "$1" | tr _ /)"
${lib.getExe pkgs.curl} \ ${lib.getExe pkgs.curl} \
--fail \ --fail \
--silent \ --silent \
--show-error \ --show-error \
--max-time 10 \ --max-time 10 \
--retry 5 "https://hc-ping.com/$ping_key/$slug?create=1" --retry 5 "https://hc-ping.com/$ping_key/$slug?create=1"
''; '';
};
}
// (
backupsWithHealthchecks
|> lib.map (backup: {
name = "restic-backups-${backup.name}";
value = {
wants = [ "healthcheck-ping@${backup.name}-backup_start.service" ];
onSuccess = [ "healthcheck-ping@${backup.name}-backup.service" ];
onFailure = [ "healthcheck-ping@${backup.name}-backup_fail.service" ];
}; };
} })
|> lib.listToAttrs
( );
backupsWithHealthchecks
|> lib.mapAttrs' (
name: _:
lib.nameValuePair "restic-backups-${name}" {
wants = [ "healthcheck-ping@${name}-backup_start.service" ];
onSuccess = [ "healthcheck-ping@${name}-backup.service" ];
onFailure = [ "healthcheck-ping@${name}-backup_fail.service" ];
}
)
)
];
}; };
} }

View file

@ -7,7 +7,8 @@
let let
backupsWithRestoreCommand = backupsWithRestoreCommand =
config.custom.services.restic.backups config.custom.services.restic.backups
|> lib.filterAttrs (_: backup: backup.enable && backup.restoreCommand.enable); |> lib.attrValues
|> lib.filter (backup: backup.enable && backup.restoreCommand.enable);
in in
{ {
options.custom.services.restic.backups = lib.mkOption { options.custom.services.restic.backups = lib.mkOption {
@ -33,23 +34,22 @@ in
config = { config = {
environment.systemPackages = environment.systemPackages =
backupsWithRestoreCommand backupsWithRestoreCommand
|> lib.mapAttrsToList ( |> lib.map (
name: backup: backup:
let
inherit (backup) name conflictingService;
inherit (backup.restoreCommand) preRestore postRestore;
hasConflictingService = conflictingService != null;
in
pkgs.writeShellApplication { pkgs.writeShellApplication {
name = "restic-restore-${name}"; name = "restic-restore-${name}";
text = text = ''
let ${lib.optionalString hasConflictingService "systemctl stop ${conflictingService}"}
inherit (backup) conflictingService; ${preRestore}
inherit (backup.restoreCommand) preRestore postRestore; restic-${name} restore latest --target /
hasConflictingService = conflictingService != null; ${postRestore}
in ${lib.optionalString hasConflictingService "systemctl start ${conflictingService}"}
'' '';
${lib.optionalString hasConflictingService "systemctl stop ${conflictingService}"}
${preRestore}
restic-${name} restore latest --target /
${postRestore}
${lib.optionalString hasConflictingService "systemctl start ${conflictingService}"}
'';
} }
); );
}; };