nixos-config/modules/restic/system.nix

132 lines
3.5 KiB
Nix
Raw Permalink Normal View History

{ pkgs, config, lib, ... }:
2020-08-22 17:44:39 +02:00
let
cfg = config.sbruder.restic.system;
2020-12-05 14:19:34 +01:00
sftpTarget = "u313368-sub4@u313368-sub4.your-storagebox.de";
sftpPort = 23;
repository = "sftp://${sftpTarget}:${toString sftpPort}/personal";
2020-08-22 17:44:39 +02:00
excludes = [
2020-12-21 13:08:22 +01:00
# Caches
2020-08-22 17:44:39 +02:00
"/home/*/Downloads/"
"/home/*/.cache/"
"/home/*/**/cache/"
2020-12-21 13:08:22 +01:00
"/home/*/.local/share/Trash" # some gui applications use it
2022-04-23 21:10:55 +02:00
"/root/.cache"
2020-12-21 13:08:22 +01:00
"/data/cache/"
2022-06-27 14:19:06 +02:00
"/var/cache/"
2020-08-22 17:44:39 +02:00
# Rust
"/home/*/.rustup/toolchains/"
"/home/*/.cargo"
2020-12-21 13:08:22 +01:00
# Misc
2020-08-22 17:44:39 +02:00
"/home/*/mount"
2022-05-15 11:14:34 +02:00
"/home/*/mounts"
2020-08-22 17:44:39 +02:00
2020-12-21 13:08:22 +01:00
# Docker (state should be kept somewhere else)
2020-08-22 17:44:39 +02:00
"/var/lib/docker/"
2021-04-06 10:47:05 +02:00
# Static configuration (generated from this repository)
"/etc/static/"
] ++ cfg.extraExcludes;
2021-02-28 11:55:58 +01:00
excludesFile = pkgs.writeText "excludes.txt" (lib.concatStringsSep "\n" excludes);
# script to use restic as user without dealing with authentication
authScript = pkgs.writeShellScriptBin "restic-auth" ''
${pkgs.restic}/bin/restic \
--password-command="pass data/backup/restic-nixos" \
--repo "${repository}" \
$@
'';
2020-08-22 17:44:39 +02:00
in
{
options.sbruder.restic.system = {
2020-12-05 14:19:34 +01:00
enable = lib.mkEnableOption "restic";
timerConfig = lib.mkOption {
type = with lib.types; attrsOf str;
default = {
OnCalendar = "18:00";
RandomizedDelaySec = "2h";
2020-12-21 12:33:46 +01:00
};
};
extraPaths = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "/data" ];
};
extraExcludes = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
};
uploadLimit = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
};
prune = lib.mkEnableOption "pruning";
2020-08-22 17:44:39 +02:00
};
2020-10-17 09:58:44 +02:00
2020-12-05 14:19:34 +01:00
config = lib.mkIf cfg.enable {
sops.secrets = {
2021-01-06 13:09:29 +01:00
restic-password = { };
restic-repository = { };
} // lib.optionalAttrs cfg.prune {
restic-ssh-key = {
sopsFile = ../../machines/${config.networking.hostName}/secrets.yaml;
};
2021-01-06 13:09:29 +01:00
};
services.restic.backups.system = {
inherit (cfg) timerConfig;
repositoryFile = config.sops.secrets.restic-repository.path;
passwordFile = config.sops.secrets.restic-password.path;
paths = [
2021-04-06 10:47:05 +02:00
"/etc"
"/home"
2021-04-06 10:47:05 +02:00
"/root"
"/srv"
"/var"
] ++ cfg.extraPaths;
2020-12-05 14:19:34 +01:00
extraBackupArgs = [
"--compression auto"
2020-12-05 14:19:34 +01:00
"--exclude-caches"
"--exclude-file=${excludesFile}"
"--tag system"
2020-12-05 14:19:34 +01:00
"--verbose"
] ++ lib.optional (cfg.uploadLimit != null) "--limit-upload=${toString cfg.uploadLimit}";
2020-12-05 14:19:34 +01:00
};
systemd.services."restic-backups-system".serviceConfig = {
2020-12-05 14:19:34 +01:00
"Nice" = 10;
"IOSchedulingClass" = "best-effort";
"IOSchedulingPriority" = 7;
};
services.restic.backups.system-prune = lib.mkIf cfg.prune {
inherit repository;
passwordFile = config.sops.secrets.restic-password.path;
timerConfig = {
OnCalendar = "*-1/2-07 03:00:00";
RandomizedDelaySec = "4h";
};
paths = [ ];
extraOptions = [
"-o"
"sftp.command='ssh -i ${config.sops.secrets.restic-ssh-key.path} -p ${toString sftpPort} ${sftpTarget} -s sftp'"
];
pruneOpts = [
"--compression auto"
"--keep-daily 7"
"--keep-monthly 12"
"--keep-weekly 5"
"--keep-yearly 10"
"--tag system"
"--verbose"
] ++ lib.optional (cfg.uploadLimit != null) "--limit-upload=${toString cfg.uploadLimit}";
};
environment.systemPackages = [
authScript
];
2020-10-17 09:58:44 +02:00
};
2020-08-22 17:44:39 +02:00
}