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,22 +1,18 @@
{ {
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 (
source = lib.mkOption { { ... }:
type = lib.types.path; {
default = null; options = {
description = "The path to save."; enable = lib.mkEnableOption "Whether to enable backups for this path or not.";
};
dest = lib.mkOption { dest = lib.mkOption {
type = lib.types.either lib.types.path lib.types.str; type = lib.types.either lib.types.path lib.types.str;
@ -54,11 +50,18 @@ in
}; };
}; };
# Implementation }
)
);
};
config = config =
let
systemd_backups = map (
attrset:
let let
script = pkgs.writeShellApplication { script = pkgs.writeShellApplication {
name = "backup.sh"; name = "backup-${baseNameOf attrset.name}.sh";
runtimeInputs = [ runtimeInputs = [
pkgs.gnutar pkgs.gnutar
pkgs.gzip pkgs.gzip
@ -67,44 +70,50 @@ in
#!/bin/bash #!/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 in
lib.mkIf cfg.enable { {
systemd.timers.backup = { timers."backup-${baseNameOf attrset.name}" = {
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
timerConfig = { timerConfig = {
OnBootSec = "5m"; OnBootSec = "5m";
OnCalendar = "* *-*-* 00:00:00"; OnCalendar = "* *-*-* 00:00:00";
Unit = "backup.service"; Unit = "backup-${baseNameOf attrset.name}.service";
Persistent = true; Persistent = true;
}; };
}; };
systemd.services.backup = { services."backup-${baseNameOf attrset.name}" = {
enable = cfg.enable; enable = attrset.value.enable;
after = [ "network-online.target" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
description = "Saves a directory and its content."; description = "Saves a directory and its content.";
serviceConfig = { serviceConfig = {
Type = "simple"; Type = "simple";
ExecStart = "${script}/bin/backup.sh"; ExecStart = "${script}/bin/backup-${baseNameOf attrset.name}.sh";
}; };
}; };
}
) (lib.attrsets.attrsToList config.services.backup);
in
{
systemd = lib.attrsets.mergeAttrsList systemd_backups;
}; };
} }