diff options
| -rw-r--r-- | custom/borgbackup.nix | 101 | ||||
| -rw-r--r-- | custom/btrfs-snapshots.nix | 2 | ||||
| -rw-r--r-- | hel.nix | 54 |
3 files changed, 116 insertions, 41 deletions
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 @@ | |||
| 1 | { config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | cfg = config.services.borgbackup; | ||
| 7 | |||
| 8 | targetOptions = { | ||
| 9 | options = { | ||
| 10 | repo = mkOption { | ||
| 11 | type = types.str; | ||
| 12 | }; | ||
| 13 | |||
| 14 | paths = mkOption { | ||
| 15 | type = types.listOf types.path; | ||
| 16 | default = []; | ||
| 17 | }; | ||
| 18 | |||
| 19 | interval = mkOption { | ||
| 20 | type = types.str; | ||
| 21 | default = "6h"; | ||
| 22 | }; | ||
| 23 | |||
| 24 | lock = mkOption { | ||
| 25 | type = types.nullOr types.str; | ||
| 26 | default = "backup"; | ||
| 27 | }; | ||
| 28 | |||
| 29 | network = mkOption { | ||
| 30 | type = types.bool; | ||
| 31 | default = true; | ||
| 32 | }; | ||
| 33 | }; | ||
| 34 | }; | ||
| 35 | in { | ||
| 36 | options = { | ||
| 37 | services.borgbackup = { | ||
| 38 | snapshots = mkOption { | ||
| 39 | type = types.nullOr (types.enum ["btrfs"]); | ||
| 40 | default = null; | ||
| 41 | }; | ||
| 42 | |||
| 43 | targets = mkOption { | ||
| 44 | type = types.attrsOf (types.submodule targetOptions); | ||
| 45 | default = {}; | ||
| 46 | }; | ||
| 47 | |||
| 48 | prefix = mkOption { | ||
| 49 | type = types.str; | ||
| 50 | }; | ||
| 51 | }; | ||
| 52 | }; | ||
| 53 | |||
| 54 | config = mkIf (any (t: t.paths != []) cfg.targets) { | ||
| 55 | services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; | ||
| 56 | |||
| 57 | systemd.timers = listToAttrs (map (target: path: nameValuePair "borgbackup-${target}@${path}" { | ||
| 58 | wantedBy = [ "timers.target" ]; | ||
| 59 | |||
| 60 | timerConfig = { | ||
| 61 | Persistent = true; | ||
| 62 | OnUnitInactiveSec = "6h"; | ||
| 63 | }; | ||
| 64 | }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path; }) tCfg.paths;) cfg.targets))); | ||
| 65 | |||
| 66 | systemd.services = map (target: nameValuePair "borgbackup-${target}@" (let | ||
| 67 | deps = flatten [ | ||
| 68 | optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service" | ||
| 69 | optional network "network-online.target" | ||
| 70 | ]; | ||
| 71 | in { | ||
| 72 | bindsTo = deps; | ||
| 73 | after = deps; | ||
| 74 | |||
| 75 | path = with pkgs; [borgbackup]; | ||
| 76 | |||
| 77 | script = '' | ||
| 78 | borg create \ | ||
| 79 | --stats \ | ||
| 80 | --list \ | ||
| 81 | --filter 'AME' \ | ||
| 82 | --exclude-caches \ | ||
| 83 | --keep-exclude-tags \ | ||
| 84 | --patterns-from .backup \ | ||
| 85 | --one-file-system \ | ||
| 86 | --compression auto,lzma \ | ||
| 87 | ${target}::${prefix}$1-{utcnow} | ||
| 88 | ''; | ||
| 89 | scriptArgs = "%i"; | ||
| 90 | |||
| 91 | serviceConfig = { | ||
| 92 | Type = "oneshot"; | ||
| 93 | WorkingDirectory = if (cfg.snapshots == null) then "%p" else "/mnt/snapshot-%i"; | ||
| 94 | Nice = 15; | ||
| 95 | IOSchedulingClass = 2; | ||
| 96 | IOSchedulingPriority = 7; | ||
| 97 | SuccessExitStatus = [1 2]; | ||
| 98 | }; | ||
| 99 | })) (attrNames cfg.targets); | ||
| 100 | }; | ||
| 101 | }; | ||
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 { | |||
| 13 | enable = mkEnableOption "a systemd unit for btrfs snapshots"; | 13 | enable = mkEnableOption "a systemd unit for btrfs snapshots"; |
| 14 | 14 | ||
| 15 | mountPoint = mkOption { | 15 | mountPoint = mkOption { |
| 16 | readOnly = true; | ||
| 16 | type = types.path; | 17 | type = types.path; |
| 17 | default = "/mnt"; | 18 | default = "/mnt"; |
| 18 | }; | 19 | }; |
| 19 | 20 | ||
| 20 | mountPrefix = mkOption { | 21 | mountPrefix = mkOption { |
| 22 | readOnly = true; | ||
| 21 | type = types.str; | 23 | type = types.str; |
| 22 | default = "snapshot-"; | 24 | default = "snapshot-"; |
| 23 | }; | 25 | }; |
| @@ -15,6 +15,7 @@ | |||
| 15 | ./custom/tinc/yggdrasil.nix | 15 | ./custom/tinc/yggdrasil.nix |
| 16 | ./custom/uucp.nix | 16 | ./custom/uucp.nix |
| 17 | ./custom/btrfs-snapshots.nix | 17 | ./custom/btrfs-snapshots.nix |
| 18 | ./custom/borgbackup.nix | ||
| 18 | ]; | 19 | ]; |
| 19 | 20 | ||
| 20 | system.stateVersion = "16.09"; | 21 | system.stateVersion = "16.09"; |
| @@ -524,47 +525,18 @@ | |||
| 524 | 525 | ||
| 525 | systemd.services."NetworkManager-wait-online".enable = true; | 526 | systemd.services."NetworkManager-wait-online".enable = true; |
| 526 | 527 | ||
| 527 | services.btrfs-snapshots.enable = true; | 528 | services.backup = { |
| 528 | 529 | snapshots = "btrfs"; | |
| 529 | systemd.timers."backup-odin@home-gkleen" = { | 530 | prefix = "automatic.yggdrasil.midgard.hel."; |
| 530 | enable = true; | 531 | targets = { |
| 531 | 532 | "odin" = { | |
| 532 | wantedBy = [ "timers.target" ]; | 533 | repo = "borg.odin:/srv/backup/borg"; |
| 533 | 534 | paths = [ "home-gkleen" ]; | |
| 534 | timerConfig = { | 535 | }; |
| 535 | Persistent = true; | 536 | "munin" = { |
| 536 | OnUnitInactiveSec = "6h"; | 537 | repo = "borg.munin:borg"; |
| 537 | }; | 538 | paths = [ "home-gkleen" ]; |
| 538 | }; | 539 | }; |
| 539 | systemd.services."backup-odin@" = { | ||
| 540 | enable = true; | ||
| 541 | |||
| 542 | bindsTo = [ "btrfs-snapshot@%i.service" "network-online.target" ]; | ||
| 543 | after = [ "btrfs-snapshot@%i.service" "network-online.target" ]; | ||
| 544 | |||
| 545 | path = with pkgs; [borgbackup]; | ||
| 546 | |||
| 547 | script = '' | ||
| 548 | borg create \ | ||
| 549 | --stats \ | ||
| 550 | --list \ | ||
| 551 | --filter 'AME' \ | ||
| 552 | --exclude-caches \ | ||
| 553 | --keep-exclude-tags \ | ||
| 554 | --patterns-from .backup \ | ||
| 555 | --one-file-system \ | ||
| 556 | --compression auto,lzma \ | ||
| 557 | borg.odin:/srv/backup/borg::yggdrasil.midgard.hel.$1-{utcnow} | ||
| 558 | ''; | ||
| 559 | scriptArgs = "%i"; | ||
| 560 | |||
| 561 | serviceConfig = { | ||
| 562 | Type = "oneshot"; | ||
| 563 | WorkingDirectory = "/mnt/snapshot-%i"; | ||
| 564 | Nice = 15; | ||
| 565 | IOSchedulingClass = 2; | ||
| 566 | IOSchedulingPriority = 7; | ||
| 567 | SuccessExitStatus = [1 2]; | ||
| 568 | }; | 540 | }; |
| 569 | }; | 541 | }; |
| 570 | } | 542 | } |
