Merge pull request #54310 from Mic92/postgresq-backup

nixos/postgresqlBackup: add backupAll option
This commit is contained in:
Silvan Mosberger 2019-01-23 21:40:39 +01:00 committed by GitHub
commit d9f39b7252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 19 deletions

View File

@ -6,11 +6,11 @@ let
cfg = config.services.postgresqlBackup; cfg = config.services.postgresqlBackup;
postgresqlBackupService = db : postgresqlBackupService = db: dumpCmd:
{ {
enable = true; enable = true;
description = "Backup of database ${db}"; description = "Backup of ${db} database(s)";
requires = [ "postgresql.service" ]; requires = [ "postgresql.service" ];
@ -26,7 +26,7 @@ let
${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz ${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
fi fi
${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db} | \ ${dumpCmd} | \
${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz ${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz
''; '';
@ -42,9 +42,7 @@ let
in { in {
options = { options = {
services.postgresqlBackup = { services.postgresqlBackup = {
enable = mkOption { enable = mkOption {
default = false; default = false;
description = '' description = ''
@ -61,6 +59,19 @@ in {
''; '';
}; };
backupAll = mkOption {
default = cfg.databases == [];
defaultText = "services.postgresqlBackup.databases == []";
type = lib.types.bool;
description = ''
Backup all databases using pg_dumpall.
This option is mutual exclusive to
<literal>services.postgresqlBackup.databases</literal>.
The resulting backup dump will have the name all.sql.gz.
This option is the default if no databases are specified.
'';
};
databases = mkOption { databases = mkOption {
default = []; default = [];
description = '' description = ''
@ -79,18 +90,36 @@ in {
type = types.string; type = types.string;
default = "-Cbo"; default = "-Cbo";
description = '' description = ''
Command line options for pg_dump. Command line options for pg_dump. This options is not used
if <literal>config.services.postgresqlBackup.backupAll</literal> is enabled.
Note that config.services.postgresqlBackup.backupAll is also active,
when no databases where specified.
''; '';
}; };
}; };
}; };
config = mkIf config.services.postgresqlBackup.enable { config = mkMerge [
{
systemd.services = listToAttrs (map (db : { assertions = [{
assertion = cfg.backupAll -> cfg.databases == [];
message = "config.services.postgresqlBackup.backupAll cannot be used together with config.services.postgresqlBackup.databases";
}];
}
(mkIf (cfg.enable && cfg.backupAll) {
systemd.services.postgresqlBackup =
postgresqlBackupService "all" "${config.services.postgresql.package}/bin/pg_dumpall";
})
(mkIf (cfg.enable && !cfg.backupAll) {
systemd.services = listToAttrs (map (db:
let
cmd = "${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db}";
in {
name = "postgresqlBackup-${db}"; name = "postgresqlBackup-${db}";
value = postgresqlBackupService db; } ) cfg.databases); value = postgresqlBackupService db cmd;
}; }) cfg.databases);
})
];
} }

View File

@ -21,7 +21,7 @@ let
CREATE TABLE xmltest ( doc xml ); CREATE TABLE xmltest ( doc xml );
INSERT INTO xmltest (doc) VALUES ('<test>ok</test>'); -- check if libxml2 enabled INSERT INTO xmltest (doc) VALUES ('<test>ok</test>'); -- check if libxml2 enabled
''; '';
make-postgresql-test = postgresql-name: postgresql-package: makeTest { make-postgresql-test = postgresql-name: postgresql-package: backup-all: makeTest {
name = postgresql-name; name = postgresql-name;
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ zagy ]; maintainers = [ zagy ];
@ -29,14 +29,17 @@ let
machine = {...}: machine = {...}:
{ {
services.postgresql.package=postgresql-package; services.postgresql.package = postgresql-package;
services.postgresql.enable = true; services.postgresql.enable = true;
services.postgresqlBackup.enable = true; services.postgresqlBackup.enable = true;
services.postgresqlBackup.databases = [ "postgres" ]; services.postgresqlBackup.databases = optional (!backup-all) "postgres";
}; };
testScript = '' testScript = let
backupName = if backup-all then "all" else "postgres";
backupService = if backup-all then "postgresqlBackup" else "postgresqlBackup-postgres";
in ''
sub check_count { sub check_count {
my ($select, $nlines) = @_; my ($select, $nlines) = @_;
return 'test $(sudo -u postgres psql postgres -tAc "' . $select . '"|wc -l) -eq ' . $nlines; return 'test $(sudo -u postgres psql postgres -tAc "' . $select . '"|wc -l) -eq ' . $nlines;
@ -56,12 +59,20 @@ let
$machine->succeed(check_count("SELECT xpath(\'/test/text()\', doc) FROM xmltest;", 1)); $machine->succeed(check_count("SELECT xpath(\'/test/text()\', doc) FROM xmltest;", 1));
# Check backup service # Check backup service
$machine->succeed("systemctl start postgresqlBackup-postgres.service"); $machine->succeed("systemctl start ${backupService}.service");
$machine->succeed("zcat /var/backup/postgresql/postgres.sql.gz | grep '<test>ok</test>'"); $machine->succeed("zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'");
$machine->succeed("stat -c '%a' /var/backup/postgresql/postgres.sql.gz | grep 600"); $machine->succeed("stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600");
$machine->shutdown; $machine->shutdown;
''; '';
}; };
in in
mapAttrs' (p-name: p-package: {name=p-name; value=make-postgresql-test p-name p-package;}) postgresql-versions (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;
}
)