backup module #92

Merged
kalipso merged 9 commits from sanoid into master 2025-03-16 13:13:56 +01:00
2 changed files with 85 additions and 0 deletions
Showing only changes of commit 286e03c853 - Show all commits

View File

@@ -0,0 +1,84 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.malobeo.backup;
newfunc = (hostname: datasetNames: (map (dataset: { name = "${hostname}_${dataset.sourceDataset}"; value = { inherit hostname; inherit dataset; }; } ) datasetNames));
peers = import ./peers.nix;
in
{
options.malobeo.backup = {
enable = mkOption {
type = types.bool;
default = false;
description = "Enable sharing metrics";
};
snapshots = mkOption {
type = types.listOf types.str;
default = [];
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)
kalipso marked this conversation as resolved Outdated

"Enable sharing metrics"?

"Enable sharing metrics"?
hosts = mkOption {
type = types.attrsOf (types.listOf (types.submodule {
options = {
sourceDataset = mkOption {
type = types.str;
};
targetDataset = mkOption {
type = types.str;
};
};
}));
description = "Hostname with list of datasets to backup.";
};
};
config = mkIf (cfg.enable) {
services.sanoid = {
enable = true;
templates."default" = {
hourly = 0;
daily = 30; #keep 30 daily snapshots
monthly = 6; #keep 6 monthly backups
yearly = 0;
autosnap = true; #take snapshots automatically
autoprune = true; #delete old snapshots
};
datasets = builtins.listToAttrs (map (name: { inherit name; value = {
useTemplate = [ "default" ];
recursive = true;
}; }) cfg.snapshots);
};
services.syncoid = with config; {
enable = true;
sshKey = sops.secrets.backup_key.path;
commonArgs = [
"--no-sync-snap"
];
interval = "*-*-* 04:15:00";
commands = builtins.mapAttrs (name: value: {
source = "backup@${peers.${value.hostname}.address}:${value.dataset.sourceDataset}";
target = "${value.dataset.targetDataset}";
sendOptions = "w";
recvOptions = "\"\"";
recursive = true;
})(builtins.listToAttrs (builtins.concatLists (builtins.attrValues (builtins.mapAttrs newfunc cfg.hosts))));
};
sops.secrets.backup_key = {
owner = config.services.syncoid.user;
key = "backup_key";
};
};
}

View File

@@ -116,6 +116,7 @@ in (utils.lib.eachSystem (builtins.filter filter_system utils.lib.defaultSystems
metrics.imports = [ ./machines/modules/malobeo/metrics.nix ];
disko.imports = [ ./machines/modules/disko ];
users.imports = [ ./machines/modules/malobeo/users.nix ];
backup.imports = [ ./machines/modules/malobeo/backup.nix ];
};
hydraJobs = nixpkgs.lib.mapAttrs (_: nixpkgs.lib.hydraJob) (