diff --git a/flake.nix b/flake.nix index 7cabd0d..e0a3708 100644 --- a/flake.nix +++ b/flake.nix @@ -465,7 +465,8 @@ extraModules = sd-images.packages.${system}.RockPro64v2.modules ++ [ (networkModule { inherit selfIp; }) (btrbkModule { - instances = builtins.map (item: item // { localMountDir = mountDir; }) backupHosts; + instances = backupHosts; + localMountDir = mountDir; }) ({ config, lib, pkgs, ... }: let diff --git a/modules/btrbk/default.nix b/modules/btrbk/default.nix index 29d5ccc..448e2a4 100644 --- a/modules/btrbk/default.nix +++ b/modules/btrbk/default.nix @@ -1,4 +1,5 @@ { instances ? [ ] +, localMountDir ? null }: { config, pkgs, ... }: @@ -55,7 +56,7 @@ let }; }; - backup = primaryIp: remoteMountDir: localMountDir: subvolumes: { + backup = instances: { backend_remote = "btrfs-progs-sudo"; ssh_identity = config.sops.secrets.private_key.path; ssh_user = "btrbk"; @@ -63,12 +64,12 @@ let target_preserve = "2h 2d 10w *m"; target_preserve_min = "24h"; transaction_log = "/var/log/btrbk.log"; - volume = { - "ssh://${primaryIp}${remoteMountDir}" = btrbkSecondary { - targetDir = "${localMountDir}/@snapshots"; + volume = builtins.foldl' (acc: { primaryIp, mountDir, name, subvolumes }: acc // { + "ssh://${primaryIp}${mountDir}" = btrbkSecondary { + targetDir = "${localMountDir}/@snapshots/${name}"; inherit subvolumes; }; - }; + }) { } instances; }; in { @@ -84,7 +85,18 @@ in btrfs-progs (writeShellScriptBin "restore-snapshot" (builtins.readFile ./restore-snapshot)) (writeShellScriptBin "restore-all-snapshots" (builtins.readFile ./restore-all-snapshots)) - ]; + ] ++ (if localMountDir != null then [ + (writeShellScriptBin + "make-backup-subdirectories" + (builtins.foldl' + (acc: { name, ... }: + "${acc}\nmkdir -p ${localMountDir}/@snapshots/${name}" + ) + "" + instances + ) + ) + ] else []); services.btrbk = { sshAccess = [ @@ -94,25 +106,31 @@ in } ]; extraPackages = with pkgs; [ gzip ]; - instances = (builtins.foldl' - (acc: { primaryIp ? null, mountDir, localMountDir ? null, subvolumes, name ? "btrbk" }: - let - selected = - if primaryIp == null && localMountDir == null then - (primary mountDir subvolumes) - else if localMountDir == null then - (secondary primaryIp mountDir subvolumes) - else - (backup primaryIp mountDir localMountDir subvolumes); - in - acc // + instances = if localMountDir == null then + (builtins.foldl' + (acc: { primaryIp ? null, mountDir, subvolumes, name ? "btrbk" }: + let + selected = + if primaryIp == null then + (primary mountDir subvolumes) + else + (secondary primaryIp mountDir subvolumes); + in + acc // + { + ${name} = { + onCalendar = "hourly"; + settings = selected; + }; + }) + { } + instances) + else { - ${name} = { + btrbk = { onCalendar = "hourly"; - settings = selected; + settings = (backup instances); }; - }) - { } - instances); + }; }; }