Move backup to single btrbk instance

This commit is contained in:
asonix 2023-05-06 15:39:28 -05:00
parent 88bc60396e
commit dfe742ba11
2 changed files with 43 additions and 24 deletions

View file

@ -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

View file

@ -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);
};
};
}