2026-08-17 17:00:44 +02:00
|
|
|
{
|
|
|
|
|
lib,
|
|
|
|
|
config,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
2026-08-12 17:45:17 +02:00
|
|
|
let
|
|
|
|
|
cfg = config.services.backup;
|
|
|
|
|
in
|
|
|
|
|
{
|
2026-08-17 17:00:44 +02:00
|
|
|
# Configuration
|
2026-08-12 17:45:17 +02:00
|
|
|
options.services.backup = {
|
|
|
|
|
enable = lib.mkEnableOption "Whether to enable backups or not.";
|
|
|
|
|
|
|
|
|
|
source = lib.mkOption {
|
|
|
|
|
type = lib.types.path;
|
|
|
|
|
default = null;
|
|
|
|
|
description = "The path to save.";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dest = lib.mkOption {
|
|
|
|
|
type = lib.types.either lib.types.path lib.types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
description = "A destination for the backup.";
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-17 17:00:44 +02:00
|
|
|
compression = {
|
|
|
|
|
command = lib.mkOption {
|
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
|
default = "tar -caf \"$DST\" \"$SRC\"";
|
|
|
|
|
description = "A bash command to compress backups. Defaults to an uncompressed tar archive.";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-12 17:45:17 +02:00
|
|
|
encryption = {
|
2026-08-17 17:00:44 +02:00
|
|
|
command = lib.mkOption {
|
|
|
|
|
type = lib.types.str;
|
|
|
|
|
description = "A bash command to encrypt backups. Defaults to a clear archive (simple cat).";
|
|
|
|
|
default = "cat \"$SRC\" > \"$DST\"";
|
|
|
|
|
};
|
2026-08-12 17:45:17 +02:00
|
|
|
key = lib.mkOption {
|
2026-08-17 17:00:44 +02:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
2026-08-12 17:45:17 +02:00
|
|
|
description = "A key to encrypt backups.";
|
2026-08-17 17:00:44 +02:00
|
|
|
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\"";
|
2026-08-12 17:45:17 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-17 17:00:44 +02:00
|
|
|
# Implementation
|
|
|
|
|
config =
|
|
|
|
|
let
|
|
|
|
|
script = pkgs.writeShellApplication {
|
|
|
|
|
name = "backup.sh";
|
|
|
|
|
runtimeInputs = [
|
|
|
|
|
pkgs.gnutar
|
|
|
|
|
pkgs.gzip
|
|
|
|
|
];
|
|
|
|
|
text = ''
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
# Archive creation and compression
|
|
|
|
|
SRC=${cfg.source}
|
|
|
|
|
DST=$(mktemp --suffix=.tar)
|
|
|
|
|
echo "Archiving and compressing $SRC to $DST."
|
|
|
|
|
${cfg.compression.command}
|
|
|
|
|
''
|
|
|
|
|
+ (
|
|
|
|
|
if cfg.encryption.command != null then
|
|
|
|
|
/* bash */ ''
|
|
|
|
|
|
|
|
|
|
# Archive encryption (optionnal)
|
|
|
|
|
SRC=$DST
|
|
|
|
|
DST=$(mktemp --suffix=.tar.encrypted)
|
|
|
|
|
echo "Encyphering $SRC to $DST."
|
|
|
|
|
''
|
|
|
|
|
+ cfg.encryption.command
|
|
|
|
|
+ "
|
|
|
|
|
"
|
|
|
|
|
else
|
|
|
|
|
""
|
|
|
|
|
)
|
|
|
|
|
+ /* bash */ ''
|
|
|
|
|
|
|
|
|
|
# Archive salvation
|
|
|
|
|
SRC=$DST
|
|
|
|
|
DST="${cfg.dest}/${baseNameOf cfg.source}.tar.enc"
|
|
|
|
|
echo "Saving $SRC to $DST."
|
|
|
|
|
${cfg.salvation.command}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
lib.mkIf cfg.enable {
|
|
|
|
|
systemd.timers.backup = {
|
|
|
|
|
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";
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-08-12 17:45:17 +02:00
|
|
|
};
|
|
|
|
|
}
|