mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-03-22 20:09:07 +01:00
restic: Refactor
This commit is contained in:
parent
e20ad1398b
commit
a322ebb623
3 changed files with 94 additions and 86 deletions
|
|
@ -1,15 +1,22 @@
|
|||
{ config, lib, ... }:
|
||||
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
|
||||
{
|
||||
options.custom.services.restic.backups = lib.mkOption {
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submodule {
|
||||
lib.types.submodule (
|
||||
{ name, ... }:
|
||||
{
|
||||
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;
|
||||
|
|
@ -24,11 +31,12 @@ in
|
|||
};
|
||||
};
|
||||
}
|
||||
)
|
||||
);
|
||||
default = { };
|
||||
};
|
||||
|
||||
config = lib.mkIf (backups != { }) {
|
||||
config = lib.mkIf (backups != [ ]) {
|
||||
sops = {
|
||||
secrets = {
|
||||
"backblaze/key-id" = { };
|
||||
|
|
@ -43,17 +51,17 @@ in
|
|||
};
|
||||
|
||||
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 =
|
||||
backups
|
||||
|> lib.mapAttrs (
|
||||
name: backup:
|
||||
lib.mkMerge [
|
||||
|> lib.map (backup: {
|
||||
inherit (backup) name;
|
||||
value = lib.mkMerge [
|
||||
{
|
||||
inherit (backup) paths;
|
||||
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;
|
||||
passwordFile = config.sops.secrets."restic/password".path;
|
||||
pruneOpts = [
|
||||
|
|
@ -67,21 +75,22 @@ in
|
|||
};
|
||||
}
|
||||
backup.extraConfig
|
||||
]
|
||||
);
|
||||
];
|
||||
})
|
||||
|> lib.listToAttrs;
|
||||
|
||||
systemd.services =
|
||||
backups
|
||||
|> lib.mapAttrs' (
|
||||
name: backup:
|
||||
lib.nameValuePair "restic-backups-${name}" (
|
||||
lib.mkIf (backup.conflictingService != null) {
|
||||
|> lib.filter (backup: backup.conflictingService != null)
|
||||
|> lib.map (backup: {
|
||||
name = "restic-backups-${backup.name}";
|
||||
value = {
|
||||
unitConfig.Conflicts = [ backup.conflictingService ];
|
||||
after = [ backup.conflictingService ];
|
||||
onSuccess = [ backup.conflictingService ];
|
||||
onFailure = [ backup.conflictingService ];
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
})
|
||||
|> lib.listToAttrs;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@
|
|||
let
|
||||
backupsWithHealthchecks =
|
||||
config.custom.services.restic.backups
|
||||
|> lib.filterAttrs (_: backup: backup.enable && backup.doHealthchecks);
|
||||
|> lib.attrValues
|
||||
|> lib.filter (backup: backup.enable && backup.doHealthchecks);
|
||||
in
|
||||
{
|
||||
options.custom.services.restic.backups = lib.mkOption {
|
||||
|
|
@ -20,11 +21,10 @@ in
|
|||
);
|
||||
};
|
||||
|
||||
config = lib.mkIf (backupsWithHealthchecks != { }) {
|
||||
config = lib.mkIf (backupsWithHealthchecks != [ ]) {
|
||||
sops.secrets."healthchecks/ping-key" = { };
|
||||
|
||||
systemd.services = lib.mkMerge [
|
||||
{
|
||||
systemd.services = {
|
||||
"healthcheck-ping@" = {
|
||||
description = "Pings healthcheck (%i)";
|
||||
serviceConfig.Type = "oneshot";
|
||||
|
|
@ -42,18 +42,17 @@ in
|
|||
'';
|
||||
};
|
||||
}
|
||||
|
||||
(
|
||||
// (
|
||||
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" ];
|
||||
}
|
||||
)
|
||||
)
|
||||
];
|
||||
|> 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
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@
|
|||
let
|
||||
backupsWithRestoreCommand =
|
||||
config.custom.services.restic.backups
|
||||
|> lib.filterAttrs (_: backup: backup.enable && backup.restoreCommand.enable);
|
||||
|> lib.attrValues
|
||||
|> lib.filter (backup: backup.enable && backup.restoreCommand.enable);
|
||||
in
|
||||
{
|
||||
options.custom.services.restic.backups = lib.mkOption {
|
||||
|
|
@ -33,17 +34,16 @@ in
|
|||
config = {
|
||||
environment.systemPackages =
|
||||
backupsWithRestoreCommand
|
||||
|> lib.mapAttrsToList (
|
||||
name: backup:
|
||||
pkgs.writeShellApplication {
|
||||
name = "restic-restore-${name}";
|
||||
text =
|
||||
|> lib.map (
|
||||
backup:
|
||||
let
|
||||
inherit (backup) conflictingService;
|
||||
inherit (backup) name conflictingService;
|
||||
inherit (backup.restoreCommand) preRestore postRestore;
|
||||
hasConflictingService = conflictingService != null;
|
||||
in
|
||||
''
|
||||
pkgs.writeShellApplication {
|
||||
name = "restic-restore-${name}";
|
||||
text = ''
|
||||
${lib.optionalString hasConflictingService "systemctl stop ${conflictingService}"}
|
||||
${preRestore}
|
||||
restic-${name} restore latest --target /
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue