diff --git a/flake.nix b/flake.nix index 2636e55..9385733 100644 --- a/flake.nix +++ b/flake.nix @@ -1,6 +1,6 @@ # flake for blakes nixos config # define new devices in outputs -# generation: 287 current 2025-10-11 09:51:39 25.05.20251006.20c4598 6.12.50 * +# generation: 288 current 2025-10-11 10:03:00 25.05.20251006.20c4598 6.12.50 * { description = "blakes nix config"; inputs = { diff --git a/modules/system/backups.nix b/modules/system/backups.nix index 12e37e2..b916485 100644 --- a/modules/system/backups.nix +++ b/modules/system/backups.nix @@ -1,69 +1,75 @@ { config, lib, pkgs, ... }: +/* +this module enables a backup script made with borg! +to use import & set the options below +to declare a backup add the following code +to a module and it will backup all listed paths +in a borg archive to the specified repo + + | <3yy> | + V V + modules.system.backups.baks = { + ${service} = { paths = [ cfg.data_dir ]; }; + }; +*/ + let cfg = config.modules.system.backups; + sec = config.sops.secrets; borg = "${pkgs.borgbackup}/bin/borg"; - backup_paths = lib.unique config.modules.system.backups.paths; - passwd_file = config.sops.secrets."borg_passwd".path; -# jobs = { -# sungger = { paths = [ "/var/lib/radarr" "/var/lib/sonarr" ]; }; -# hoass = { paths = [ "/var/lib/zigbee2mqtt" "/var/lib/hass" "/var/lib/mosquitto" ]; }; -# huh = { paths = [ "/home/blake/.nix" ]; }; -# }; + baks = lib.unique config.modules.system.backups.baks; + passwd_file = ; in { options.modules.system.backups = { enable = lib.mkEnableOption "enables backups with borg"; - paths = lib.mkOption { - type = lib.types.listOf lib.types.path; - default = []; - description = "list of directories to back up"; - }; - jobs = lib.mkOption { + baks = lib.mkOption { type = lib.types.attrsOf ( lib.types.attrsOf (lib.types.listOf lib.types.path) ); default = {}; - description = "Borg backup jobs; each job has a list of paths."; + description = "backup jobs, nested attribute sets should be = paths []"; }; repo = lib.mkOption { type = lib.types.path; default = "/holocron/borg"; description = "borg repository path"; }; - passphraseFile = lib.mkOption { + passwd_file = lib.mkOption { type = lib.types.path; - default = passwd_file; + default = sec."borg_passwd".path; description = "borg repository passphrase file"; }; }; - config = lib.mkIf (cfg.enable && backup_paths != []) { + config = lib.mkIf (cfg.enable && baks != {}) { systemd.services.backups = { description = "backup service with borg!"; path = [ pkgs.borgbackup ]; serviceConfig = { Type = "oneshot"; +# EnvironmentFile = config.modules.system.backups.passphraseFile; # the actual script borg is using ExecStart = pkgs.writeShellScript "borg-backup" '' set -euo pipefail export BORG_PASSPHRASE="$(cat ${passwd_file})" export BORG_REPO="${cfg.repo}" - timestamp="$(date +'%Y-%m-%dT%H:%M:%S')" + timestamp="$(date +'%Y-%m-%d_%H:%M:%S')" - # Initialize repo if it doesn't exist + # init repo in needed if ! borg info "$BORG_REPO" >/dev/null 2>&1; then echo "Initializing Borg repo at $BORG_REPO" borg init --encryption=repokey "$BORG_REPO" fi - echo "Starting backup run at $timestamp" + echo "starting backup at $timestamp" - # Loop over all jobs (attribute set keys) + # loop for each backup ${lib.concatStringsSep "\n\n" (lib.mapAttrsToList (bak_name: bak_paths: '' - echo "=== Backing up ${bak_name} ===" - archive="${bak_name}-$timestamp" - echo "Backing up paths: ${lib.concatStringsSep " " bak_paths.paths} → $archive" + echo "------------ Backing up ${bak_name} ------------" + archive="$timestamp-${bak_name}" + echo "backing up: ${lib.concatStringsSep " " bak_paths.paths} → $archive" borg create \ --verbose \ --filter AME \ @@ -74,7 +80,7 @@ in "$BORG_REPO::$archive" \ ${lib.concatStringsSep " " bak_paths.paths} - echo "Pruning old backups for ${bak_name}..." + echo "pruning old backups for ${bak_name}..." borg prune -v --list "$BORG_REPO" \ --prefix "${bak_name}-" \ --keep-daily=7 \ @@ -82,8 +88,7 @@ in --keep-monthly=-1 '' ) cfg.jobs)} - - echo "backup run complete at $BORG_REPO::$archive" + echo "backup run complete at \"$BORG_REPO::$archive\"" ''; }; }; @@ -110,6 +115,3 @@ in }; }; } - -# add to modules -# modules.system.backups.paths = lib.mkIf cfg.backups [ ]; diff --git a/modules/system/default.nix b/modules/system/default.nix index fdb4f80..28c6edd 100644 --- a/modules/system/default.nix +++ b/modules/system/default.nix @@ -26,9 +26,7 @@ - modules.system.backups.jobs = { - sungger = { paths = [ "/var/lib/radarr" "/var/lib/sonarr" ]; }; - hoass = { paths = [ "/var/lib/zigbee2mqtt" "/var/lib/hass" "/var/lib/mosquitto" ]; }; - huh = { paths = [ "/home/blake/.nix" ]; }; + modules.system.backups.baks = { + ${service} = { paths = [ cfg.data_dir ]; }; }; }