summary refs log tree commit diff
path: root/nixos/modules/services/backup/postgresql-backup.nix
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2021-06-04 17:34:26 +0200
committerRobert Hensing <robert@roberthensing.nl>2021-06-05 15:09:27 +0200
commit81c8189a841728a813bcde8604b80427fcf33522 (patch)
tree97bc681294e3cd43396fab963525ad6305cfa03c /nixos/modules/services/backup/postgresql-backup.nix
parentc586e42763e0f093d16b4b655759cb340171ad42 (diff)
downloadnixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar.gz
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar.bz2
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar.lz
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar.xz
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.tar.zst
nixpkgs-81c8189a841728a813bcde8604b80427fcf33522.zip
nixos/postgresqlBackup: Only replace backup when successful
Previously, a failed backup would always overwrite ${db}.sql.gz,
because the bash `>` redirect truncates the file; even if the
backup was going to fail.
On the next run, the ${db}.prev.sql.gz backup would be
overwritten by the bad ${db}.sql.gz.

Now, if the backup fails, the ${db}.in-progress.sql.gz is in an
unknown state, but ${db}.sql.gz will not be written.
On the next run, ${db}.prev.sql.gz (our only good backup) will
not be overwritten because ${db}.sql.gz does not exist.
Diffstat (limited to 'nixos/modules/services/backup/postgresql-backup.nix')
-rw-r--r--nixos/modules/services/backup/postgresql-backup.nix6
1 files changed, 5 insertions, 1 deletions
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 = {