try to improve database dump logic on container shutdown

Signed-off-by: Simon L <szaimen@e.mail.de>
This commit is contained in:
Simon L 2023-12-12 14:22:33 +01:00
parent c1ef319b87
commit 312f238bf6

View file

@ -167,25 +167,29 @@ if [ -f "/var/lib/postgresql/data/postgresql.conf" ]; then
fi fi
fi fi
# Catch docker stop attempts do_database_dump() {
trap 'true' SIGINT SIGTERM set -x
rm -f "$DUMP_FILE.temp"
# Start the database touch "$DUMP_DIR/export.failed"
exec docker-entrypoint.sh postgres & if pg_dump --username "$POSTGRES_USER" "$POSTGRES_DB" > "$DUMP_FILE.temp"; then
wait $!
# Continue with shutdown procedure: do database dump, etc.
rm -f "$DUMP_FILE.temp"
touch "$DUMP_DIR/export.failed"
if pg_dump --username "$POSTGRES_USER" "$POSTGRES_DB" > "$DUMP_FILE.temp"; then
rm -f "$DUMP_FILE" rm -f "$DUMP_FILE"
mv "$DUMP_FILE.temp" "$DUMP_FILE" mv "$DUMP_FILE.temp" "$DUMP_FILE"
pg_ctl stop -m fast pg_ctl stop -m fast
rm "$DUMP_DIR/export.failed" rm "$DUMP_DIR/export.failed"
echo 'Database dump successful!' echo 'Database dump successful!'
set +x
exit 0 exit 0
else else
pg_ctl stop -m fast pg_ctl stop -m fast
echo "Database dump unsuccessful!" echo "Database dump unsuccessful!"
set +x
exit 1 exit 1
fi fi
}
# Catch docker stop attempts
trap do_database_dump SIGINT SIGTERM
# Start the database
exec docker-entrypoint.sh postgres &
wait $!