diff --git a/nixos/modules/services/backup/postgresql-backup.nix b/nixos/modules/services/backup/postgresql-backup.nix
index 8857335a6e5..f658eb756f7 100644
--- a/nixos/modules/services/backup/postgresql-backup.nix
+++ b/nixos/modules/services/backup/postgresql-backup.nix
@@ -17,6 +17,8 @@ let
path = [ pkgs.coreutils pkgs.gzip config.services.postgresql.package ];
script = ''
+ set -e -o pipefail
+
umask 0077 # ensure backup is only readable by postgres user
if [ -e ${cfg.location}/${db}.sql.gz ]; then
@@ -24,7 +26,9 @@ let
fi
${dumpCmd} | \
- 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 = {
diff --git a/nixos/tests/postgresql.nix b/nixos/tests/postgresql.nix
index 091e64294ac..0369a070719 100644
--- a/nixos/tests/postgresql.nix
+++ b/nixos/tests/postgresql.nix
@@ -73,8 +73,30 @@ let
machine.succeed(
"systemctl start ${backupService}.service",
"zcat /var/backup/postgresql/${backupName}.sql.gz | grep 'ok'",
+ "ls -hal /var/backup/postgresql/ >/dev/console",
"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 'ok'",
+ "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 'ok'",
+ )
+
with subtest("Initdb works"):
machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2")