nix-dotfiles/modules/backup.nix

131 lines
4.3 KiB
Nix
Raw Normal View History

2023-12-26 19:06:02 +01:00
{ config, lib, pkgs, ... }:
let cfg = config.services.backup;
in {
2023-12-26 19:06:02 +01:00
options.services.backup = {
enable = lib.mkEnableOption "backup";
2024-01-30 18:37:13 +01:00
offsite = lib.mkOption {
type = with lib.types; listOf str;
default = [ ];
description = "Offsite backup hostnames.";
};
2023-12-26 19:06:02 +01:00
paths = lib.mkOption {
type = with lib.types; listOf str;
2023-12-27 10:03:13 +01:00
default = [ ];
2023-12-26 19:06:02 +01:00
description = "Extra paths to include in backup.";
};
exclude = lib.mkOption {
type = with lib.types; listOf str;
2023-12-27 10:03:13 +01:00
default = [ ];
2023-12-26 19:06:02 +01:00
description = "Extra paths to exclude in backup.";
};
2024-01-30 18:37:13 +01:00
backup_at = lib.mkOption {
type = lib.types.int;
default = 2;
description = "Time to run backup.";
};
2023-12-26 19:06:02 +01:00
};
config = {
2024-01-30 18:37:13 +01:00
assertions = [
{
assertion = cfg.paths != [ ] -> cfg.enable;
message = "Configuring backup services.backup.paths without enabling services.backup.enable is useless!";
}
{
assertion = cfg.backup_at < 24;
message = "Backup time must be less than 24 hours!";
}
];
2023-12-26 19:06:02 +01:00
services = {
postgresqlBackup = {
inherit (config.services.postgresql) enable;
backupAll = true;
2024-01-30 18:37:13 +01:00
startAt = "*-*-* ${lib.fixedWidthString 2 "0" (toString cfg.backup_at)}:00:00";
2023-12-26 19:06:02 +01:00
};
restic.backups =
let
commonOpts = {
extraBackupArgs = [ "--exclude-file=${pkgs.writeText "restic-exclude-file" (lib.concatMapStrings (x: x + "\n") cfg.exclude)}" ];
2024-01-30 18:37:13 +01:00
2023-12-26 19:06:02 +01:00
initialize = true;
passwordFile = config.sops.secrets."restic/password".path;
paths = [
"/etc/group"
"/etc/machine-id"
"/etc/passwd"
"/etc/shadow"
2024-01-30 18:37:13 +01:00
"/etc/ssh/ssh_host_ecdsa_key"
"/etc/ssh/ssh_host_ecdsa_key.pub"
2023-12-26 19:06:02 +01:00
"/etc/ssh/ssh_host_ed25519_key"
"/etc/ssh/ssh_host_ed25519_key.pub"
"/etc/ssh/ssh_host_rsa_key"
"/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.services.mysql.enable "/var/lib/mysql/"
++ lib.optional (config.security.acme.certs != { }) "/var/lib/acme/" ++ lib.optional config.security.dhparams.enable "/var/lib/dhparams/"
2024-01-30 18:37:13 +01:00
++ lib.optional config.mailserver.enable config.mailserver.mailDirectory;
pruneOpts = [ "--group-by host" "--keep-daily 7" "--keep-weekly 4" "--keep-monthly 12" ];
2024-01-30 18:37:13 +01:00
2023-12-26 19:06:02 +01:00
timerConfig = {
2024-01-30 18:37:13 +01:00
OnCalendar = "*-*-* ${lib.fixedWidthString 2 "0" (toString cfg.backup_at)}:30:00";
2023-12-26 19:06:02 +01:00
RandomizedDelaySec = "5m";
};
};
in
lib.mkIf cfg.enable {
local = commonOpts // { repository = "/var/backup"; };
2024-01-30 18:37:13 +01:00
offsite = lib.mkIf (cfg.offsite != [ ]) commonOpts // { repository = "sftp://offsite/${config.networking.hostName}"; };
2023-12-26 19:06:02 +01:00
};
};
2024-01-30 18:37:13 +01:00
sops.secrets = lib.mkIf (cfg.enable && cfg.offsite != [ ])
{
"restic/offsite/private" = {
owner = "root";
path = "/root/.ssh/id_offsite-backup";
sopsFile = ./backup.yaml;
};
2023-12-26 19:06:02 +01:00
2024-01-30 18:37:13 +01:00
"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;
};
} // lib.mkIf cfg.enable { "restic/password".owner = "root"; };
2023-12-26 19:06:02 +01:00
2024-01-30 18:37:13 +01:00
system.activationScripts.linkResticSSHConfigIntoVirtioFS = lib.mkIf (cfg.enable && cfg.offsite != [ ]) ''
2023-12-26 19:06:02 +01:00
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 = {
2024-01-30 18:37:13 +01:00
restic-backups-local.serviceConfig.Environment = "RESTIC_PROGRESS_FPS=0.016666";
restic-backups-offsite.serviceConfig.Environment = lib.mkIf (cfg.offsite != [ ]) "RESTIC_PROGRESS_FPS=0.016666";
2023-12-26 19:06:02 +01:00
};
2024-01-30 18:37:13 +01:00
timers = lib.mkIf config.services.postgresqlBackup.enable { postgresqlBackup.timerConfig.RandomizedDelaySec = "5m"; };
2023-12-26 19:06:02 +01:00
};
};
2023-12-27 10:03:13 +01:00
}