backup module fix, it now supports multiple folders
This commit is contained in:
parent
da94e520ee
commit
cf8a2f0b74
5 changed files with 145 additions and 122 deletions
|
|
@ -15,7 +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
|
/etc/nixos/modules/backup
|
||||||
|
|
||||||
# machine-specific config
|
# machine-specific config
|
||||||
./bootloader.nix
|
./bootloader.nix
|
||||||
|
|
@ -49,12 +49,12 @@ in
|
||||||
services.backup = {
|
services.backup = {
|
||||||
"/home/zo/dev" = {
|
"/home/zo/dev" = {
|
||||||
enable = true;
|
enable = true;
|
||||||
dest = "/tmp/";
|
dest = "/tmp/backups/";
|
||||||
};
|
};
|
||||||
|
|
||||||
"/home/zo/Documents" = {
|
"/home/zo/Documents" = {
|
||||||
enable = true;
|
enable = true;
|
||||||
dest = "/tmp/";
|
dest = "/tmp/backups/";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,119 +0,0 @@
|
||||||
{
|
|
||||||
lib,
|
|
||||||
pkgs,
|
|
||||||
config,
|
|
||||||
...
|
|
||||||
}:
|
|
||||||
{
|
|
||||||
# Configuration
|
|
||||||
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;
|
|
||||||
default = null;
|
|
||||||
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 = {
|
|
||||||
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 {
|
|
||||||
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\"";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
config =
|
|
||||||
let
|
|
||||||
systemd_backups = map (
|
|
||||||
attrset:
|
|
||||||
let
|
|
||||||
script = pkgs.writeShellApplication {
|
|
||||||
name = "backup-${baseNameOf attrset.name}.sh";
|
|
||||||
runtimeInputs = [
|
|
||||||
pkgs.gnutar
|
|
||||||
pkgs.gzip
|
|
||||||
];
|
|
||||||
text = ''
|
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Archive creation and compression
|
|
||||||
SRC=${attrset.name}
|
|
||||||
DST=$(mktemp --suffix=.tar)
|
|
||||||
echo "Archiving and compressing $SRC to $DST."
|
|
||||||
${attrset.value.compression.command}
|
|
||||||
|
|
||||||
# Archive encryption (optionnal)
|
|
||||||
SRC=$DST
|
|
||||||
DST=$(mktemp --suffix=.tar.encrypted)
|
|
||||||
echo "Encyphering $SRC to $DST."
|
|
||||||
${attrset.value.encryption.command}
|
|
||||||
|
|
||||||
# Archive salvation
|
|
||||||
SRC=$DST
|
|
||||||
DST="${attrset.value.dest}/${baseNameOf attrset.name}.tar.enc"
|
|
||||||
echo "Saving $SRC to $DST."
|
|
||||||
${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
|
|
||||||
{
|
|
||||||
systemd = lib.attrsets.mergeAttrsList systemd_backups;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
85
modules/backup/default.nix
Normal file
85
modules/backup/default.nix
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
{
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
config,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
{
|
||||||
|
# Configuration
|
||||||
|
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;
|
||||||
|
default = null;
|
||||||
|
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 = {
|
||||||
|
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 {
|
||||||
|
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\"";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
config =
|
||||||
|
let
|
||||||
|
systemd_set = (
|
||||||
|
lib.attrsets.mapAttrs (
|
||||||
|
path: config:
|
||||||
|
(import ./systemd-utils.nix {
|
||||||
|
inherit path config;
|
||||||
|
script = import ./shell-utils.nix {
|
||||||
|
inherit path config pkgs;
|
||||||
|
};
|
||||||
|
})
|
||||||
|
) config.services.backup
|
||||||
|
);
|
||||||
|
|
||||||
|
merge =
|
||||||
|
key:
|
||||||
|
lib.pipe systemd_set [
|
||||||
|
builtins.attrValues
|
||||||
|
(map (cfg: cfg.${key}))
|
||||||
|
lib.mkMerge
|
||||||
|
];
|
||||||
|
in
|
||||||
|
{
|
||||||
|
systemd = {
|
||||||
|
timers = merge "timers";
|
||||||
|
services = merge "services";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
33
modules/backup/shell-utils.nix
Normal file
33
modules/backup/shell-utils.nix
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
path,
|
||||||
|
config,
|
||||||
|
pkgs,
|
||||||
|
}:
|
||||||
|
pkgs.writeShellApplication {
|
||||||
|
name = "backup-${baseNameOf path}.sh";
|
||||||
|
runtimeInputs = [
|
||||||
|
pkgs.gnutar
|
||||||
|
pkgs.gzip
|
||||||
|
];
|
||||||
|
text = ''
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Archive creation and compression
|
||||||
|
SRC=${path}
|
||||||
|
DST=$(mktemp --suffix=.tar)
|
||||||
|
echo "Archiving and compressing $SRC to $DST."
|
||||||
|
${config.compression.command}
|
||||||
|
|
||||||
|
# Archive encryption (optionnal)
|
||||||
|
SRC=$DST
|
||||||
|
DST=$(mktemp --suffix=.tar.encrypted)
|
||||||
|
echo "Encyphering $SRC to $DST."
|
||||||
|
${config.encryption.command}
|
||||||
|
|
||||||
|
# Archive salvation
|
||||||
|
SRC=$DST
|
||||||
|
DST="${config.dest}/${baseNameOf path}.tar.enc"
|
||||||
|
echo "Saving $SRC to $DST."
|
||||||
|
${config.salvation.command}
|
||||||
|
'';
|
||||||
|
}
|
||||||
24
modules/backup/systemd-utils.nix
Normal file
24
modules/backup/systemd-utils.nix
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
path,
|
||||||
|
config,
|
||||||
|
script,
|
||||||
|
}:
|
||||||
|
{
|
||||||
|
timers."backup-${baseNameOf path}" = {
|
||||||
|
wantedBy = [ "timers.target" ];
|
||||||
|
timerConfig = {
|
||||||
|
OnBootSec = "5m";
|
||||||
|
OnCalendar = "* *-*-* 00:00:00";
|
||||||
|
Unit = "backup-${baseNameOf path}.service";
|
||||||
|
Persistent = true;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
services."backup-${baseNameOf path}" = {
|
||||||
|
enable = config.enable;
|
||||||
|
description = "Saves a directory and its content.";
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "simple";
|
||||||
|
ExecStart = "${script}/bin/backup-${baseNameOf path}.sh";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue