dotfiles/role/backup/default.nix
Raphael Borun Das Gupta bf0be939cc reformat all the nix files
using
    nix run -f channel:nixos-unstable nixfmt -c nixfmt $(git ls-files *.nix)
2020-04-05 19:05:38 +02:00

64 lines
1.7 KiB
Nix

# Regular backup role to a separate device
{ systemdMount, # Systemd mount name
borgArchiveFolder, # Absolute borg root folder
keepWithin ? "14d", keepWeekly ? "4", keepMonthly ? "6", keepYearly ? "-1"
, schedule ? "13:37", # Systemd Schedule of backup timer
}:
let pkgs = import <nixpkgs> { };
in {
systemd = {
services.backup = {
description =
"Backup of all user data and system configuration with BorgBackup";
serviceConfig.Type = "oneshot";
path = with pkgs; [ bash borgbackup ];
script = ''
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
systemctl start ${systemdMount}
export BORG_REPO=${borgArchiveFolder} \
BORG_BASE_DIR=${borgArchiveFolder}/borg-base-dir
echo "Backup started at `date`"
borg create --exclude /var/backup \
--exclude /var/tmp \
--exclude /var/cache \
$BORG_REPO::{hostname}-{now} \
/etc \
/home \
/root \
/var
sync
echo "Backup finished at `date`"
echo "Backup prune started at `date`"
borg prune --prefix '{hostname}-' \
--keep-within ${keepWithin} \
--keep-weekly ${keepWeekly} \
--keep-monthly ${keepMonthly} \
--keep-yearly ${keepYearly}
sync
echo "Backup prune finished at `date`"
systemctl stop ${systemdMount}
'';
};
timers.backup = {
description = "Backup Schedule";
timerConfig = {
OnCalendar = schedule;
Persistent = "true";
};
wantedBy = [ "timers.target" ];
};
};
}