2024-06-02 21:12:25 +00:00
|
|
|
{ lib, config, pkgs, utils, ... }:
|
2024-06-02 19:35:28 +00:00
|
|
|
let
|
|
|
|
cfg = config.backup-helper;
|
|
|
|
restic-wrapper = pkgs.writeShellApplication {
|
|
|
|
name = "restic-wrapper";
|
|
|
|
runtimeInputs = [
|
|
|
|
pkgs.restic
|
|
|
|
];
|
|
|
|
text = ''
|
2024-06-02 21:41:37 +00:00
|
|
|
if [ -z "$BACKUP_HELPER_DIRS" ]; then
|
2024-06-02 21:50:20 +00:00
|
|
|
echo "BACKUP_HELPER_DIRS is not set"
|
2024-06-02 20:23:36 +00:00
|
|
|
exit 1;
|
|
|
|
fi
|
2024-06-02 21:50:20 +00:00
|
|
|
exit_code=0;
|
2024-06-02 21:41:37 +00:00
|
|
|
for i in ''${BACKUP_HELPER_DIRS//,/ }; do
|
|
|
|
echo "Starting backup of $i";
|
2024-06-02 22:01:36 +00:00
|
|
|
if ! output=$(restic backup "$i"); then
|
2024-06-02 21:58:24 +00:00
|
|
|
exit_code="$?";
|
2024-06-02 21:59:14 +00:00
|
|
|
echo "Backup of $i failed with exit code $?:"
|
2024-06-02 21:50:20 +00:00
|
|
|
echo "$output"
|
|
|
|
else
|
|
|
|
echo "Backup of $i successful:"
|
|
|
|
echo "$output"
|
|
|
|
fi
|
2024-06-02 21:41:37 +00:00
|
|
|
done
|
2024-06-02 21:50:20 +00:00
|
|
|
exit "$exit_code";
|
2024-06-02 19:35:28 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.backup-helper.enable = lib.mkEnableOption "Enable backup-helper";
|
|
|
|
options.backup-helper = {
|
|
|
|
restic-repository = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "rest:http://10.69.12.52:8000/backup-nix";
|
|
|
|
description = "Repository to use for restic backup.";
|
|
|
|
};
|
|
|
|
backup-dirs = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = "Directories to be backed up.";
|
|
|
|
};
|
|
|
|
schedule = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "*-*-* 00:00:00";
|
|
|
|
description = "Schedule for backups. Needs to be valid systemd OnCalendar value.";
|
|
|
|
};
|
|
|
|
password-file = lib.mkOption {
|
2024-06-02 20:19:45 +00:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2024-06-02 20:03:51 +00:00
|
|
|
default = null;
|
2024-06-02 19:35:28 +00:00
|
|
|
description = "File containing the restic password.";
|
|
|
|
};
|
|
|
|
randomized-delay = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 0;
|
|
|
|
description = "Randomized delay in seconds to spread out backups.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
2024-06-02 21:41:37 +00:00
|
|
|
systemd.services."backup-helper" = {
|
2024-06-02 20:17:33 +00:00
|
|
|
after = [ "network-online.target" ];
|
2024-06-02 19:35:28 +00:00
|
|
|
environment = {
|
|
|
|
RESTIC_REPOSITORY = cfg.restic-repository;
|
2024-06-02 21:41:37 +00:00
|
|
|
BACKUP_HELPER_DIRS = lib.strings.concatStringsSep "," cfg.backup-dirs;
|
2024-06-02 19:35:28 +00:00
|
|
|
} // lib.attrsets.optionalAttrs (builtins.hasAttr "password-file" cfg) {
|
|
|
|
RESTIC_PASSWORD_FILE = cfg.password-file;
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2024-06-02 21:41:37 +00:00
|
|
|
ExecStart = "${restic-wrapper}/bin/restic-wrapper";
|
2024-06-02 19:35:28 +00:00
|
|
|
};
|
|
|
|
};
|
2024-06-02 21:41:37 +00:00
|
|
|
systemd.timers."backup-helper" = {
|
2024-06-02 20:05:10 +00:00
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.schedule;
|
|
|
|
Persistent = true;
|
|
|
|
RandomizedDelaySec = cfg.randomized-delay;
|
2024-06-02 19:35:28 +00:00
|
|
|
};
|
2024-06-02 20:45:23 +00:00
|
|
|
wantedBy = [ "timers.target" ];
|
2024-06-02 20:44:14 +00:00
|
|
|
};
|
2024-06-02 19:35:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|