Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

5.5 KB
Raw Blame

1. Reproduce the unclean stop

  • 1.1 Add a clean-shutdown case to e2e/docker-storage/run.sh and to its all list. Add a helper that reads Database cluster state with pg_controldata from a helper container on a stopped volume. The first part of the case starts the candidate on a named volume and waits for health. It stops the container with docker stop and the default timeout, then asserts three things: the stop took less than 10 seconds, the exit status is 0 and the cluster state is shut down. Verify that docker build --tag databasus-storage-test:local . succeeds and that CASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh fails, because the current image leaves the state in production.
  • 1.2 Add a startup-stop case and add it to the all list. Each of its two parts starts the candidate on a fresh volume and runs a docker exec loop inside the container. The loop waits for the target process and sends it SIGSTOP. In the first part the target is a postgres --single process. In the second part it is the postmaster of the regular server, a postgres process without --single whose PID matches postmaster.pid. With the target frozen, the case starts docker stop in the background. In the first part only, it asserts that Stop requested during startup appears in the log within 2 seconds while the target is still stopped. In the second part, a foreground pg_isready or psql call can wait on the frozen postmaster, so the case sends SIGCONT without that assertion. In both parts, the case then sends SIGCONT to the target where not yet sent and asserts that the container exits with status 143, which rules out a kill by the stop timeout, and that the log has no Starting Databasus application... line. In the second part it also asserts that the cluster state is shut down. Verify that CASE=startup-stop DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh fails against the current image.

2. Supervise the services

  • 2.1 In docker/start.sh, split main into a root phase and a runtime phase under databasus, following design decision 1. The root phase ends with exec gosu databasus /app/start.sh and a phase argument, and the PostgreSQL steps drop their gosu databasus prefix. Install a TERM and INT trap at the start of each phase, following design decision 4. Before the application starts, the handler logs Stop requested during startup and stops the PostgreSQL process running at that moment. The regular server receives SIGINT for a fast shutdown, a single-user step SIGTERM. The handler waits in a loop while kill -0 succeeds and then exits with 143 or 130. Run the single-user steps in the background, followed by wait on their process ID. Verify that docker build --tag databasus-storage-test:local . succeeds and that CASE=startup-stop DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh passes.
  • 2.2 Replace exec gosu databasus ./main with a supervision function that follows design decisions 1, 2, 3 and 5. It starts the application in the background, waits on both process IDs with wait -n and forwards a stop signal to the application. It repeats wait -n -p until Bash names the process that exited, collects every status with || status=$? and logs Databasus application exited with status <n>. Afterwards it sends the postmaster SIGINT for a fast shutdown, waits for it to exit and exits with the application's status. When PostgreSQL ends first, it stops the application and exits with 1. Verify that docker build --tag databasus-storage-test:local . succeeds and that CASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh passes.

3. Cover the remaining stop paths

  • 3.1 Extend the first part of clean-shutdown. Assert that Databasus application exited with status 0 comes before PostgreSQL's received fast shutdown request line in docker logs. Verify that CASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh passes.

  • 3.2 Add three parts to clean-shutdown, each on a fresh volume.

    • Interrupt: send docker kill --signal INT to a healthy container. Assert that it exits within 10 seconds and that the cluster state is shut down.
    • Application exit: send SIGTERM to the application process alone with docker exec <container> /bin/bash -c 'kill -TERM <pid of ./main>', because the image ships no kill executable. Assert that the container exits with 0 and that the cluster state is shut down.
    • PostgreSQL exit: send SIGKILL to the postmaster from head -1 /databasus-data/pgdata/postmaster.pid. Assert that the container exits with a non-zero status, that the log has the supervisor's ERROR: PostgreSQL exited with status line and that it lacks the Databasus application exited with status line of a normal application exit.

    Verify that CASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh passes.

  • 3.3 Verify that the bind-root, upgrades and postgresql-recovery cases still pass one at a time through CASE=<case> DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.sh.

4. Specs and review

  • 4.1 Verify that openspec validate stop-embedded-postgresql-cleanly --strict passes.
  • 4.2 Run the mandatory full implementation review from AGENTS.md and resolve its blocking findings. Then rebuild the image and verify that the clean-shutdown and startup-stop cases pass.