nixos/backup.nix

85 lines
2.3 KiB
Nix
Raw Normal View History

2020-11-16 21:14:46 +01:00
{ config, lib, pkgs, ... }:
let
cfg = import ./vars-backup.nix;
borgbackupMonitor = { config, pkgs, lib, ... }: with lib; {
key = "borgbackupMonitor";
_file = "borgbackupMonitor";
config.systemd.services = {
"notify-problems@" = {
enable = true;
serviceConfig.User = "felix";
environment.SERVICE = "%i";
script = ''
export $(cat /proc/$(${pkgs.procps}/bin/pgrep -x "sway" -u "$USER")/environ |grep -z '^DBUS_SESSION_BUS_ADDRESS=')
${pkgs.libnotify}/bin/notify-send -u critical "$SERVICE FAILED!" "Run journalctl -u $SERVICE for details"
'';
};
} // flip mapAttrs' config.services.borgbackup.jobs (name: value:
nameValuePair "borgbackup-job-${name}" {
unitConfig.OnFailure = "notify-problems@%i.service";
}
);
config.systemd.timers = flip mapAttrs' config.services.borgbackup.jobs (name: value:
nameValuePair "borgbackup-job-${name}" {
# forces backup after boot in case server was powered off during scheduled event
2020-11-16 21:14:46 +01:00
timerConfig.Persistent = true;
# only if network is available
wantedBy = [ "timers.target" ];
after = [ "network-online.target" ];
2020-11-16 21:14:46 +01:00
}
);
};
in
{
# notification
imports = [
borgbackupMonitor
];
services.borgbackup.jobs.hetzner = {
2020-11-16 21:14:46 +01:00
paths = [
"/"
];
exclude = [
"/nix"
2022-02-09 17:39:12 +01:00
"/sys"
"/run"
"/proc"
2020-11-16 21:14:46 +01:00
"/root/.cache/"
"**/.Trash"
"/tmp/*"
"/var/lock/*"
"/var/run/*"
"/var/tmp/*"
"/home/*/tmp"
"/home/*/todo"
"/home/*/.cache"
"/home/*/.gvfs"
"/home/*/.thumbnails"
"/home/*/.local/share/Trash"
2022-01-05 16:14:27 +01:00
"/srv/data/tmp"
"/srv/data/todo"
2020-11-16 21:14:46 +01:00
];
2022-01-05 16:14:27 +01:00
extraCreateArgs = "--exclude-caches --keep-exclude-tags --stats";
2020-11-16 21:14:46 +01:00
encryption = {
mode = "repokey-blake2";
passCommand = "cat /root/.borg_password";
};
environment.BORG_RSH = "ssh -o 'StrictHostKeyChecking=no' -i /root/.ssh/backup_ed25519 -p 23";
repo = "${cfg.borg.user}@${cfg.borg.host}:${cfg.borg.dir}";
compression = "auto,zstd";
doInit = false;
startAt = "daily";
prune.keep = {
last = 1;
within = "3d";
daily = 7;
weekly = 4;
monthly = 6;
yearly = 2;
};
};
}