mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-01-21 18:41:34 +01:00
Automate restic restore command creation
This commit is contained in:
parent
731f0ec615
commit
e6989963c7
6 changed files with 70 additions and 95 deletions
58
modules/system/services/restic-backups/restore.nix
Normal file
58
modules/system/services/restic-backups/restore.nix
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
...
|
||||
}:
|
||||
let
|
||||
backupsWithRestoreCommand =
|
||||
config.custom.services.resticBackups
|
||||
|> lib.filterAttrs (_: value: value.enable)
|
||||
|> lib.filterAttrs (_: value: value.restoreCommand.enable);
|
||||
in
|
||||
{
|
||||
options.custom.services.resticBackups = lib.mkOption {
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submodule {
|
||||
options.restoreCommand = {
|
||||
enable = lib.mkEnableOption "" // {
|
||||
default = true;
|
||||
};
|
||||
preRestore = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
};
|
||||
postRestore = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
};
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
config = {
|
||||
environment.systemPackages =
|
||||
backupsWithRestoreCommand
|
||||
|> lib.mapAttrsToList (
|
||||
name: value:
|
||||
pkgs.writeShellApplication {
|
||||
name = "restic-restore-${name}";
|
||||
text =
|
||||
let
|
||||
inherit (value) dependentService;
|
||||
hasDependentService = dependentService != null;
|
||||
in
|
||||
''
|
||||
${lib.optionalString hasDependentService "sudo systemctl stop ${dependentService}"}
|
||||
sudo --user=${value.user} bash -c "
|
||||
${value.restoreCommand.preRestore}
|
||||
restic-nextcloud restore latest --target /
|
||||
${value.restoreCommand.postRestore}
|
||||
"
|
||||
${lib.optionalString hasDependentService "sudo systemctl start ${dependentService}"}
|
||||
'';
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue