From 2d749057f9c2858b3266c442f2bdb23735f94f90 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Mon, 3 Aug 2026 12:16:08 +0100 Subject: [PATCH] Add ONCE backup and restore hooks By default, ONCE pauses the application container when taking a backup. This guarantees a consistent snapshot of the data, but for a large installation and/or a slow destination path (like a network mount), that pause could be a bit disruptive to live traffic. But ONCE also support a pause-free path: if a `pre-backup` hook is present in the container, ONCE will run that and skip the pause. So by supplying necessary hooks, we can avoid any disruption from backups. - `pre-backup` runs the existing script/admin/prepare-backup, which uses SQLite's online backup API to get a consistent snapshop. - `post-restore` moves our snaphotted file back into place, and removes the orphaned sidecar files --- Dockerfile | 3 +++ hooks/post-restore | 12 ++++++++++++ hooks/pre-backup | 2 ++ 3 files changed, 17 insertions(+) create mode 100755 hooks/post-restore create mode 100755 hooks/pre-backup diff --git a/Dockerfile b/Dockerfile index 6cfbd33..b65a227 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,6 +66,9 @@ ENV HTTP_WRITE_TIMEOUT=300 COPY --from=build --chown=rails:rails /usr/local/bundle /usr/local/bundle COPY --from=build --chown=rails:rails /rails /rails +# Install ONCE backup/restore hooks +COPY --chmod=755 hooks /hooks + # Set version and revision ARG APP_VERSION ENV APP_VERSION=$APP_VERSION diff --git a/hooks/post-restore b/hooks/post-restore new file mode 100755 index 0000000..0b9138d --- /dev/null +++ b/hooks/post-restore @@ -0,0 +1,12 @@ +#!/bin/bash +set -euo pipefail + +db_name="${RAILS_ENV:-production}.sqlite3" +snapshot="/rails/storage/backups/$db_name" +db="/rails/storage/db/$db_name" + +if [ -f "$snapshot" ]; then + mkdir -p "$(dirname "$db")" + cp "$snapshot" "$db" + rm -f "$db-wal" "$db-shm" +fi diff --git a/hooks/pre-backup b/hooks/pre-backup new file mode 100755 index 0000000..43d47f9 --- /dev/null +++ b/hooks/pre-backup @@ -0,0 +1,2 @@ +#!/bin/bash +exec /rails/script/admin/prepare-backup