Move backup role
This commit is contained in:
parent
7a381c755e
commit
5ab6e73d31
1 changed files with 0 additions and 0 deletions
68
role/backup/default.nix
Normal file
68
role/backup/default.nix
Normal file
|
@ -0,0 +1,68 @@
|
|||
# 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" ];
|
||||
};
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue