Merge pull request #125696 from hercules-ci/postgresql-backup-only-replace-if-successful

nixos/postgresqlBackup: only replace if successful
This commit is contained in:
Mario Rodas 2021-07-07 18:41:57 -05:00 committed by GitHub
commit bb1ff7da54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -14,15 +14,21 @@ let
requires = [ "postgresql.service" ]; requires = [ "postgresql.service" ];
path = [ pkgs.coreutils pkgs.gzip config.services.postgresql.package ];
script = '' script = ''
set -e -o pipefail
umask 0077 # ensure backup is only readable by postgres user umask 0077 # ensure backup is only readable by postgres user
if [ -e ${cfg.location}/${db}.sql.gz ]; then if [ -e ${cfg.location}/${db}.sql.gz ]; then
${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
fi fi
${dumpCmd} | \ ${dumpCmd} | \
${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz gzip -c > ${cfg.location}/${db}.in-progress.sql.gz
mv ${cfg.location}/${db}.in-progress.sql.gz ${cfg.location}/${db}.sql.gz
''; '';
serviceConfig = { serviceConfig = {
@ -113,12 +119,12 @@ in {
}) })
(mkIf (cfg.enable && cfg.backupAll) { (mkIf (cfg.enable && cfg.backupAll) {
systemd.services.postgresqlBackup = systemd.services.postgresqlBackup =
postgresqlBackupService "all" "${config.services.postgresql.package}/bin/pg_dumpall"; postgresqlBackupService "all" "pg_dumpall";
}) })
(mkIf (cfg.enable && !cfg.backupAll) { (mkIf (cfg.enable && !cfg.backupAll) {
systemd.services = listToAttrs (map (db: systemd.services = listToAttrs (map (db:
let let
cmd = "${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db}"; cmd = "pg_dump ${cfg.pgdumpOptions} ${db}";
in { in {
name = "postgresqlBackup-${db}"; name = "postgresqlBackup-${db}";
value = postgresqlBackupService db cmd; value = postgresqlBackupService db cmd;

View File

@ -73,8 +73,30 @@ let
machine.succeed( machine.succeed(
"systemctl start ${backupService}.service", "systemctl start ${backupService}.service",
"zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'", "zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'",
"ls -hal /var/backup/postgresql/ >/dev/console",
"stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600", "stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600",
) )
with subtest("Backup service fails gracefully"):
# Sabotage the backup process
machine.succeed("rm /run/postgresql/.s.PGSQL.5432")
machine.fail(
"systemctl start ${backupService}.service",
)
machine.succeed(
"ls -hal /var/backup/postgresql/ >/dev/console",
"zcat /var/backup/postgresql/${backupName}.prev.sql.gz | grep '<test>ok</test>'",
"stat /var/backup/postgresql/${backupName}.in-progress.sql.gz",
)
# In a previous version, the second run would overwrite prev.sql.gz,
# so we test a second run as well.
machine.fail(
"systemctl start ${backupService}.service",
)
machine.succeed(
"stat /var/backup/postgresql/${backupName}.in-progress.sql.gz",
"zcat /var/backup/postgresql/${backupName}.prev.sql.gz | grep '<test>ok</test>'",
)
with subtest("Initdb works"): with subtest("Initdb works"):
machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2") machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2")