2023-12-27 01:59:28 -05:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
2024-01-22 11:09:37 -05:00
|
|
|
}: let
|
|
|
|
inherit (lib) filterAttrs mapAttrs mkDefault mkOption types;
|
|
|
|
|
2023-12-27 11:39:38 -05:00
|
|
|
cfg = config.services.borgbackup;
|
|
|
|
secrets = config.sops.secrets;
|
|
|
|
in {
|
2023-12-27 01:59:28 -05:00
|
|
|
# Make this file declare default settings
|
2023-12-27 11:39:38 -05:00
|
|
|
options.services.borgbackup = {
|
2023-12-27 01:59:28 -05:00
|
|
|
defaults = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
2023-12-27 11:39:38 -05:00
|
|
|
configs = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
};
|
2023-12-27 01:59:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
programs.ssh.knownHosts = {
|
|
|
|
pve.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG/4mrp8E4Ittwg8feRmPtDHSDR2+Pq4uZHeF5MweVcW";
|
|
|
|
};
|
|
|
|
|
|
|
|
services.borgbackup = {
|
|
|
|
defaults = {
|
2024-01-06 22:47:50 -05:00
|
|
|
environment = mkDefault {BORG_RSH = "ssh -i ${secrets.borg-ssh.path}";};
|
2023-12-27 01:59:28 -05:00
|
|
|
|
2024-01-06 22:47:50 -05:00
|
|
|
repo = mkDefault "ssh://matt@pve/data/backups/borg";
|
|
|
|
encryption = mkDefault {
|
2023-12-27 01:59:28 -05:00
|
|
|
mode = "repokey";
|
|
|
|
passCommand = let
|
|
|
|
cat = "${pkgs.coreutils}/bin/cat";
|
2023-12-27 11:39:38 -05:00
|
|
|
key = secrets.borg-repo.path;
|
2023-12-27 01:59:28 -05:00
|
|
|
in "${cat} ${key}";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Run every 3 hours
|
2024-01-06 22:47:50 -05:00
|
|
|
startAt = mkDefault "00/3:00";
|
|
|
|
compression = mkDefault "auto,lzma";
|
2023-12-27 01:59:28 -05:00
|
|
|
};
|
2023-12-27 11:39:38 -05:00
|
|
|
|
2024-01-07 02:05:16 -05:00
|
|
|
jobs = let
|
|
|
|
tempJobs = mapAttrs (_: v: cfg.defaults // v) cfg.configs;
|
|
|
|
in
|
|
|
|
mapAttrs (n: v: let
|
|
|
|
attrs = filterAttrs (n: _: n != "preHook" || n != "postHook" || n != "paths") v;
|
|
|
|
pathPrefix = "/root/snaps";
|
|
|
|
snapPath = "${pathPrefix}/${n}";
|
|
|
|
in
|
|
|
|
attrs
|
|
|
|
// {
|
|
|
|
paths = map (x: snapPath + x) v.paths;
|
|
|
|
|
2024-01-08 11:25:18 -05:00
|
|
|
preHook =
|
|
|
|
v.preHook
|
|
|
|
or ""
|
|
|
|
+
|
|
|
|
/*
|
|
|
|
bash
|
|
|
|
*/
|
|
|
|
''
|
|
|
|
if [[ ! -d ${pathPrefix} ]]; then
|
|
|
|
mkdir -p ${pathPrefix}
|
|
|
|
fi
|
2024-01-07 02:05:16 -05:00
|
|
|
|
2024-01-08 11:25:18 -05:00
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r / ${snapPath}
|
|
|
|
'';
|
2024-01-07 02:05:16 -05:00
|
|
|
|
2024-01-08 11:25:18 -05:00
|
|
|
postHook =
|
|
|
|
/*
|
|
|
|
bash
|
|
|
|
*/
|
|
|
|
''
|
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume delete ${snapPath}
|
|
|
|
''
|
|
|
|
+ v.postHook or "";
|
2024-01-07 02:05:16 -05:00
|
|
|
})
|
|
|
|
tempJobs;
|
2023-12-27 01:59:28 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|