From 32282ae39d352428988891207fb4f276a311846a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 6 Feb 2022 21:20:24 +0100 Subject: vidhar: borg --- modules/borgbackup/btrfs-snapshots.nix | 52 ------ modules/borgbackup/default.nix | 206 ---------------------- modules/borgbackup/lvm-snapshots.nix | 133 -------------- modules/borgbackup/repokeys/borg_munin__borg.yaml | 33 ---- 4 files changed, 424 deletions(-) delete mode 100644 modules/borgbackup/btrfs-snapshots.nix delete mode 100644 modules/borgbackup/default.nix delete mode 100644 modules/borgbackup/lvm-snapshots.nix delete mode 100644 modules/borgbackup/repokeys/borg_munin__borg.yaml (limited to 'modules/borgbackup') diff --git a/modules/borgbackup/btrfs-snapshots.nix b/modules/borgbackup/btrfs-snapshots.nix deleted file mode 100644 index 96d2b2ba..00000000 --- a/modules/borgbackup/btrfs-snapshots.nix +++ /dev/null @@ -1,52 +0,0 @@ -{ config, lib, pkgs, ... }: - -with lib; - -let - cfg = config.services.btrfs-snapshots; - - snapshotMount = str: "${str}${cfg.mountSuffix}"; -in { - options = { - - services.btrfs-snapshots = { - enable = mkEnableOption "a systemd unit for btrfs snapshots"; - - mountSuffix = mkOption { - type = types.str; - default = ".snapshot"; - }; - - readOnly = mkOption { - type = types.bool; - default = true; - }; - - persist = mkOption { - type = types.bool; - default = false; - }; - }; - - }; - - - config = mkIf cfg.enable { - systemd.services."btrfs-snapshot@" = { - enable = true; - - unitConfig = { - StopWhenUnneeded = !cfg.persist; - }; - - serviceConfig = with pkgs; { - Type = "oneshot"; - 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 "%f"}"; - }; - }; - - }; -} diff --git a/modules/borgbackup/default.nix b/modules/borgbackup/default.nix deleted file mode 100644 index a0419d0e..00000000 --- a/modules/borgbackup/default.nix +++ /dev/null @@ -1,206 +0,0 @@ -{ config, lib, utils, pkgs, ... }: - -with utils; -with lib; - -let - cfg = config.services.borgbackup; - - lvmPath = { - options = { - LV = mkOption { - type = types.str; - }; - VG = mkOption { - type = types.str; - }; - }; - }; - - pathType = if cfg.snapshots == "lvm" then types.submodule lvmPath else types.path; - - 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); - - mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else ""; - - targetOptions = { - options = { - repo = mkOption { - type = types.str; - }; - - paths = mkOption { - type = types.listOf pathType; - default = []; - }; - - prune = mkOption { - type = types.attrsOf (types.listOf types.str); - default = {}; - }; - - interval = mkOption { - type = types.str; - default = "6h"; - }; - - jitter = mkOption { - type = with types; nullOr str; - default = "6h"; - }; - - lock = mkOption { - type = types.nullOr types.str; - default = "backup"; - }; - - network = mkOption { - type = types.bool; - default = true; - }; - - lockWait = mkOption { - type = types.int; - default = 600; - }; - - keyFile = mkOption { - type = types.nullOr types.path; - default = null; - }; - }; - }; -in { - disabledModules = [ "services/backup/borgbackup.nix" ]; - - options = { - services.borgbackup = { - snapshots = mkOption { - type = types.nullOr (types.enum ["btrfs" "lvm"]); - default = null; - }; - - targets = mkOption { - type = types.attrsOf (types.submodule targetOptions); - default = {}; - }; - - prefix = mkOption { - type = types.str; - }; - }; - }; - - imports = - [ ./lvm-snapshots.nix - ./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 = withSuffix (path.VG + "-" + path.LV); - }) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets))))); - - systemd.targets."timers-borg" = { - wantedBy = [ "timers.target" ]; - }; - - systemd.slices."system-borgbackup" = {}; - - systemd.timers = (listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${systemdPath path}" { - requiredBy = [ "timers-borg.target" ]; - - timerConfig = { - Persistent = false; - OnBootSec = tCfg.interval; - OnUnitActiveSec = tCfg.interval; - RandomizedDelaySec = mkIf (tCfg.jitter != null) tCfg.jitter; - }; - }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets)))) // (mapAttrs' (target: tCfg: nameValuePair "borgbackup-prune-${target}" { - enable = tCfg.prune != {}; - - requiredBy = [ "timers-borg.target" ]; - - timerConfig = { - Persistent = false; - OnBootSec = tCfg.interval; - OnUnitActiveSec = tCfg.interval; - RandomizedDelaySec = mkIf (tCfg.jitter != null) tCfg.jitter; - }; - }) cfg.targets); - - systemd.services = (mapAttrs' (target: tCfg: nameValuePair "borgbackup-${target}@" (let - deps = flatten [ - (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service") - (optional tCfg.network "network-online.target") - ]; - in { - bindsTo = deps; - after = deps; - - path = with pkgs; [borgbackup] ++ optional (tCfg.lock != null) utillinux; - - script = let - borgCmd = '' - borg create \ - --lock-wait ${toString tCfg.lockWait} \ - --stats \ - --list \ - --filter 'AME' \ - --exclude-caches \ - --keep-exclude-tags \ - --patterns-from .backup-${target} \ - --one-file-system \ - --compression auto,lzma \ - ${tCfg.repo}::${cfg.prefix}$1-{utcnow} - ''; - in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; - scriptArgs = if cfg.snapshots == "lvm" then "%I" else "%i"; - - unitConfig = { - AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; - DefaultDependencies = false; - 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}/${withSuffix "%I"}" else "${withSuffix "%f"}"); - Nice = 15; - IOSchedulingClass = 2; - IOSchedulingPriority = 7; - SuccessExitStatus = [1 2]; - Slice = "system-borgbackup.slice"; - Environment = lib.mkIf (tCfg.keyFile != null) "BORG_KEY_FILE=${tCfg.keyFile}"; - }; - })) cfg.targets) // (mapAttrs' (target: tCfg: nameValuePair "borgbackup-prune-${target}" { - enable = tCfg.prune != {}; - - bindsTo = ["network-online.target"]; - after = ["network-online.target"]; - - path = with pkgs; [borgbackup]; - - script = concatStringsSep "\n" (mapAttrsToList (path: args: '' - borg prune \ - --lock-wait ${toString tCfg.lockWait} \ - --list \ - --stats \ - --prefix ${escapeShellArg "${cfg.prefix}${path}"} \ - ${escapeShellArgs args} \ - ${tCfg.repo} - '') tCfg.prune); - - serviceConfig = { - Type = "oneshot"; - Slice = "system-borgbackup.slice"; - Environment = lib.mkIf (tCfg.keyFile != null) "BORG_KEY_FILE=${tCfg.keyFile}"; - }; - }) cfg.targets); - }; -} diff --git a/modules/borgbackup/lvm-snapshots.nix b/modules/borgbackup/lvm-snapshots.nix deleted file mode 100644 index 9b2a6562..00000000 --- a/modules/borgbackup/lvm-snapshots.nix +++ /dev/null @@ -1,133 +0,0 @@ -{ config, lib, utils, pkgs, ... }: - -with utils; -with lib; - -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}-${cfg.mountSuffix}"; - - snapshotConfig = { - options = { - LV = mkOption { - type = types.str; - }; - - VG = mkOption { - type = types.str; - }; - - mountName = mkOption { - type = types.nullOr types.str; - default = null; - }; - - cowSize = mkOption { - type = types.str; - default = "-l20%ORIGIN"; - }; - - readOnly = mkOption { - type = types.bool; - default = true; - }; - - persist = mkOption { - type = types.bool; - default = false; - }; - }; - }; -in { - options = { - - services.lvm-snapshots = { - snapshots = mkOption { - type = types.attrsOf (types.submodule snapshotConfig); - default = {}; - }; - - mountPoint = mkOption { - type = types.path; - default = "/mnt"; - }; - - mountSuffix = mkOption { - type = types.str; - default = "-snapshot"; - }; - }; - }; - - - config = mkIf (cfg != {}) { - - boot.kernelModules = [ "dm_snapshot" ]; - - # system.activationScripts = mapAttrs' (name: scfg: nameValuePair ("lvm-mountpoint" + name) '' - # mkdir -p ${snapshotMount name} - # '') cfg.snapshots; - - systemd.services = mapAttrs' (name: scfg: nameValuePair ("lvm-snapshot@" + escapeSystemdPath name) { - enable = true; - - description = "LVM-snapshot of ${scfg.VG}/${scfg.LV}"; - - bindsTo = ["${escapeSystemdPath "/dev/${scfg.VG}/${scfg.LV}"}.device"]; - after = ["${escapeSystemdPath "/dev/${scfg.VG}/${scfg.LV}"}.device"]; - - unitConfig = { - StopWhenUnneeded = !scfg.persist; - AssertPathIsDirectory = "/var/lock"; - }; - - path = with pkgs; [ devicemapper utillinux ]; - - script = '' - ( - flock -xn -E 4 9 - if [[ "$?" -ne 0 ]]; then - exit $? - fi - - lvcreate -s ${scfg.cowSize} --name ${snapshotName name} ${scfg.VG}/${scfg.LV} - - sleep infinity & - ) 9>/var/lock/lvm-snapshot.${scfg.VG} - ''; - - preStart = '' - lvremove -f ${scfg.VG}/${snapshotName name} - ''; - - preStop = '' - lvremove -f ${scfg.VG}/${snapshotName name} - ''; - - serviceConfig = with pkgs; { - Type = "forking"; - RestartForceExitStatus = [ "4" ]; - RestartSec = "5min"; - }; - }) cfg.snapshots; - - systemd.mounts = mapAttrsToList (name: scfg: { - enable = true; - - unitConfig = { - # AssertPathIsDirectory = snapshotMount name; - StopWhenUnneeded = !scfg.persist; - }; - - bindsTo = [ ("lvm-snapshot@" + escapeSystemdPath name + ".service") ]; - after = [ ("lvm-snapshot@" + escapeSystemdPath name + ".service") ]; - - options = concatStringsSep "," ([ "noauto" ] ++ optional scfg.readOnly "ro"); - - where = snapshotMount name; - what = "/dev/" + scfg.VG + "/" + snapshotName name; - }) cfg.snapshots; - }; -} diff --git a/modules/borgbackup/repokeys/borg_munin__borg.yaml b/modules/borgbackup/repokeys/borg_munin__borg.yaml deleted file mode 100644 index f302fe06..00000000 --- a/modules/borgbackup/repokeys/borg_munin__borg.yaml +++ /dev/null @@ -1,33 +0,0 @@ -key: ENC[AES256_GCM,data:mxh+Jtxx+HyD246yPwo0vy7vSTz3IG8VmfbxPMwqJRreh9ZwkGnH5aCTDOvWOHIrkmzaRMF3oCi1P8D29+abMUZdt0MuJ3UE6iL8+SXlflR+WACgALM2Df+x9B3BwQM3yeoCiWG+ebr0iQPHM3jqqpkjoRv1CcythxG2deZueur9lzgC2CwG1g3O8Prnl9z0JQGOa+gjic8Zwfn38B1BECeNPrbjzICGBOrSbN/6EnfBDygI2QzseamzK2I6R6jT+QxHvkl+Zi1m2TRB+4o82VgTjPhIReJyT7PrlDnUyrKObhCOlb3v+LiSdp16IPIDVs968kyDzgyi7QPOpGr+5tutWCZrau5xhPDrONKByl/0nVVwEZfRIYATvEXtn5okJru/mglcpeD0I7AtLt+Vfv9CB9pQczvkHo0cDtgudQDf9ADt/nkmqHugm5VfMg9m9aGbKqzXt6pPOMsXSbS43K7wgDaduLZ/PW4Ookx9gTNLtJHnZ64GBorOv4QSrZIZF8pE1FsQdUhmp/YzVhaNBnjCr+Jh77sYjoOwzF77Xy+VP2C/yVIf492P+FcgkSj6XhYYqHffpFW9l/xmUvyQF5gjj2k5T21UvgChhI1HeLPzQ7W9+xuGSMtg58aD/VPe1loCy8zLITNl71bneararRS5vItoZyzMdmIRMLAZD1klPmDNe1yufTpubOXzNYbWUqFUZtwH/mDL5GRZBD9dqs2b3F26c1CUyw==,iv:NJBHesKSZ1zuKk8qHnYKqIwMnFkH+rkQD1bam5XpLXU=,tag:EiYbIFY/r/eTSTJIhYV+GA==,type:str] -sops: - kms: [] - gcp_kms: [] - azure_kv: [] - hc_vault: [] - lastmodified: '2021-01-02T20:38:48Z' - mac: ENC[AES256_GCM,data:3rkFTOk3r2dx3hOqu1u7XIIibTDfqNlRcWY9X2N/LFa/BKojgDt5tcpbphV4HqWvl8nS+fPcVrIElJfQ/QGFEOx68G95BhByntT9+JhSbHJt73dGnCSroZCw5QefdydREGvA5n00Vo9yT9IMvQsQbmpRzo6hcrSSUvagZqmZckA=,iv:F/HllDzyxgulIWZbfz9bFKR+SFg4PoaUYZ5N5hfIzw0=,tag:h2NXmvj/thhBg1rIkwdXXA==,type:str] - pgp: - - created_at: '2021-01-02T20:38:09Z' - enc: | - -----BEGIN PGP MESSAGE----- - - hF4Dgwm4NZSaLAcSAQdAwmvyXlr9MyfPfLgkfQkoktKBV2WA2xhZrGL7NeeGfhAw - REk+clJ9WgiJ0iceRAONPnEjeiK0J6Fsj+5Ulq8flFGkoj5Pta0pm/9fudKmcPdC - 0l4BF0G5LSpG1EmY+LmVdSdas16rWgthnojoXPvbbHG6jZs3aDETshdiN8Bdlqsf - aVhq2LYzscnYezNcdernR4uojtiFny8qcmdF3tFacr+mkgfgIQr0W9yWFhDH15gm - =4TwU - -----END PGP MESSAGE----- - fp: F1AF20B9511B63F681A14E8D51AEFBCD1DEF68F8 - - created_at: '2021-01-02T20:38:09Z' - enc: | - -----BEGIN PGP MESSAGE----- - - hF4DXxoViZlp6dISAQdAruPXj9IsllEN7R5jk4gF7bW0ZirhvX7qsu22/6HbSw8w - 66RwN3WGjYO1CcVbHKuLqVVaUBCnrR/4XHN0JYUaqjubrSZBTWFKTBFsKSTT0LZq - 0l4BKcsXrbGpYC5+yQvg0RHJ7LplxpKOmqMY8KGckvGnVf2xg7k6wuWQREFzqwt+ - lOa3x+xFy9c0JwE8AafyKjb/cgqJiMb96lhsH57BpXJa2E39ImQbXqzDzdx2jEUt - =3rxi - -----END PGP MESSAGE----- - fp: 30D3453B8CD02FE2A3E7C78C0FB536FB87AE8F51 - unencrypted_suffix: _unencrypted - version: 3.6.1 -- cgit v1.2.3