snapshot /home also hourly

This commit is contained in:
Motiejus Jakštys 2024-07-29 16:31:32 +03:00
parent 7970c36801
commit 9e301158fa
2 changed files with 45 additions and 25 deletions

View File

@ -81,18 +81,20 @@ in
btrfssnapshot = { btrfssnapshot = {
enable = true; enable = true;
subvolumes = { subvolumes = [
"/home" = { {
subvolume = "/home";
label = "5minutely"; label = "5minutely";
keep = 12; keep = 12;
refreshInterval = "*:0/5"; refreshInterval = "*:0/5";
}; }
#"/home" = { {
# label = "hourly"; subvolume = "/home";
# keep = 24; label = "hourly";
# refreshInterval = "*:00:00"; keep = 24;
#}; refreshInterval = "*:00:00";
}; }
];
}; };
wifibackup = { wifibackup = {

View File

@ -6,6 +6,9 @@
}: }:
let let
cfg = config.mj.services.btrfssnapshot; cfg = config.mj.services.btrfssnapshot;
svcName =
subvol: label:
"btrfs-snapshot-${lib.strings.sanitizeDerivationName subvol}-${lib.strings.sanitizeDerivationName label}";
in in
{ {
options.mj.services.btrfssnapshot = { options.mj.services.btrfssnapshot = {
@ -15,8 +18,9 @@ in
default = { }; default = { };
type = type =
with lib.types; with lib.types;
attrsOf (submodule { listOf (submodule {
options = { options = {
subvolume = lib.mkOption { type = str; };
label = lib.mkOption { type = str; }; label = lib.mkOption { type = str; };
keep = lib.mkOption { type = int; }; keep = lib.mkOption { type = int; };
refreshInterval = lib.mkOption { type = str; }; refreshInterval = lib.mkOption { type = str; };
@ -27,22 +31,36 @@ in
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
systemd = { 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' ( timers = lib.listToAttrs (
subvolume: params: map (
lib.nameValuePair "btrfs-snapshot-${lib.strings.sanitizeDerivationName subvolume}" { params:
description = "${params.label} btrfs snapshot for ${subvolume}"; lib.nameValuePair (svcName params.subvolume params.label) {
description = "${params.label} btrfs snapshot for ${params.subvolume}";
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = params.refreshInterval; timerConfig.OnCalendar = params.refreshInterval;
} }
) cfg.subvolumes; ) 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
);
}; };
}; };
} }