2021-08-09 19:34:23 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2022-01-11 16:08:21 +01:00
|
|
|
inherit (lib)
|
|
|
|
attrsets
|
|
|
|
concatStringsSep
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
optional
|
|
|
|
;
|
|
|
|
|
2021-08-09 19:34:23 +02:00
|
|
|
cfg = config.my.services.restic-backup;
|
2022-01-11 16:08:21 +01:00
|
|
|
excludeArg = "--exclude-file=" + (pkgs.writeText "excludes.txt" (concatStringsSep "\n" cfg.exclude));
|
2021-08-09 19:34:23 +02:00
|
|
|
makePruneOpts = pruneOpts:
|
|
|
|
attrsets.mapAttrsToList (name: value: "--keep-${name} ${toString value}") pruneOpts;
|
|
|
|
in {
|
2022-01-11 16:08:21 +01:00
|
|
|
options.my.services.restic-backup = let inherit (lib) types; in {
|
2021-08-09 19:34:23 +02:00
|
|
|
enable = mkEnableOption "Enable Restic backups for this host";
|
|
|
|
|
|
|
|
repo = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/mnt/hdd";
|
|
|
|
description = "Restic backup repo";
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
paths = mkOption {
|
2022-01-11 16:08:21 +01:00
|
|
|
type = types.listOf types.str;
|
2021-08-09 19:34:23 +02:00
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"/var/lib"
|
|
|
|
"/home"
|
|
|
|
];
|
|
|
|
description = "Paths to backup";
|
|
|
|
};
|
|
|
|
|
|
|
|
exclude = mkOption {
|
2022-01-11 16:08:21 +01:00
|
|
|
type = types.listOf types.str;
|
2021-08-09 19:34:23 +02:00
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
# very large paths
|
|
|
|
"/var/lib/docker"
|
|
|
|
"/var/lib/systemd"
|
|
|
|
"/var/lib/libvirt"
|
|
|
|
|
|
|
|
# temporary files created by `cargo` and `go build`
|
|
|
|
"**/target"
|
|
|
|
"/home/*/go/bin"
|
|
|
|
"/home/*/go/pkg"
|
|
|
|
];
|
|
|
|
description = "Paths to exclude from backup";
|
|
|
|
};
|
|
|
|
|
|
|
|
prune = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {
|
|
|
|
daily = 7;
|
|
|
|
weekly = 4;
|
|
|
|
monthly = 6;
|
|
|
|
};
|
|
|
|
};
|
2022-01-17 21:57:00 +01:00
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/root/restic/password";
|
|
|
|
};
|
|
|
|
|
|
|
|
environmentFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/root/restic/creds";
|
|
|
|
};
|
2022-01-18 09:05:22 +01:00
|
|
|
|
|
|
|
timerConfig = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = {
|
|
|
|
OnCalendar = "daily";
|
|
|
|
};
|
|
|
|
};
|
2021-08-09 19:34:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [ pkgs.restic ];
|
|
|
|
|
|
|
|
services.restic.backups.backblaze = {
|
|
|
|
initialize = true;
|
|
|
|
|
|
|
|
paths = cfg.paths;
|
|
|
|
|
|
|
|
repository = cfg.repo;
|
2022-01-17 21:57:00 +01:00
|
|
|
passwordFile = cfg.passwordFile;
|
|
|
|
environmentFile = cfg.environmentFile;
|
2021-08-09 19:34:23 +02:00
|
|
|
|
2021-08-09 20:14:09 +02:00
|
|
|
extraBackupArgs = [ "--verbose=2" ]
|
2022-01-11 16:08:21 +01:00
|
|
|
++ optional (builtins.length cfg.exclude != 0) excludeArg;
|
2021-08-09 19:34:23 +02:00
|
|
|
|
2022-01-18 09:05:22 +01:00
|
|
|
timerConfig = cfg.timerConfig;
|
2021-08-09 19:34:23 +02:00
|
|
|
|
|
|
|
pruneOpts = makePruneOpts cfg.prune;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|