backup module improvement to support multiple folders.
This commit is contained in:
parent
92d0f25480
commit
da94e520ee
2 changed files with 108 additions and 91 deletions
|
|
@ -46,9 +46,17 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
services.backup.enable = true;
|
||||
services.backup.source = "/home/zo/dev";
|
||||
services.backup.dest = "/tmp/";
|
||||
services.backup = {
|
||||
"/home/zo/dev" = {
|
||||
enable = true;
|
||||
dest = "/tmp/";
|
||||
};
|
||||
|
||||
"/home/zo/Documents" = {
|
||||
enable = true;
|
||||
dest = "/tmp/";
|
||||
};
|
||||
};
|
||||
|
||||
programs = {
|
||||
steam.enable = true;
|
||||
|
|
|
|||
|
|
@ -1,22 +1,18 @@
|
|||
{
|
||||
lib,
|
||||
config,
|
||||
pkgs,
|
||||
config,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.backup;
|
||||
in
|
||||
{
|
||||
# Configuration
|
||||
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.";
|
||||
};
|
||||
options.services.backup = lib.mkOption {
|
||||
type = lib.types.attrsOf (
|
||||
lib.types.submodule (
|
||||
{ ... }:
|
||||
{
|
||||
options = {
|
||||
enable = lib.mkEnableOption "Whether to enable backups for this path or not.";
|
||||
|
||||
dest = lib.mkOption {
|
||||
type = lib.types.either lib.types.path lib.types.str;
|
||||
|
|
@ -54,11 +50,18 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
# Implementation
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
config =
|
||||
let
|
||||
systemd_backups = map (
|
||||
attrset:
|
||||
let
|
||||
script = pkgs.writeShellApplication {
|
||||
name = "backup.sh";
|
||||
name = "backup-${baseNameOf attrset.name}.sh";
|
||||
runtimeInputs = [
|
||||
pkgs.gnutar
|
||||
pkgs.gzip
|
||||
|
|
@ -67,44 +70,50 @@ in
|
|||
#!/bin/bash
|
||||
|
||||
# Archive creation and compression
|
||||
SRC=${cfg.source}
|
||||
SRC=${attrset.name}
|
||||
DST=$(mktemp --suffix=.tar)
|
||||
echo "Archiving and compressing $SRC to $DST."
|
||||
${cfg.compression.command}
|
||||
${attrset.value.compression.command}
|
||||
|
||||
# Archive encryption (optionnal)
|
||||
SRC=$DST
|
||||
DST=$(mktemp --suffix=.tar.encrypted)
|
||||
echo "Encyphering $SRC to $DST."
|
||||
${cfg.encryption.command}
|
||||
${attrset.value.encryption.command}
|
||||
|
||||
# Archive salvation
|
||||
SRC=$DST
|
||||
DST="${cfg.dest}/${baseNameOf cfg.source}.tar.enc"
|
||||
DST="${attrset.value.dest}/${baseNameOf attrset.name}.tar.enc"
|
||||
echo "Saving $SRC to $DST."
|
||||
${cfg.salvation.command}
|
||||
${attrset.value.salvation.command}
|
||||
'';
|
||||
};
|
||||
in
|
||||
lib.mkIf cfg.enable {
|
||||
systemd.timers.backup = {
|
||||
{
|
||||
timers."backup-${baseNameOf attrset.name}" = {
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnBootSec = "5m";
|
||||
OnCalendar = "* *-*-* 00:00:00";
|
||||
Unit = "backup.service";
|
||||
Unit = "backup-${baseNameOf attrset.name}.service";
|
||||
Persistent = true;
|
||||
};
|
||||
};
|
||||
systemd.services.backup = {
|
||||
enable = cfg.enable;
|
||||
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.sh";
|
||||
ExecStart = "${script}/bin/backup-${baseNameOf attrset.name}.sh";
|
||||
};
|
||||
};
|
||||
}
|
||||
) (lib.attrsets.attrsToList config.services.backup);
|
||||
in
|
||||
{
|
||||
systemd = lib.attrsets.mergeAttrsList systemd_backups;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue