backup module improvement to support multiple folders.

This commit is contained in:
boyreau 2026-08-18 15:52:18 +02:00
commit da94e520ee
2 changed files with 108 additions and 91 deletions

View file

@ -46,9 +46,17 @@ in
}; };
}; };
services.backup.enable = true; services.backup = {
services.backup.source = "/home/zo/dev"; "/home/zo/dev" = {
services.backup.dest = "/tmp/"; enable = true;
dest = "/tmp/";
};
"/home/zo/Documents" = {
enable = true;
dest = "/tmp/";
};
};
programs = { programs = {
steam.enable = true; steam.enable = true;

View file

@ -1,110 +1,119 @@
{ {
lib, lib,
config,
pkgs, pkgs,
config,
... ...
}: }:
let
cfg = config.services.backup;
in
{ {
# Configuration # Configuration
options.services.backup = { options.services.backup = lib.mkOption {
enable = lib.mkEnableOption "Whether to enable backups or not."; type = lib.types.attrsOf (
lib.types.submodule (
{ ... }:
{
options = {
enable = lib.mkEnableOption "Whether to enable backups for this path or not.";
source = lib.mkOption { dest = lib.mkOption {
type = lib.types.path; type = lib.types.either lib.types.path lib.types.str;
default = null; default = null;
description = "The path to save."; description = "A destination for the backup.";
}; };
dest = lib.mkOption { compression = {
type = lib.types.either lib.types.path lib.types.str; command = lib.mkOption {
default = null; type = lib.types.nullOr lib.types.str;
description = "A destination for the backup."; default = "tar -caf \"$DST\" \"$SRC\"";
}; description = "A bash command to compress backups. Defaults to an uncompressed tar archive.";
};
};
compression = { encryption = {
command = lib.mkOption { command = lib.mkOption {
type = lib.types.nullOr lib.types.str; type = lib.types.str;
default = "tar -caf \"$DST\" \"$SRC\""; description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat).";
description = "A bash command to compress backups. Defaults to an uncompressed tar archive."; default = "cat \"$SRC\" > \"$DST\"";
}; };
}; key = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = "A key to encrypt backups.";
default = null;
};
};
encryption = { salvation = {
command = lib.mkOption { command = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat)."; description = "A bash command to save the archive. Defaults to a copy from source to dest.";
default = "cat \"$SRC\" > \"$DST\""; default = "cp \"$SRC\" \"$DST\"";
}; };
key = lib.mkOption { };
type = lib.types.nullOr lib.types.path; };
description = "A key to encrypt backups.";
default = null;
};
};
salvation = { }
command = lib.mkOption { )
type = lib.types.str; );
description = "A bash command to save the archive. Defaults to a copy from source to dest.";
default = "cp \"$SRC\" \"$DST\"";
};
};
}; };
# Implementation
config = config =
let let
script = pkgs.writeShellApplication { systemd_backups = map (
name = "backup.sh"; attrset:
runtimeInputs = [ let
pkgs.gnutar script = pkgs.writeShellApplication {
pkgs.gzip name = "backup-${baseNameOf attrset.name}.sh";
]; runtimeInputs = [
text = '' pkgs.gnutar
#!/bin/bash pkgs.gzip
];
text = ''
#!/bin/bash
# Archive creation and compression # Archive creation and compression
SRC=${cfg.source} SRC=${attrset.name}
DST=$(mktemp --suffix=.tar) DST=$(mktemp --suffix=.tar)
echo "Archiving and compressing $SRC to $DST." echo "Archiving and compressing $SRC to $DST."
${cfg.compression.command} ${attrset.value.compression.command}
# Archive encryption (optionnal) # Archive encryption (optionnal)
SRC=$DST SRC=$DST
DST=$(mktemp --suffix=.tar.encrypted) DST=$(mktemp --suffix=.tar.encrypted)
echo "Encyphering $SRC to $DST." echo "Encyphering $SRC to $DST."
${cfg.encryption.command} ${attrset.value.encryption.command}
# Archive salvation # Archive salvation
SRC=$DST SRC=$DST
DST="${cfg.dest}/${baseNameOf cfg.source}.tar.enc" DST="${attrset.value.dest}/${baseNameOf attrset.name}.tar.enc"
echo "Saving $SRC to $DST." echo "Saving $SRC to $DST."
${cfg.salvation.command} ${attrset.value.salvation.command}
''; '';
}; };
in
{
timers."backup-${baseNameOf attrset.name}" = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "5m";
OnCalendar = "* *-*-* 00:00:00";
Unit = "backup-${baseNameOf attrset.name}.service";
Persistent = true;
};
};
services."backup-${baseNameOf attrset.name}" = {
enable = attrset.value.enable;
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
description = "Saves a directory and its content.";
serviceConfig = {
Type = "simple";
ExecStart = "${script}/bin/backup-${baseNameOf attrset.name}.sh";
};
};
}
) (lib.attrsets.attrsToList config.services.backup);
in in
lib.mkIf cfg.enable { {
systemd.timers.backup = { systemd = lib.attrsets.mergeAttrsList systemd_backups;
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "5m";
OnCalendar = "* *-*-* 00:00:00";
Unit = "backup.service";
Persistent = true;
};
};
systemd.services.backup = {
enable = cfg.enable;
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
description = "Saves a directory and its content.";
serviceConfig = {
Type = "simple";
ExecStart = "${script}/bin/backup.sh";
};
};
}; };
} }