288 current 2025-10-11 10:03:00 25.05.20251006.20c4598 6.12.50 *

This commit is contained in:
2025-10-11 10:22:54 -05:00
parent 0cb49c0c42
commit f51570624b
3 changed files with 35 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
# flake for blakes nixos config # flake for blakes nixos config
# define new devices in outputs # 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"; description = "blakes nix config";
inputs = { inputs = {

View File

@@ -1,69 +1,75 @@
{ config, lib, pkgs, ... }: { 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 let
cfg = config.modules.system.backups; cfg = config.modules.system.backups;
sec = config.sops.secrets;
borg = "${pkgs.borgbackup}/bin/borg"; borg = "${pkgs.borgbackup}/bin/borg";
backup_paths = lib.unique config.modules.system.backups.paths; baks = lib.unique config.modules.system.backups.baks;
passwd_file = config.sops.secrets."borg_passwd".path; passwd_file = ;
# 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" ]; };
# };
in in
{ {
options.modules.system.backups = { options.modules.system.backups = {
enable = lib.mkEnableOption "enables backups with borg"; enable = lib.mkEnableOption "enables backups with borg";
paths = lib.mkOption { baks = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [];
description = "list of directories to back up";
};
jobs = lib.mkOption {
type = lib.types.attrsOf ( lib.types.attrsOf (lib.types.listOf lib.types.path) ); type = lib.types.attrsOf ( lib.types.attrsOf (lib.types.listOf lib.types.path) );
default = {}; default = {};
description = "Borg backup jobs; each job has a list of paths."; description = "backup jobs, nested attribute sets should be <bak_name> = paths [<list_of_paths>]";
}; };
repo = lib.mkOption { repo = lib.mkOption {
type = lib.types.path; type = lib.types.path;
default = "/holocron/borg"; default = "/holocron/borg";
description = "borg repository path"; description = "borg repository path";
}; };
passphraseFile = lib.mkOption { passwd_file = lib.mkOption {
type = lib.types.path; type = lib.types.path;
default = passwd_file; default = sec."borg_passwd".path;
description = "borg repository passphrase file"; description = "borg repository passphrase file";
}; };
}; };
config = lib.mkIf (cfg.enable && backup_paths != []) { config = lib.mkIf (cfg.enable && baks != {}) {
systemd.services.backups = { systemd.services.backups = {
description = "backup service with borg!"; description = "backup service with borg!";
path = [ pkgs.borgbackup ]; path = [ pkgs.borgbackup ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
# EnvironmentFile = config.modules.system.backups.passphraseFile;
# the actual script borg is using # the actual script borg is using
ExecStart = pkgs.writeShellScript "borg-backup" '' ExecStart = pkgs.writeShellScript "borg-backup" ''
set -euo pipefail set -euo pipefail
export BORG_PASSPHRASE="$(cat ${passwd_file})" export BORG_PASSPHRASE="$(cat ${passwd_file})"
export BORG_REPO="${cfg.repo}" 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 if ! borg info "$BORG_REPO" >/dev/null 2>&1; then
echo "Initializing Borg repo at $BORG_REPO" echo "Initializing Borg repo at $BORG_REPO"
borg init --encryption=repokey "$BORG_REPO" borg init --encryption=repokey "$BORG_REPO"
fi 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: ${lib.concatStringsSep "\n\n" (lib.mapAttrsToList (bak_name: bak_paths:
'' ''
echo "=== Backing up ${bak_name} ===" echo "------------ Backing up ${bak_name} ------------"
archive="${bak_name}-$timestamp" archive="$timestamp-${bak_name}"
echo "Backing up paths: ${lib.concatStringsSep " " bak_paths.paths} $archive" echo "backing up: ${lib.concatStringsSep " " bak_paths.paths} $archive"
borg create \ borg create \
--verbose \ --verbose \
--filter AME \ --filter AME \
@@ -74,7 +80,7 @@ in
"$BORG_REPO::$archive" \ "$BORG_REPO::$archive" \
${lib.concatStringsSep " " bak_paths.paths} ${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" \ borg prune -v --list "$BORG_REPO" \
--prefix "${bak_name}-" \ --prefix "${bak_name}-" \
--keep-daily=7 \ --keep-daily=7 \
@@ -82,8 +88,7 @@ in
--keep-monthly=-1 --keep-monthly=-1
'' ''
) cfg.jobs)} ) 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 [ <path> ];

View File

@@ -26,9 +26,7 @@
modules.system.backups.jobs = { modules.system.backups.baks = {
sungger = { paths = [ "/var/lib/radarr" "/var/lib/sonarr" ]; }; ${service} = { paths = [ cfg.data_dir ]; };
hoass = { paths = [ "/var/lib/zigbee2mqtt" "/var/lib/hass" "/var/lib/mosquitto" ]; };
huh = { paths = [ "/home/blake/.nix" ]; };
}; };
} }