1af4f366ca
For large setups it is useful to list all databases explicit (for example if temporary databases are also present) and store them in extra files. For smaller setups it is more convenient to just backup all databases at once, because it is easy to forget to update configuration when adding/renaming databases. pg_dumpall also has the advantage that it backups users/passwords. As a result the module becomes easier to use because it is sufficient in the default case to just set one option (services.postgresqlBackup.enable).
79 lines
2.8 KiB
Nix
79 lines
2.8 KiB
Nix
{ system ? builtins.currentSystem,
|
|
config ? {},
|
|
pkgs ? import ../.. { inherit system config; }
|
|
}:
|
|
|
|
with import ../lib/testing.nix { inherit system pkgs; };
|
|
with pkgs.lib;
|
|
|
|
let
|
|
postgresql-versions = pkgs.callPackages ../../pkgs/servers/sql/postgresql { };
|
|
test-sql = pkgs.writeText "postgresql-test" ''
|
|
CREATE EXTENSION pgcrypto; -- just to check if lib loading works
|
|
CREATE TABLE sth (
|
|
id int
|
|
);
|
|
INSERT INTO sth (id) VALUES (1);
|
|
INSERT INTO sth (id) VALUES (1);
|
|
INSERT INTO sth (id) VALUES (1);
|
|
INSERT INTO sth (id) VALUES (1);
|
|
INSERT INTO sth (id) VALUES (1);
|
|
CREATE TABLE xmltest ( doc xml );
|
|
INSERT INTO xmltest (doc) VALUES ('<test>ok</test>'); -- check if libxml2 enabled
|
|
'';
|
|
make-postgresql-test = postgresql-name: postgresql-package: backup-all: makeTest {
|
|
name = postgresql-name;
|
|
meta = with pkgs.stdenv.lib.maintainers; {
|
|
maintainers = [ zagy ];
|
|
};
|
|
|
|
machine = {...}:
|
|
{
|
|
services.postgresql.package = postgresql-package;
|
|
services.postgresql.enable = true;
|
|
|
|
services.postgresqlBackup.enable = true;
|
|
services.postgresqlBackup.databases = optional (!backup-all) "postgres";
|
|
};
|
|
|
|
testScript = let
|
|
backupName = if backup-all then "all" else "postgres";
|
|
backupService = if backup-all then "postgresqlBackup" else "postgresqlBackup-postgres";
|
|
in ''
|
|
sub check_count {
|
|
my ($select, $nlines) = @_;
|
|
return 'test $(sudo -u postgres psql postgres -tAc "' . $select . '"|wc -l) -eq ' . $nlines;
|
|
}
|
|
|
|
$machine->start;
|
|
$machine->waitForUnit("postgresql");
|
|
# postgresql should be available just after unit start
|
|
$machine->succeed("cat ${test-sql} | sudo -u postgres psql");
|
|
$machine->shutdown; # make sure that postgresql survive restart (bug #1735)
|
|
sleep(2);
|
|
$machine->start;
|
|
$machine->waitForUnit("postgresql");
|
|
$machine->fail(check_count("SELECT * FROM sth;", 3));
|
|
$machine->succeed(check_count("SELECT * FROM sth;", 5));
|
|
$machine->fail(check_count("SELECT * FROM sth;", 4));
|
|
$machine->succeed(check_count("SELECT xpath(\'/test/text()\', doc) FROM xmltest;", 1));
|
|
|
|
# Check backup service
|
|
$machine->succeed("systemctl start ${backupService}.service");
|
|
$machine->succeed("zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'");
|
|
$machine->succeed("stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600");
|
|
$machine->shutdown;
|
|
'';
|
|
|
|
};
|
|
in
|
|
(mapAttrs' (name: package: { inherit name; value=make-postgresql-test name package false;}) postgresql-versions) // (
|
|
# just pick one version for the dump all test
|
|
let
|
|
first = head (attrNames postgresql-versions);
|
|
name = "${first}-backup-all";
|
|
in {
|
|
${name} = make-postgresql-test name postgresql-versions.${first} true;
|
|
}
|
|
)
|