summaryrefslogtreecommitdiffstats
path: root/nixos/tests/pgbackrest/posix.nix
blob: fd75cf45d03a004a841e702d412325dd6f14aa02 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{ lib, pkgs, ... }:
let
  inherit (import ../ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
  backupPath = "/var/lib/pgbackrest";
in
{
  name = "pgbackrest-posix";

  nodes.primary =
    {
      pkgs,
      ...
    }:
    {
      services.openssh.enable = true;
      users.users.postgres.openssh.authorizedKeys.keys = [
        snakeOilPublicKey
      ];

      services.postgresql = {
        enable = true;
        initialScript = pkgs.writeText "init.sql" ''
          CREATE TABLE t(c text);
          INSERT INTO t VALUES ('hello world');
        '';
        # To make sure we're waiting for read-write after recovery.
        ensureUsers = [
          {
            name = "app-user";
            ensureClauses.login = true;
          }
        ];
      };

      services.pgbackrest = {
        enable = true;
        repos.backup = {
          type = "posix";
          path = backupPath;
          host-user = "pgbackrest";
        };
      };
    };

  nodes.backup =
    {
      nodes,
      ...
    }:
    {
      services.openssh.enable = true;
      users.users.pgbackrest.openssh.authorizedKeys.keys = [
        snakeOilPublicKey
      ];

      services.pgbackrest = {
        enable = true;
        repos.localhost.path = backupPath;

        stanzas.default = {
          jobs.future = {
            schedule = "3000-01-01";
            type = "full";
          };
          instances.primary = {
            path = nodes.primary.services.postgresql.dataDir;
            user = "postgres";
          };
        };

        # Examples from https://pgbackrest.org/configuration.html#introduction
        # Not used for the test, except for dumping the config.
        stanzas.config-format.settings = {
          start-fast = true;
          compress-level = 3;
          buffer-size = "2MiB";
          db-timeout = 600;
          db-exclude = [
            "db1"
            "db2"
            "db5"
          ];
          tablespace-map = {
            ts_01 = "/db/ts_01";
            ts_02 = "/db/ts_02";
          };
        };
      };
    };

  testScript =
    { nodes, ... }:
    ''
      start_all()

      primary.wait_for_unit("multi-user.target")
      backup.wait_for_unit("multi-user.target")

      with subtest("config file is written correctly"):
        from textwrap import dedent
        have = backup.succeed("cat /etc/pgbackrest/pgbackrest.conf")
        want = dedent("""\
          [config-format]
          buffer-size=2MiB
          compress-level=3
          db-exclude=db1
          db-exclude=db2
          db-exclude=db5
          db-timeout=600
          start-fast=y
          tablespace-map=ts_01=/db/ts_01
          tablespace-map=ts_02=/db/ts_02
        """)
        assert want in have, repr((want, have))

      primary.log(primary.succeed("""
        HOME="${nodes.primary.services.postgresql.dataDir}"
        mkdir -m 700 -p ~/.ssh
        cat ${snakeOilPrivateKey} > ~/.ssh/id_ecdsa
        chmod 400 ~/.ssh/id_ecdsa
        ssh-keyscan backup >> ~/.ssh/known_hosts
        chown -R postgres:postgres ~/.ssh
      """))

      backup.log(backup.succeed("""
        HOME="${backupPath}"
        mkdir -m 700 -p ~/.ssh
        cat ${snakeOilPrivateKey} > ~/.ssh/id_ecdsa
        chmod 400 ~/.ssh/id_ecdsa
        ssh-keyscan primary >> ~/.ssh/known_hosts
        chown -R pgbackrest:pgbackrest ~
      """))

      with subtest("backup/restore works with remote instance/local repo (SSH)"):
        backup.succeed("sudo -u pgbackrest pgbackrest --stanza=default stanza-create")
        backup.succeed("sudo -u pgbackrest pgbackrest --stanza=default check")

        backup.systemctl("start pgbackrest-default-future")

        # corrupt cluster
        primary.systemctl("stop postgresql")
        primary.execute("rm ${nodes.primary.services.postgresql.dataDir}/global/pg_control")

        primary.succeed("sudo -u postgres pgbackrest --stanza=default restore --delta")

        primary.systemctl("start postgresql")
        primary.wait_for_unit("postgresql.target")
        assert "hello world" in primary.succeed("sudo -u postgres psql -c 'TABLE t;'")
    '';
}