git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #125696 from hercules-ci/postgresql-backup-only-replace-if-successful

nixos/postgresqlBackup: only replace if successful

commitbb1ff7da54b22be0b314c4dffb56212e9678e7db authorMario Rodas <marsam@users.noreply.github.com> dateWed 7 Jul 2021, 18:41 (5 years ago) parent596f82581c8189
2 files changed+32−4
nixos/modules/services/backup/postgresql-backup.nix+10−4 nixos/tests/postgresql.nix+22
nixos/modules/services/backup/postgresql-backup.nix+10−4
@@ -14,15 +14,21 @@ let
1414
1515 requires = [ "postgresql.service" ];
1616
17+ path = [ pkgs.coreutils pkgs.gzip config.services.postgresql.package ];
18+
1719 script = ''
20+ set -e -o pipefail
21+
1822 umask 0077 # ensure backup is only readable by postgres user
1923
2024 if [ -e ${cfg.location}/${db}.sql.gz ]; then
21− ${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
25+ mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
2226 fi
2327
2428 ${dumpCmd} | \
25− ${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz
29+ gzip -c > ${cfg.location}/${db}.in-progress.sql.gz
30+
31+ mv ${cfg.location}/${db}.in-progress.sql.gz ${cfg.location}/${db}.sql.gz
2632 '';
2733
2834 serviceConfig = {
@@ -113,12 +119,12 @@ in {
113119 })
114120 (mkIf (cfg.enable && cfg.backupAll) {
115121 systemd.services.postgresqlBackup =
116− postgresqlBackupService "all" "${config.services.postgresql.package}/bin/pg_dumpall";
122+ postgresqlBackupService "all" "pg_dumpall";
117123 })
118124 (mkIf (cfg.enable && !cfg.backupAll) {
119125 systemd.services = listToAttrs (map (db:
120126 let
121− cmd = "${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db}";
127+ cmd = "pg_dump ${cfg.pgdumpOptions} ${db}";
122128 in {
123129 name = "postgresqlBackup-${db}";
124130 value = postgresqlBackupService db cmd;
nixos/tests/postgresql.nix+22
@@ -73,8 +73,30 @@ let
7373 machine.succeed(
7474 "systemctl start ${backupService}.service",
7575 "zcat /var/backup/postgresql/${backupName}.sql.gz | grep '<test>ok</test>'",
76+ "ls -hal /var/backup/postgresql/ >/dev/console",
7677 "stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600",
7778 )
79+ with subtest("Backup service fails gracefully"):
80+ # Sabotage the backup process
81+ machine.succeed("rm /run/postgresql/.s.PGSQL.5432")
82+ machine.fail(
83+ "systemctl start ${backupService}.service",
84+ )
85+ machine.succeed(
86+ "ls -hal /var/backup/postgresql/ >/dev/console",
87+ "zcat /var/backup/postgresql/${backupName}.prev.sql.gz | grep '<test>ok</test>'",
88+ "stat /var/backup/postgresql/${backupName}.in-progress.sql.gz",
89+ )
90+ # In a previous version, the second run would overwrite prev.sql.gz,
91+ # so we test a second run as well.
92+ machine.fail(
93+ "systemctl start ${backupService}.service",
94+ )
95+ machine.succeed(
96+ "stat /var/backup/postgresql/${backupName}.in-progress.sql.gz",
97+ "zcat /var/backup/postgresql/${backupName}.prev.sql.gz | grep '<test>ok</test>'",
98+ )
99+
78100
79101 with subtest("Initdb works"):
80102 machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2")