From 9e301158fa6e1dffa288641c373eab486ba81e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 29 Jul 2024 16:31:32 +0300 Subject: [PATCH] snapshot /home also hourly --- hosts/mtworx/configuration.nix | 20 +++++---- modules/services/btrfssnapshot/default.nix | 50 +++++++++++++++------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/hosts/mtworx/configuration.nix b/hosts/mtworx/configuration.nix index 32ad30d..b612abc 100644 --- a/hosts/mtworx/configuration.nix +++ b/hosts/mtworx/configuration.nix @@ -81,18 +81,20 @@ in btrfssnapshot = { enable = true; - subvolumes = { - "/home" = { + subvolumes = [ + { + subvolume = "/home"; label = "5minutely"; keep = 12; refreshInterval = "*:0/5"; - }; - #"/home" = { - # label = "hourly"; - # keep = 24; - # refreshInterval = "*:00:00"; - #}; - }; + } + { + subvolume = "/home"; + label = "hourly"; + keep = 24; + refreshInterval = "*:00:00"; + } + ]; }; wifibackup = { diff --git a/modules/services/btrfssnapshot/default.nix b/modules/services/btrfssnapshot/default.nix index 8a888a4..b0f4969 100644 --- a/modules/services/btrfssnapshot/default.nix +++ b/modules/services/btrfssnapshot/default.nix @@ -6,6 +6,9 @@ }: let cfg = config.mj.services.btrfssnapshot; + svcName = + subvol: label: + "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvol}-${lib.strings.sanitizeDerivationName label}"; in { options.mj.services.btrfssnapshot = { @@ -15,8 +18,9 @@ in default = { }; type = with lib.types; - attrsOf (submodule { + listOf (submodule { options = { + subvolume = lib.mkOption { type = str; }; label = lib.mkOption { type = str; }; keep = lib.mkOption { type = int; }; refreshInterval = lib.mkOption { type = str; }; @@ -27,22 +31,36 @@ in config = lib.mkIf cfg.enable { systemd = { - services = lib.mapAttrs' ( - subvolume: params: - lib.nameValuePair "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" { - description = "${params.label} btrfs snapshot for ${subvolume} (keep ${builtins.toString params.keep})"; - serviceConfig.ExecStart = "${pkgs.btrfs-auto-snapshot}/bin/btrfs-auto-snapshot --verbose --label=${params.label} --keep=${builtins.toString params.keep} ${subvolume}"; - } - ) cfg.subvolumes; - timers = lib.mapAttrs' ( - subvolume: params: - lib.nameValuePair "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" { - description = "${params.label} btrfs snapshot for ${subvolume}"; - wantedBy = [ "timers.target" ]; - timerConfig.OnCalendar = params.refreshInterval; - } - ) cfg.subvolumes; + timers = lib.listToAttrs ( + map ( + params: + lib.nameValuePair (svcName params.subvolume params.label) { + description = "${params.label} btrfs snapshot for ${params.subvolume}"; + wantedBy = [ "timers.target" ]; + timerConfig.OnCalendar = params.refreshInterval; + } + ) cfg.subvolumes + ); + + services = lib.listToAttrs ( + map ( + params: + lib.nameValuePair (svcName params.subvolume params.label) { + description = "${params.label} btrfs snapshot for ${params.subvolume} (keep ${builtins.toString params.keep})"; + serviceConfig = { + Type = "oneshot"; + ExecStart = '' + ${pkgs.btrfs-auto-snapshot}/bin/btrfs-auto-snapshot \ + --verbose \ + --label=${params.label} \ + --keep=${builtins.toString params.keep} \ + ${params.subvolume}''; + }; + } + ) cfg.subvolumes + ); + }; }; }