Add timed backup

This commit is contained in:
Fabian Hauser 2020-03-01 00:26:08 +00:00
parent e5d92728f0
commit 746680731e
2 changed files with 69 additions and 2 deletions

View file

@ -11,6 +11,7 @@
../hardware/nuc.nix ../hardware/nuc.nix
../role/base.nix ../role/base.nix
../role/dropbear.nix ../role/dropbear.nix
(import ../role/backup.nix {systemdMount = "var-backup.mount"; borgArchiveFolder = "/var/backup/montalin";})
]; ];
boot.tmpOnTmpfs = true; boot.tmpOnTmpfs = true;
@ -21,8 +22,8 @@
}; };
fileSystems = { fileSystems = {
"/" = { device = "/dev/mapper/root"; fsType = "btrfs"; }; "/" = { device = "/dev/mapper/root"; fsType = "btrfs"; options = [ "defaults" "noatime" ]; };
"/var/backup" = { device = "/dev/mapper/backup"; fsType = "ext4"; }; "/var/backup" = { device = "/dev/mapper/backup"; fsType = "ext4"; options = [ "defaults" "noauto" "noatime" ]; };
"/boot" = { device = "/dev/disk/by-uuid/0065-E4EA"; fsType = "vfat"; options = [ "defaults" "noatime" ]; }; "/boot" = { device = "/dev/disk/by-uuid/0065-E4EA"; fsType = "vfat"; options = [ "defaults" "noatime" ]; };
}; };

66
role/backup.nix Normal file
View file

@ -0,0 +1,66 @@
{
systemdMount,
borgArchiveFolder,
keepWithin? "14d",
keepWeekly? "4",
keepMonthly? "6",
keepYearly? "-1",
}:
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 = "13:37";
Persistent = "true";
};
wantedBy = [ "timers.target" ];
};
};
}