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(-) (limited to 'custom') 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 From 2d915b4b8d74664645d1bb063100390938111b09 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:50:52 +0100 Subject: ... --- custom/borgbackup.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 0db4b5da..73023999 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); - withSuffix = path: path + if cfg.snapshots == "btrfs" then config.services.btrfs-snapshots.mountSuffix else config.services.lvm-snapshots.mountSuffix; + 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; -- cgit v1.2.3 From 16907736b58f1edf298635b529588fbbcb974b04 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:52:00 +0100 Subject: ... --- custom/borgbackup.nix | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 73023999..fbe5bb4e 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -87,8 +87,9 @@ in { }; imports = - optional (cfg.snapshots == "lvm") ./lvm-snapshots.nix - ++ optional (cfg.snapshats == "btrfs") ./btrfs-snapshots.nix; + [ ./lvm-snapshots.nix + ./btrfs-snapshots.nix + ]; config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; -- cgit v1.2.3 From 3f6de9c937985478e1daf9017936d4a47ad6c47f Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:53:52 +0100 Subject: ... --- custom/borgbackup.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index fbe5bb4e..12f197a6 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -23,7 +23,7 @@ let 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; + mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else ""; targetOptions = { options = { @@ -163,7 +163,7 @@ in { serviceConfig = { Type = "oneshot"; - WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${mountPoint}/${withPrefix "%i"}"); + WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${withSuffix "%i"}"); Nice = 15; IOSchedulingClass = 2; IOSchedulingPriority = 7; -- cgit v1.2.3 From 3c72017e817ce77a2d7b6a6de7afbd43166da138 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:54:18 +0100 Subject: ... --- custom/btrfs-snapshots.nix | 6 ------ 1 file changed, 6 deletions(-) (limited to 'custom') diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix index 034b5019..6cc89da9 100644 --- a/custom/btrfs-snapshots.nix +++ b/custom/btrfs-snapshots.nix @@ -32,16 +32,10 @@ in { config = mkIf cfg.enable { - - system.activationScripts."btrfs-snapshots" = '' - mkdir -p ${cfg.mountPoint} - ''; - systemd.services."btrfs-snapshot@" = { enable = true; unitConfig = { - AssertPathIsDirectory = cfg.mountPoint; StopWhenUnneeded = !cfg.persist; }; -- cgit v1.2.3 From 03a8b2c710ea21dacda02f9894776272be70f670 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:56:19 +0100 Subject: ... --- custom/borgbackup.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 12f197a6..2c121fae 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -163,7 +163,7 @@ in { serviceConfig = { Type = "oneshot"; - WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${withSuffix "%i"}"); + WorkingDirectory = if (cfg.snapshots == null) then "%I" else withSuffix "%I"; Nice = 15; IOSchedulingClass = 2; IOSchedulingPriority = 7; -- cgit v1.2.3 From 6e36dda31362181007a21fcee8a61c9b7e163af0 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 18:59:22 +0100 Subject: ... --- custom/borgbackup.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 2c121fae..b292f6f2 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -163,7 +163,7 @@ in { serviceConfig = { Type = "oneshot"; - WorkingDirectory = if (cfg.snapshots == null) then "%I" else withSuffix "%I"; + WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${withSuffix "%f"}"); Nice = 15; IOSchedulingClass = 2; IOSchedulingPriority = 7; -- cgit v1.2.3 From 00df6a0d065afaf6ead86737aff7103d8c14de6d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 24 Nov 2019 19:00:09 +0100 Subject: ... --- custom/btrfs-snapshots.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'custom') diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix index 6cc89da9..96d2b2ba 100644 --- a/custom/btrfs-snapshots.nix +++ b/custom/btrfs-snapshots.nix @@ -41,10 +41,10 @@ in { serviceConfig = with pkgs; { Type = "oneshot"; - ExecStartPre = "-${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%i"}"; - ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%i"}"; + ExecStartPre = "-${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}"; + ExecStart = "${btrfs-progs}/bin/btrfs subvolume snapshot ${optionalString cfg.readOnly "-r"} %f ${snapshotMount "%f"}"; RemainAfterExit = true; - ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%i"}"; + ExecStop = "${btrfs-progs}/bin/btrfs subvolume delete -c ${snapshotMount "%f"}"; }; }; -- cgit v1.2.3