Files
twooeyandClaude Sonnet 5 6299391f23 Run wp-cli as www-data instead of root; drop --allow-root
--allow-root was routing around wp-cli's own safety check rather than
addressing why root was there in the first place: docker exec defaults to
the container's root user because the image never sets a non-root user
for exec sessions — it was never about the actual web-facing attack
surface, which already runs as www-data (verified: php-fpm's worker
processes, the ones executing plugin/theme code for real requests, run as
uid 33, not root; only our own deliberate admin commands were root).

wp-config.php and the rest of wp-core are already owned by www-data (the
official entrypoint sets this up), so there's no actual reason for our
own commands to run as anything else. Verified: a full clean deploy and
a bookstore-core wp-cli command both work identically running as
www-data, no functional change, just removed an unnecessary privilege.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-27 13:39:19 -04:00

38 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Restores a backup INTO STAGING ONLY. Deliberately has no production path —
# the launch gate is "a full backup has been restored into staging," never
# production overwritten by a drill.
set -euo pipefail
DB_DUMP="${1:?Usage: restore.sh <db-dump.sql.gz> <uploads.tar.gz>}"
UPLOADS_ARCHIVE="${2:?Usage: restore.sh <db-dump.sql.gz> <uploads.tar.gz>}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
ENV_FILE=".env.staging"
export ENVIRONMENT=staging
# See deploy.sh for why: Compose warns on any $-shaped value in --env-file
# even when unused, so DB_PASSWORD/DB_ROOT_PASSWORD are filtered out of the
# copy Compose actually sees.
COMPOSE_ENV_FILE="$(mktemp)"
trap 'rm -f "$COMPOSE_ENV_FILE"' EXIT
grep -Ev '^(DB_PASSWORD|DB_ROOT_PASSWORD)=' "$ENV_FILE" > "$COMPOSE_ENV_FILE"
COMPOSE="docker compose -p bookstore-staging -f docker-compose.yml -f docker-compose.staging.yml --env-file ${COMPOSE_ENV_FILE}"
echo "!! this OVERWRITES the staging database and uploads !!"
read -r -p "type 'restore' to continue: " confirm
[[ "$confirm" == "restore" ]] || { echo "aborted"; exit 1; }
echo "==> restoring database"
gunzip -c "$DB_DUMP" | $COMPOSE exec -T db sh -c "exec mariadb -u\"\$MARIADB_USER\" -p\"\$(cat /run/secrets/db_password)\" \"\$MARIADB_DATABASE\""
echo "==> restoring uploads"
ARCHIVE_DIR="$(cd "$(dirname "$UPLOADS_ARCHIVE")" && pwd)"
ARCHIVE_NAME="$(basename "$UPLOADS_ARCHIVE")"
$COMPOSE run --rm -T -u www-data -v "${ARCHIVE_DIR}:/restore:ro" wordpress \
tar -xzf "/restore/${ARCHIVE_NAME}" -C /var/www/html/wp-content
echo "==> restore complete — verify the site before treating the drill as passed"