nix-config/modules/backup.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.8 KiB
Nix
Raw Normal View History

2023-05-18 01:55:16 +02:00
{ config, lib, pkgs, ... }:
2022-10-31 20:30:25 +01:00
2023-05-18 01:55:16 +02:00
let
cfg = config.services.backup;
in
2022-10-31 20:30:25 +01:00
{
2023-05-18 01:55:16 +02:00
options.services.backup = {
enable = lib.mkEnableOption "backup" // {
default = config.services.postgresql.enable;
};
paths = lib.mkOption {
type = with lib.types; listOf str;
default = [];
description = "Extra paths to include in backup.";
};
exclude = lib.mkOption {
type = with lib.types; listOf str;
default = [];
description = "Extra paths to exclude in backup.";
};
};
config = {
assertions = [ {
2023-11-11 04:34:19 +01:00
assertion = cfg.paths != [ ] -> cfg.enable;
message = "Configuring backup services.backup.paths without enabling services.backup.enable is useless!";
} ];
services = {
postgresqlBackup = {
inherit (config.services.postgresql) enable;
backupAll = true;
startAt = "*-*-* 04:00:00";
};
restic.backups =
let
commonOpts = {
2023-05-18 01:55:16 +02:00
extraBackupArgs = [
"--exclude-file=${pkgs.writeText "restic-exclude-file" (lib.concatMapStrings (x: x + "\n") cfg.exclude)}"
];
initialize = true;
passwordFile = config.sops.secrets."restic/password".path;
2023-05-18 17:15:45 +02:00
paths = [
"/etc/group"
"/etc/machine-id"
"/etc/passwd"
"/etc/shadow"
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
2023-11-11 04:25:36 +01:00
"/etc/ssh/ssh_host_rsa_key"
2023-05-18 17:15:45 +02:00
"/etc/ssh/ssh_host_rsa_key.pub"
"/etc/subgid"
"/etc/subuid"
"/var/lib/nixos/"
] ++ cfg.paths
++ lib.optional config.services.postgresql.enable "/var/backup/postgresql/"
++ lib.optional (config.security.acme.certs != {}) "/var/lib/acme/"
++ lib.optional config.security.dhparams.enable "/var/lib/dhparams/";
pruneOpts = [
"--group-by host"
"--keep-daily 7"
"--keep-weekly 4"
"--keep-monthly 12"
];
timerConfig = {
OnCalendar = "*-*-* 04:30:00";
RandomizedDelaySec = "5m";
};
};
in
2023-05-18 01:55:16 +02:00
lib.mkIf cfg.enable {
2023-11-11 23:44:50 +01:00
server9 = commonOpts // {
repositoryFile = config.sops.secrets."restic/repositories/server9".path;
2023-05-18 01:55:16 +02:00
};
offsite = commonOpts // {
repository = "sftp://offsite/${config.networking.hostName}";
2023-05-18 01:55:16 +02:00
};
};
};
2023-05-18 01:55:16 +02:00
sops.secrets = lib.mkIf cfg.enable {
"restic/offsite/private" = {
owner = "root";
path = "/root/.ssh/id_offsite-backup";
sopsFile = ./backup.yaml;
};
"restic/offsite/public" = {
owner = "root";
path = "/root/.ssh/id_offsite-backup.pub";
sopsFile = ./backup.yaml;
};
"restic/offsite/ssh-config" = {
owner = "root";
path = "/root/.ssh/config";
sopsFile = ./backup.yaml;
};
2023-11-11 04:34:05 +01:00
# relies on defaultSopsFile
"restic/password".owner = "root";
2023-11-11 23:44:50 +01:00
"restic/repositories/server9".owner = "root";
};
2023-05-18 01:55:16 +02:00
system.activationScripts.linkResticSSHConfigIntoVirtioFS = lib.mkIf cfg.enable ''
echo "Linking restic ssh config..."
mkdir -m700 -p /home/root/.ssh/
ln -fs {,/home}/root/.ssh/id_offsite-backup
ln -fs {,/home}/root/.ssh/id_offsite-backup.pub
ln -fs {,/home}/root/.ssh/config
'';
systemd = lib.mkIf cfg.enable {
services = {
2023-11-11 23:44:50 +01:00
restic-backups-server9.serviceConfig.Environment = "RESTIC_PROGRESS_FPS=0.016666";
restic-backups-offsite.serviceConfig.Environment = "RESTIC_PROGRESS_FPS=0.016666";
};
timers = lib.mkIf config.services.postgresqlBackup.enable {
postgresqlBackup.timerConfig.RandomizedDelaySec = "5m";
};
};
2022-10-31 20:30:25 +01:00
};
}