From 05393c477a2ec1bfc0ed8c6a4f8a80066f5ec81d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Mon, 9 Apr 2018 13:35:37 +0200 Subject: borg on odin --- custom/borgbackup.nix | 30 +++++++++++++++++++++++++----- custom/lvm-snapshots.nix | 1 + hel.nix | 8 ++++---- odin.nix | 14 +++++++++----- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 91de89a5..77e7637a 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -5,6 +5,21 @@ with lib; let cfg = config.services.borgbackup; + lvmPath = { + options = { + LV = mkOption { + type = types.str; + }; + VG = mkOption { + type = types.str; + }; + }; + }; + + pathType = if cfg.snapshots == "lvm" then types.submodule lvmPath else types.path; + + systemdPath = path: if cfg.snapshots == "lvm" then escapeSystemdPath "${path.VG}/${path.LV}" else escapeSystemdPath path; + targetOptions = { options = { repo = mkOption { @@ -12,7 +27,7 @@ let }; paths = mkOption { - type = types.listOf types.str; + type = types.listOf pathType; default = []; }; @@ -46,7 +61,7 @@ in { options = { services.borgbackup = { snapshots = mkOption { - type = types.nullOr (types.enum ["btrfs"]); + type = types.nullOr (types.enum ["btrfs" "lvm"]); default = null; }; @@ -63,8 +78,13 @@ in { config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; + + services.lvm-snapshots = mkIf (cfg.snapshots == "lvm") (listToAttrs (map (path: nameValuePair (systemdPath path) { + inherit (path) LV VG; + mountName = "snapshot-${systemdPath path}"; + }) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets))))); - systemd.timers = (listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" { + systemd.timers = (listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${systemdPath path}" { wantedBy = [ "timers.target" ]; timerConfig = { @@ -102,7 +122,7 @@ in { --filter 'AME' \ --exclude-caches \ --keep-exclude-tags \ - --patterns-from .backup \ + --patterns-from .backup-${target} \ --one-file-system \ --compression auto,lzma \ ${tCfg.repo}::${cfg.prefix}$1-{utcnow} @@ -133,7 +153,7 @@ in { --lock-wait ${toString tCfg.lockWait} \ --list \ --stats \ - --prefix "${cfg.prefix}${path}" \ + --prefix "${cfg.prefix}${escapeSystemdPath path}" \ ${concatStringsSep " " args} \ ${tCfg.repo} '') tCfg.prune); diff --git a/custom/lvm-snapshots.nix b/custom/lvm-snapshots.nix index 159fe98b..ea69e1ff 100644 --- a/custom/lvm-snapshots.nix +++ b/custom/lvm-snapshots.nix @@ -50,6 +50,7 @@ in { mountPoint = mkOption { type = types.path; + readOnly = true; default = "/mnt"; }; }; diff --git a/hel.nix b/hel.nix index fc2edd28..a8a3db89 100644 --- a/hel.nix +++ b/hel.nix @@ -523,9 +523,9 @@ targets = { "odin" = { repo = "borg.odin:/srv/backup/borg"; - paths = [ "home-gkleen" ]; + paths = [ "/home/gkleen" ]; prune = { - "home-gkleen" = + "/home/gkleen" = [ "--keep-within 24H" "--keep-daily 31" "--keep-monthly 12" @@ -535,9 +535,9 @@ }; "munin" = { repo = "borg.munin:borg"; - paths = [ "home-gkleen" ]; + paths = [ "/home/gkleen" ]; prune = { - "home-gkleen" = + "/home/gkleen" = [ "--keep-within 24H" "--keep-daily 31" "--keep-monthly 12" diff --git a/odin.nix b/odin.nix index 588ff9e4..ec8731e9 100644 --- a/odin.nix +++ b/odin.nix @@ -148,11 +148,15 @@ networks = ["127.0.0.0/8" "[::ffff:127.0.0.0]/104" "[::1]/128" "10.141.0.0/16"]; }; - services.lvm-snapshots = { - snapshots = { - mail = { - LV = "mail"; - VG = "raid6"; + services.borgbackup = { + snapshots = "lvm"; + prefix = "automatic.yggdrasil.asgard.odin."; + targets = { + "munin" = { + repo = "borg.munin:borg"; + paths = [ + { VG = "raid6"; LV = "mail"; } + ]; }; }; }; -- cgit v1.2.3