nixos-configs/devices/servivi/modules/borgbackup.nix
matt1432 63e0ca56cb
All checks were successful
Discord / discord commits (push) Successful in 1m1s
feat(servers): make backups from snapshots so shutting down services isnt needed
2024-01-07 02:05:16 -05:00

71 lines
1.8 KiB
Nix

{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.borgbackup;
secrets = config.sops.secrets;
in {
# Make this file declare default settings
options.services.borgbackup = {
defaults = mkOption {
type = types.attrs;
};
configs = mkOption {
type = types.attrs;
};
};
config = {
programs.ssh.knownHosts = {
pve.publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG/4mrp8E4Ittwg8feRmPtDHSDR2+Pq4uZHeF5MweVcW";
};
services.borgbackup = {
defaults = {
environment = mkDefault {BORG_RSH = "ssh -i ${secrets.borg-ssh.path}";};
repo = mkDefault "ssh://matt@pve/data/backups/borg";
encryption = mkDefault {
mode = "repokey";
passCommand = let
cat = "${pkgs.coreutils}/bin/cat";
key = secrets.borg-repo.path;
in "${cat} ${key}";
};
# Run every 3 hours
startAt = mkDefault "00/3:00";
compression = mkDefault "auto,lzma";
};
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;
preHook = v.preHook or "" + ''
if [[ ! -d ${pathPrefix} ]]; then
mkdir -p ${pathPrefix}
fi
${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r / ${snapPath}
'';
postHook = ''
${pkgs.btrfs-progs}/bin/btrfs subvolume delete ${snapPath}
'' + v.postHook or "";
})
tempJobs;
};
};
}