From 0d8c62f7b347966ca545dd77528a31780b94f792 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:50:14 +0100 Subject: refactor borgbackup --- custom/borgbackup.nix | 16 +++++++--------- custom/btrfs-snapshots.nix | 11 +++-------- custom/lvm-snapshots.nix | 7 ++++++- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 3871af0a..0db4b5da 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -21,7 +21,7 @@ let systemdPath = path: escapeSystemdPath (if cfg.snapshots == "lvm" then "${path.VG}-${path.LV}" else path); - withPrefix = path: config.services.btrfs-snapshots.mountPrefix + path; + withSuffix = path: path + if cfg.snapshots == "btrfs" then config.services.btrfs-snapshots.mountSuffix else config.services.lvm-snapshots.mountSuffix; mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else config.services.btrfs-snapshots.mountPoint; @@ -86,18 +86,16 @@ in { }; }; - imports = [ - ./lvm-snapshots.nix - ./btrfs-snapshots.nix - ]; + imports = + optional (cfg.snapshots == "lvm") ./lvm-snapshots.nix + ++ optional (cfg.snapshats == "btrfs") ./btrfs-snapshots.nix; config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { - services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; services.lvm-snapshots.snapshots = mkIf (cfg.snapshots == "lvm") (listToAttrs (map (path: nameValuePair (path.VG + "-" + path.LV) { inherit (path) LV VG; - mountName = withPrefix (path.VG + "-" + path.LV); + mountName = withSuffix (path.VG + "-" + path.LV); }) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets))))); systemd.targets."timers-borg" = { @@ -159,12 +157,12 @@ in { unitConfig = { AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; DefaultDependencies = false; - RequiresMountsFor = mkIf (cfg.snapshots == "lvm") [ "${mountPoint}/${withPrefix "%I"}" ]; + RequiresMountsFor = mkIf (cfg.snapshots == "lvm") [ "${mountPoint}/${withSuffix "%I"}" ]; }; serviceConfig = { Type = "oneshot"; - WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${mountPoint}/${withPrefix "%I"}" else "${mountPoint}/${withPrefix "%i"}"); + WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${mountPoint}/${withPrefix "%i"}"); Nice = 15; IOSchedulingClass = 2; IOSchedulingPriority = 7; diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix index 39e0bf0c..034b5019 100644 --- a/custom/btrfs-snapshots.nix +++ b/custom/btrfs-snapshots.nix @@ -5,21 +5,16 @@ with lib; let cfg = config.services.btrfs-snapshots; - snapshotMount = str: "${cfg.mountPoint}/${cfg.mountPrefix}${str}"; + snapshotMount = str: "${str}${cfg.mountSuffix}"; in { options = { services.btrfs-snapshots = { enable = mkEnableOption "a systemd unit for btrfs snapshots"; - mountPoint = mkOption { - type = types.path; - default = "/mnt"; - }; - - mountPrefix = mkOption { + mountSuffix = mkOption { type = types.str; - default = "snapshot-"; + default = ".snapshot"; }; readOnly = mkOption { diff --git a/custom/lvm-snapshots.nix b/custom/lvm-snapshots.nix index 98c64330..13cbf421 100644 --- a/custom/lvm-snapshots.nix +++ b/custom/lvm-snapshots.nix @@ -7,7 +7,7 @@ let cfg = config.services.lvm-snapshots; snapshotMount = name: "${cfg.mountPoint}/${if isNull cfg.snapshots."${name}".mountName then name else cfg.snapshots."${name}".mountName}"; - snapshotName = name: "${name}-snapshot"; + snapshotName = name: "${name}-${cfg.mountSuffix}"; snapshotConfig = { options = { @@ -53,6 +53,11 @@ in { type = types.path; default = "/mnt"; }; + + mountSuffix = mkOption { + type = types.str; + default = "-snapshot"; + }; }; }; -- cgit v1.2.3