backup module yay

This commit is contained in:
boyreau 2026-08-17 17:00:44 +02:00
commit 31482db136
2 changed files with 98 additions and 9 deletions

View file

@ -15,9 +15,7 @@ in
/etc/nixos/modules/security.nix /etc/nixos/modules/security.nix
/etc/nixos/modules/ssl.nix /etc/nixos/modules/ssl.nix
/etc/nixos/modules/tailscale.nix /etc/nixos/modules/tailscale.nix
/etc/nixos/modules/backup.nix
# temporary headscale
/etc/nixos/modules/headscale
# machine-specific config # machine-specific config
./bootloader.nix ./bootloader.nix
@ -48,6 +46,10 @@ in
}; };
}; };
services.backup.enable = true;
services.backup.source = "/home/zo/dev";
services.backup.dest = "/tmp/";
programs = { programs = {
steam.enable = true; steam.enable = true;
fuse.userAllowOther = true; fuse.userAllowOther = true;

View file

@ -1,8 +1,14 @@
{ lib, config, ... }: {
lib,
config,
pkgs,
...
}:
let let
cfg = config.services.backup; cfg = config.services.backup;
in in
{ {
# Configuration
options.services.backup = { options.services.backup = {
enable = lib.mkEnableOption "Whether to enable backups or not."; enable = lib.mkEnableOption "Whether to enable backups or not.";
@ -18,17 +24,98 @@ in
description = "A destination for the backup."; description = "A destination for the backup.";
}; };
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.";
};
};
encryption = { encryption = {
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\"";
};
key = lib.mkOption { key = lib.mkOption {
type = lib.types.path; type = lib.types.nullOr lib.types.path;
description = "A key to encrypt backups."; 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\"";
}; };
}; };
}; };
config = lib.mkIf cfg.enable { # 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 = { systemd.services.backup = {
wantedBy = [ "multi-user.target" ]; 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";
};
}; };
}; };
} }