2023-12-27 18:14:49 +00:00
|
|
|
{ pkgs, lib, config, ... }:
|
|
|
|
let
|
2023-12-29 12:46:12 +00:00
|
|
|
cfg = config.lab.backups;
|
2023-12-27 18:14:49 +00:00
|
|
|
beforeEverything = pkgs.writeShellScriptBin "beforeEverything" ''
|
2024-01-04 21:52:31 +00:00
|
|
|
if [ -d "${cfg.snapshotLocation}" ]; then
|
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume delete ${cfg.snapshotLocation}
|
|
|
|
fi
|
2023-12-27 18:14:49 +00:00
|
|
|
|
2024-01-04 21:52:31 +00:00
|
|
|
${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r ${cfg.subvolumeLocation} ${cfg.snapshotLocation}
|
2023-12-27 18:14:49 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
borgmaticConfig = pkgs.writeTextFile {
|
|
|
|
name = "borgmatic-config";
|
|
|
|
text = ''
|
|
|
|
source_directories:
|
2024-01-04 21:52:31 +00:00
|
|
|
- ${cfg.snapshotLocation}
|
2023-12-27 18:14:49 +00:00
|
|
|
repositories:
|
|
|
|
- path: ${cfg.repoLocation}
|
2024-01-04 21:52:31 +00:00
|
|
|
label: nfs
|
2023-12-27 18:14:49 +00:00
|
|
|
keep_daily: 7
|
|
|
|
keep_weekly: 4
|
|
|
|
keep_monthly: 6
|
|
|
|
unknown_unencrypted_repo_access_is_ok: true
|
|
|
|
before_everything:
|
|
|
|
- ${beforeEverything}/bin/beforeEverything
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2023-12-29 12:46:12 +00:00
|
|
|
options.lab.backups = {
|
2023-12-27 18:14:49 +00:00
|
|
|
enable = lib.mkOption {
|
|
|
|
default = false;
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = ''
|
|
|
|
Whether to enable backups of persistent data on this machine.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
repoLocation = lib.mkOption {
|
2024-01-04 21:52:31 +00:00
|
|
|
# TODO: maybe make sure data disk is enabled? is there an "ensure" method in nix?
|
|
|
|
default = "${config.lab.dataDisk.mountPoint}/backups/nfs.borg";
|
2023-12-27 18:14:49 +00:00
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
Location of the Borg repository to back up to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-04 21:52:31 +00:00
|
|
|
subvolumeLocation = lib.mkOption {
|
|
|
|
default = "${config.lab.dataDisk.mountPoint}/nfs";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
|
|
|
Location of the btrfs subvolume holding the data.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
snapshotLocation = lib.mkOption {
|
|
|
|
default = "${config.lab.dataDisk.mountPoint}/nfs-backup";
|
2023-12-27 18:14:49 +00:00
|
|
|
type = lib.types.str;
|
|
|
|
description = ''
|
2024-01-04 21:52:31 +00:00
|
|
|
Location to (temporary) create a snapshot of the subvolume.
|
2023-12-27 18:14:49 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2024-01-04 21:52:31 +00:00
|
|
|
environment.systemPackages = with pkgs; [ borgbackup ];
|
2023-12-27 18:14:49 +00:00
|
|
|
# Converted from:
|
|
|
|
# https://github.com/borgmatic-collective/borgmatic/tree/84823dfb912db650936e3492f6ead7e0e0d32a0f/sample/systemd
|
|
|
|
systemd.services.borgmatic = {
|
|
|
|
description = "borgmatic backup";
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
2024-01-04 21:52:31 +00:00
|
|
|
|
2023-12-27 18:14:49 +00:00
|
|
|
unitConfig = {
|
|
|
|
ConditionACPower = true;
|
|
|
|
};
|
2024-01-04 21:52:31 +00:00
|
|
|
|
2023-12-27 18:14:49 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
Nice = 19;
|
|
|
|
CPUSchedulingPolicy = "batch";
|
|
|
|
IOSchedulingClass = "best-effort";
|
|
|
|
IOSchedulingPriority = 7;
|
|
|
|
IOWeight = 100;
|
|
|
|
Restart = "no";
|
|
|
|
LogRateLimitIntervalSec = 0;
|
|
|
|
};
|
2024-01-04 21:52:31 +00:00
|
|
|
|
2023-12-27 18:14:49 +00:00
|
|
|
preStart = "${pkgs.coreutils}/bin/sleep 1m";
|
2024-01-04 21:52:31 +00:00
|
|
|
script = "${pkgs.systemd}/bin/systemd-inhibit --who=\"borgmatic\" --what=\"sleep:shutdown\" --why=\"Prevent interrupting scheduled backup\" ${pkgs.borgmatic}/bin/borgmatic --verbosity -2 --syslog-verbosity 1 -c ${borgmaticConfig}";
|
2023-12-27 18:14:49 +00:00
|
|
|
};
|
|
|
|
|
2024-01-04 21:52:31 +00:00
|
|
|
systemd.timers.borgmatic = {
|
|
|
|
description = "Run borgmatic backup";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "*-*-* 3:00:00";
|
|
|
|
Persistent = true;
|
|
|
|
RandomizedDelaySec = "3h";
|
|
|
|
};
|
2023-12-27 18:14:49 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|