PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
1. Reproduce the unclean stop
- 1.1 Add a
clean-shutdowncase toe2e/docker-storage/run.shand to itsalllist. Add a helper that readsDatabase cluster statewithpg_controldatafrom 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 withdocker stopand the default timeout, then asserts three things: the stop took less than 10 seconds, the exit status is0and the cluster state isshut down. Verify thatdocker build --tag databasus-storage-test:local .succeeds and thatCASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shfails, because the current image leaves the statein production. - 1.2 Add a
startup-stopcase and add it to thealllist. Each of its two parts starts the candidate on a fresh volume and runs adocker execloop inside the container. The loop waits for the target process and sends itSIGSTOP. In the first part the target is apostgres --singleprocess. In the second part it is the postmaster of the regular server, apostgresprocess without--singlewhose PID matchespostmaster.pid. With the target frozen, the case startsdocker stopin the background. In the first part only, it asserts thatStop requested during startupappears in the log within 2 seconds while the target is still stopped. In the second part, a foregroundpg_isreadyorpsqlcall can wait on the frozen postmaster, so the case sendsSIGCONTwithout that assertion. In both parts, the case then sendsSIGCONTto the target where not yet sent and asserts that the container exits with status143, which rules out a kill by the stop timeout, and that the log has noStarting Databasus application...line. In the second part it also asserts that the cluster state isshut down. Verify thatCASE=startup-stop DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shfails against the current image.
2. Supervise the services
- 2.1 In
docker/start.sh, splitmaininto a root phase and a runtime phase underdatabasus, following design decision 1. The root phase ends withexec gosu databasus /app/start.shand a phase argument, and the PostgreSQL steps drop theirgosu databasusprefix. Install aTERMandINTtrap at the start of each phase, following design decision 4. Before the application starts, the handler logsStop requested during startupand stops the PostgreSQL process running at that moment. The regular server receivesSIGINTfor a fast shutdown, a single-user stepSIGTERM. The handler waits in a loop whilekill -0succeeds and then exits with143or130. Run the single-user steps in the background, followed bywaiton their process ID. Verify thatdocker build --tag databasus-storage-test:local .succeeds and thatCASE=startup-stop DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shpasses. - 2.2 Replace
exec gosu databasus ./mainwith a supervision function that follows design decisions 1, 2, 3 and 5. It starts the application in the background, waits on both process IDs withwait -nand forwards a stop signal to the application. It repeatswait -n -puntil Bash names the process that exited, collects every status with|| status=$?and logsDatabasus application exited with status <n>. Afterwards it sends the postmasterSIGINTfor 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 with1. Verify thatdocker build --tag databasus-storage-test:local .succeeds and thatCASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shpasses.
3. Cover the remaining stop paths
3.1 Extend the first part of
clean-shutdown. Assert thatDatabasus application exited with status 0comes before PostgreSQL'sreceived fast shutdown requestline indocker logs. Verify thatCASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shpasses.3.2 Add three parts to
clean-shutdown, each on a fresh volume.- Interrupt: send
docker kill --signal INTto a healthy container. Assert that it exits within 10 seconds and that the cluster state isshut down. - Application exit: send
SIGTERMto the application process alone withdocker exec <container> /bin/bash -c 'kill -TERM <pid of ./main>', because the image ships nokillexecutable. Assert that the container exits with0and that the cluster state isshut down. - PostgreSQL exit: send
SIGKILLto the postmaster fromhead -1 /databasus-data/pgdata/postmaster.pid. Assert that the container exits with a non-zero status, that the log has the supervisor'sERROR: PostgreSQL exited with statusline and that it lacks theDatabasus application exited with statusline of a normal application exit.
Verify that
CASE=clean-shutdown DATABASUS_IMAGE=databasus-storage-test:local bash e2e/docker-storage/run.shpasses.- Interrupt: send
3.3 Verify that the
bind-root,upgradesandpostgresql-recoverycases still pass one at a time throughCASE=<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 --strictpasses. - 4.2 Run the mandatory full implementation review from
AGENTS.mdand resolve its blocking findings. Then rebuild the image and verify that theclean-shutdownandstartup-stopcases pass.