[backup] fix errors
All checks were successful
Check flake syntax / flake-check (push) Successful in 5m44s

This commit is contained in:
2025-03-16 10:09:03 +01:00
parent 286e03c853
commit d5e94b50cb

View File

@@ -2,8 +2,18 @@
with lib; with lib;
let let
cfg = config.malobeo.backup; cfg = config.malobeo.backup;
newfunc = (hostname: datasetNames: (map (dataset: { name = "${hostname}_${dataset.sourceDataset}"; value = { inherit hostname; inherit dataset; }; } ) datasetNames)); hostToCommand = (hostname: datasetNames:
(map (dataset: {
name = "${hostname}_${dataset.sourceDataset}";
value = {
inherit hostname;
inherit (dataset) sourceDataset targetDataset;
};
} ) datasetNames));
peers = import ./peers.nix; peers = import ./peers.nix;
enableSnapshots = cfg.snapshots != null;
enableBackups = cfg.hosts != null;
in in
{ {
options.malobeo.backup = { options.malobeo.backup = {
@@ -14,15 +24,14 @@ in
}; };
snapshots = mkOption { snapshots = mkOption {
type = types.listOf types.str; type = types.nullOr (types.listOf types.str);
default = []; default = null;
description = "Automatic snapshots will be created for the given datasets"; description = "Automatic snapshots will be created for the given datasets";
}; };
#TODO: instead listof str we need dataset here to declare the dataset name on the source host
# and also the dataset name on target host (which stores the backups)
hosts = mkOption { hosts = mkOption {
type = types.attrsOf (types.listOf (types.submodule { default = null;
type = types.nullOr (types.attrsOf (types.listOf (types.submodule {
options = { options = {
sourceDataset = mkOption { sourceDataset = mkOption {
type = types.str; type = types.str;
@@ -31,13 +40,19 @@ in
type = types.str; type = types.str;
}; };
}; };
})); })));
description = "Hostname with list of datasets to backup."; description = "Hostname with list of datasets to backup.";
}; };
sshKey = mkOption {
default = null;
type = types.nullOr types.str;
description = "Set path to ssh key used for pull backups. Otherwise default key is used";
};
}; };
config = mkIf (cfg.enable) { config = mkIf (cfg.enable) {
services.sanoid = { services.sanoid = mkIf (enableSnapshots) {
enable = true; enable = true;
templates."default" = { templates."default" = {
@@ -56,10 +71,9 @@ in
}; }) cfg.snapshots); }; }) cfg.snapshots);
}; };
services.syncoid = with config; { services.syncoid = mkIf (enableBackups) {
enable = true; enable = true;
sshKey = cfg.sshKey;
sshKey = sops.secrets.backup_key.path;
commonArgs = [ commonArgs = [
"--no-sync-snap" "--no-sync-snap"
@@ -68,17 +82,12 @@ in
interval = "*-*-* 04:15:00"; interval = "*-*-* 04:15:00";
commands = builtins.mapAttrs (name: value: { commands = builtins.mapAttrs (name: value: {
source = "backup@${peers.${value.hostname}.address}:${value.dataset.sourceDataset}"; source = "backup@${peers.${value.hostname}.address}:${value.sourceDataset}";
target = "${value.dataset.targetDataset}"; target = "${value.targetDataset}";
sendOptions = "w"; sendOptions = "w";
recvOptions = "\"\""; recvOptions = "\"\"";
recursive = true; recursive = true;
})(builtins.listToAttrs (builtins.concatLists (builtins.attrValues (builtins.mapAttrs newfunc cfg.hosts)))); })(builtins.listToAttrs (builtins.concatLists (builtins.attrValues (builtins.mapAttrs hostToCommand cfg.hosts))));
};
sops.secrets.backup_key = {
owner = config.services.syncoid.user;
key = "backup_key";
}; };
}; };
} }