From 33a25859498ac876d5dcc55231e1a3fc3580a7de Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 17 Dec 2017 04:08:47 +0100 Subject: =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom/borgbackup.nix | 101 +++++++++++++++++++++++++++++++++++++++++++++ custom/btrfs-snapshots.nix | 2 + 2 files changed, 103 insertions(+) create mode 100644 custom/borgbackup.nix (limited to 'custom') diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix new file mode 100644 index 00000000..49da1975 --- /dev/null +++ b/custom/borgbackup.nix @@ -0,0 +1,101 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.borgbackup; + + targetOptions = { + options = { + repo = mkOption { + type = types.str; + }; + + paths = mkOption { + type = types.listOf types.path; + default = []; + }; + + interval = mkOption { + type = types.str; + default = "6h"; + }; + + lock = mkOption { + type = types.nullOr types.str; + default = "backup"; + }; + + network = mkOption { + type = types.bool; + default = true; + }; + }; + }; +in { + options = { + services.borgbackup = { + snapshots = mkOption { + type = types.nullOr (types.enum ["btrfs"]); + default = null; + }; + + targets = mkOption { + type = types.attrsOf (types.submodule targetOptions); + default = {}; + }; + + prefix = mkOption { + type = types.str; + }; + }; + }; + + config = mkIf (any (t: t.paths != []) cfg.targets) { + services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; + + systemd.timers = listToAttrs (map (target: path: nameValuePair "borgbackup-${target}@${path}" { + wantedBy = [ "timers.target" ]; + + timerConfig = { + Persistent = true; + OnUnitInactiveSec = "6h"; + }; + }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path; }) tCfg.paths;) cfg.targets))); + + systemd.services = map (target: nameValuePair "borgbackup-${target}@" (let + deps = flatten [ + optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service" + optional network "network-online.target" + ]; + in { + bindsTo = deps; + after = deps; + + path = with pkgs; [borgbackup]; + + script = '' + borg create \ + --stats \ + --list \ + --filter 'AME' \ + --exclude-caches \ + --keep-exclude-tags \ + --patterns-from .backup \ + --one-file-system \ + --compression auto,lzma \ + ${target}::${prefix}$1-{utcnow} + ''; + scriptArgs = "%i"; + + serviceConfig = { + Type = "oneshot"; + WorkingDirectory = if (cfg.snapshots == null) then "%p" else "/mnt/snapshot-%i"; + Nice = 15; + IOSchedulingClass = 2; + IOSchedulingPriority = 7; + SuccessExitStatus = [1 2]; + }; + })) (attrNames cfg.targets); + }; +}; diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix index 03b17d23..7114e9d9 100644 --- a/custom/btrfs-snapshots.nix +++ b/custom/btrfs-snapshots.nix @@ -13,11 +13,13 @@ in { enable = mkEnableOption "a systemd unit for btrfs snapshots"; mountPoint = mkOption { + readOnly = true; type = types.path; default = "/mnt"; }; mountPrefix = mkOption { + readOnly = true; type = types.str; default = "snapshot-"; }; -- cgit v1.2.3