summaryrefslogtreecommitdiff
path: root/os/srv/backup.nix
blob: 3d1d583fccb07ffb5f86f2455ca8893ff81bb605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.os.srv.replication;
  pgLockScript = pkgs.writeScriptBin "pg-lock" ''
    #!/bin/sh
    microvm -s database-vm -- sudo -u postgres psql -c "SELECT pg_backup_start('sanoid_snap');"
  '';

  pgUnlockScript = pkgs.writeScriptBin "pg-unlock" ''
    #!/bin/sh
    microvm -s database-vm -- sudo -u postgres psql -c "SELECT pg_backup_stop();"
  '';
in
{
  options.os.srv.replication.enable = lib.mkEnableOption "enables replications";
  config = lib.mkIf cfg.enable {
    services = {
      sanoid = {
        enable = true;
        templates.production = {
          autosnap = true;
          autoprune = true;
          hourly = 24;
          daily = 7;
          weekly = 4;
          monthly = 3;
        };
        datasets = {
          "zroot/rpool/appdata/db/postgres" = {
            useTemplate = [ "production" ];

            pre_snapshot_script = "${pgLockScript}/bin/pg-lock";
            post_snapshot_script = "${pgUnlockScript}/bin/pg-unlock";
            no_inconsistent_snapshot = true;
            force_post_snapshot_script = true;
            script_timeout = 30;
          };

          "zroot/rpool/appdata/db/redis".useTemplate = [ "production" ];

          "zroot/rpool/appdata/db/couchdb".useTemplate = [ "production" ];

          "zroot/rpool/appdata/cfg".useTemplate = [ "production" ];

          "zroot/rpool/appdata/games".useTemplate = [ "production" ];

          "zroot/rpool/appdata/mail".useTemplate = [ "production" ];

          "zroot/rpool/containers".useTemplate = [ "production" ];
        };
      };

      syncoid = {
        enable = true;
        commonArgs = [
          "-w"
          "--delete-target-snapshots"
          "--use-hold"
          "--no-sync-snap"
        ];
        commands = {
          "sync-databases" = {
            source = "zroot/rpool/appdata/db";
            target = "tank/ztank/backup/nvme/db";
            recursive = true;
          };

          "sync-config" = {
            source = "zroot/rpool/appdata/cfg";
            target = "tank/ztank/backup/nvme/cfg";
          };

          "sync-games" = {
            source = "zroot/rpool/appdata/games";
            target = "tank/ztank/backup/nvme/games";
          };

          "sync-mail" = {
            source = "zroot/rpool/appdata/mail";
            target = "tank/ztank/backup/nvme/mail";
          };

          "sync-docker" = {
            source = "zroot/rpool/containers";
            target = "tank/ztank/backup/nvme/containers";
          };
        };
      };
    };
  };
}