From fad740947d826c1c04c5162fb57a06ec64b7a449 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 10 Apr 2022 13:24:12 -0700 Subject: add a module for backup with restic Do a single backup for the host, instead of running multiple ones. --- modules/services/backup/default.nix | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 modules/services/backup/default.nix (limited to 'modules/services/backup/default.nix') diff --git a/modules/services/backup/default.nix b/modules/services/backup/default.nix new file mode 100644 index 0000000..52378d3 --- /dev/null +++ b/modules/services/backup/default.nix @@ -0,0 +1,83 @@ +{ config, pkgs, lib, ... }: +let cfg = config.my.services.backup; +in { + options.my.services.backup = with lib; { + enable = mkEnableOption "Enable backups for this host"; + + repository = mkOption { + type = types.str; + example = "/data/slow/backups/system"; + description = "The repository to back up to"; + }; + + passwordFile = mkOption { + type = types.str; + example = "/var/lib/restic/password.txt"; + description = "Read the repository's password from this path"; + }; + + paths = mkOption { + type = with types; listOf str; + default = [ ]; + example = [ "/var/lib" "/home" ]; + description = "Paths to backup"; + }; + + exclude = mkOption { + type = with types; listOf str; + 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"; + }; + + pruneOpts = mkOption { + type = with types; listOf str; + default = [ + "--keep-last 10" + "--keep-hourly 24" + "--keep-daily 7" + "--keep-weekly 5" + "--keep-monthly 12" + "--keep-yearly 100" + ]; + example = [ "--keep-last 5" "--keep-weekly 2" ]; + description = '' + List of options to give to the `forget` subcommand after a backup. + ''; + }; + + timerConfig = mkOption { + # NOTE: I do not know how to cleanly set the type + default = { OnCalendar = "daily"; }; + example = { + OnCalendar = "00:05"; + RandomizedDelaySec = "5h"; + }; + description = '' + When to run the backup. See man systemd.timer for details. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + services.restic.backups = { + # Take care of included and excluded files + paths = cfg.paths; + extraBackupArgs = [ "--verbose=2" ] + ++ lib.optional (builtins.length cfg.exclude != 0) excludeArg; + # Take care of creating the repository if it doesn't exist + initialize = true; + inherit (cfg) passwordFile pruneOpts timerConfig repository; + }; + }; +} -- cgit 1.4.1