backup-helper/backup.nix

102 lines
2.9 KiB
Nix

{
lib,
config,
pkgs,
...
}:
let
cfg = config.backup-helper;
restic-wrapper = pkgs.writeShellApplication {
name = "restic-wrapper";
runtimeInputs = [
pkgs.restic
];
text = ''
if [ -z "$BACKUP_HELPER_DIRS" ]; then
echo "BACKUP_HELPER_DIRS is not set"
exit 1;
fi
exit_code=0;
for i in ''${BACKUP_HELPER_DIRS//,/ }; do
echo "Starting backup of $i";
if ! output=$(restic backup "$i"); then
exit_code=1;
echo "Backup of $i failed with exit code $?:"
echo "$output"
else
echo "Backup of $i successful:"
echo "$output"
fi
done
if [ "$BACKUP_FORGET" -eq 1 ]; then
echo "Removing old backups"
output=$(restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune)
echo "$output"
fi
exit "$exit_code";
'';
};
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 {
type = lib.types.nullOr lib.types.str;
default = null;
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.";
};
forget = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Run restic forget after backup.";
};
};
config = lib.mkIf cfg.enable {
systemd.services."backup-helper" = {
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
environment =
{
RESTIC_REPOSITORY = cfg.restic-repository;
BACKUP_HELPER_DIRS = lib.strings.concatStringsSep "," cfg.backup-dirs;
BACKUP_FORGET = if cfg.forget then "1" else "0";
}
// lib.attrsets.optionalAttrs (builtins.hasAttr "password-file" cfg) {
RESTIC_PASSWORD_FILE = cfg.password-file;
};
serviceConfig = {
Type = "oneshot";
ExecStart = "${restic-wrapper}/bin/restic-wrapper";
};
};
systemd.timers."backup-helper" = {
timerConfig = {
OnCalendar = cfg.schedule;
Persistent = true;
RandomizedDelaySec = cfg.randomized-delay;
};
wantedBy = [ "timers.target" ];
};
};
}