nixos-config/modules/rbackup.nix

106 lines
2.3 KiB
Nix
Raw Normal View History

2024-08-29 03:26:04 +02:00
{
config,
lib,
pkgs,
...
}:
with lib;
let
escape = escapeShellArg;
cfg = config.rbackup;
2024-08-29 03:26:04 +02:00
start =
name: plan:
let
login-name = "root";
identity = plan.sshKeyPath;
ssh = "ssh -i ${escape identity}";
in
pkgs.writers.writeBash "backup.${name}" ''
set -efu
rsync_src=${escape plan.src}
rsync_dst=${escape plan.dst}
echo >&2 "update snapshot current; $rsync_dst <- $rsync_src"
rsync \
--rsh=${escape ssh} \
--append -avz \
2023-11-18 12:49:47 +01:00
${optionalString plan.delete "--delete"} \
"$rsync_src/" \
"$rsync_dst"
'';
in
{
options.rbackup = {
plans = mkOption {
default = { };
2024-08-29 03:26:04 +02:00
type = types.attrsOf (
types.submodule (
{ config, ... }:
{
options = {
sshKeyPath = mkOption {
type = types.str;
};
src = mkOption {
type = types.str;
};
dst = mkOption {
type = types.str;
};
startAt = mkOption {
default = "hourly";
type = with types; nullOr str; # TODO systemd.time(7)'s calendar event
};
delete = mkOption {
type = types.bool;
default = true;
description = "delete old files (adds the --delete argument to rsync)";
};
timerConfig = mkOption {
type = with types; attrsOf str;
default = optionalAttrs (config.startAt != null) {
OnCalendar = config.startAt;
};
};
};
2024-08-29 03:26:04 +02:00
}
)
);
};
};
config = {
2024-08-29 03:26:04 +02:00
systemd.services = mapAttrs' (
name: plan:
nameValuePair "rbackup.${name}" {
path = with pkgs; [
coreutils
gnused
openssh
rsync
util-linux
];
restartIfChanged = false;
serviceConfig = rec {
ExecStart = start name plan;
SyslogIdentifier = ExecStart.name;
Type = "oneshot";
};
}
) cfg.plans;
2024-08-29 03:26:04 +02:00
systemd.timers = mapAttrs' (
name: plan:
nameValuePair "rbackup.${name}" {
wantedBy = [ "timers.target" ];
timerConfig = plan.timerConfig;
2024-08-29 03:26:04 +02:00
}
) cfg.plans;
};
}