Owner bought and approved the new host on 2026-11-22 (mainland latency ~44 ms vs ~300 ms on 118.194.235.34). The four production workflows pin the host, so the release SHA must carry the new value. The runbook records the fresh-start sequence (no Supabase import). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
184 lines
9.7 KiB
YAML
184 lines
9.7 KiB
YAML
name: Backup Production Daily
|
|
|
|
on:
|
|
schedule:
|
|
# 04:00 Asia/Shanghai (UTC+8). Gitea cron is UTC.
|
|
- cron: "0 20 * * *"
|
|
workflow_dispatch:
|
|
inputs:
|
|
artifact_retention_days:
|
|
description: Days to keep the encrypted artifact on Gitea
|
|
required: false
|
|
default: "14"
|
|
type: string
|
|
local_retention_days:
|
|
description: Days to keep this workflow's encrypted files on the host
|
|
required: false
|
|
default: "7"
|
|
type: string
|
|
|
|
permissions:
|
|
contents: read
|
|
actions: write
|
|
|
|
concurrency:
|
|
group: production-mutation
|
|
cancel-in-progress: false
|
|
queue: max
|
|
|
|
jobs:
|
|
backup:
|
|
if: gitea.ref == 'refs/heads/main'
|
|
runs-on: xiaoxin
|
|
timeout-minutes: 30
|
|
env:
|
|
GITEA_SHA: ${{ gitea.sha }}
|
|
GITEA_REF: ${{ gitea.ref }}
|
|
GITEA_API_URL: ${{ gitea.api_url }}
|
|
GITEA_REPOSITORY: ${{ gitea.repository }}
|
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
|
DEPLOY_HOST: ${{ vars.PRODUCTION_HOST }}
|
|
DEPLOY_PORT: ${{ vars.PRODUCTION_PORT }}
|
|
DEPLOY_USER: ${{ vars.PRODUCTION_USER }}
|
|
DEPLOY_PATH: ${{ vars.PRODUCTION_PATH }}
|
|
PRODUCTION_KNOWN_HOSTS: ${{ vars.PRODUCTION_KNOWN_HOSTS }}
|
|
ARTIFACT_RETENTION_DAYS: ${{ inputs.artifact_retention_days }}
|
|
LOCAL_RETENTION_DAYS: ${{ inputs.local_retention_days }}
|
|
steps:
|
|
- name: Create, restore-verify, and retrieve the daily encrypted backup
|
|
id: backup
|
|
env:
|
|
SSH_PRIVATE_KEY_BASE64: ${{ secrets.PRODUCTION_SSH_PRIVATE_KEY }}
|
|
run: |
|
|
set -euo pipefail
|
|
set +x
|
|
[[ "$GITEA_REF" == "refs/heads/main" ]]
|
|
[[ "$GITEA_SHA" =~ ^[0-9a-f]{40}$ ]]
|
|
artifact_retention="${ARTIFACT_RETENTION_DAYS:-14}"
|
|
[ -n "$artifact_retention" ] || artifact_retention=14
|
|
local_retention="${LOCAL_RETENTION_DAYS:-7}"
|
|
[ -n "$local_retention" ] || local_retention=7
|
|
[[ "$artifact_retention" =~ ^[1-9][0-9]{0,3}$ ]]
|
|
[[ "$local_retention" =~ ^[1-9][0-9]{0,3}$ ]]
|
|
[[ "$DEPLOY_HOST" == "83.229.126.99" ]]
|
|
[[ "$DEPLOY_PORT" =~ ^[1-9][0-9]{0,4}$ ]] && (( DEPLOY_PORT <= 65535 ))
|
|
[[ "$DEPLOY_USER" == "deploy" ]]
|
|
[[ "$DEPLOY_PATH" == "/opt/jyotisha-production" ]]
|
|
[[ "${GITHUB_RUN_ID:-}" =~ ^[0-9]+$ ]]
|
|
test -n "$PRODUCTION_KNOWN_HOSTS"
|
|
echo "artifact_retention_days=$artifact_retention" >>"$GITHUB_OUTPUT"
|
|
echo "local_retention_days=$local_retention" >>"$GITHUB_OUTPUT"
|
|
ssh_root="${RUNNER_TEMP}/production-daily-ssh"
|
|
key_path="$ssh_root/id_ed25519"
|
|
known_hosts_path="$ssh_root/known_hosts"
|
|
artifact_directory="artifacts/production-daily"
|
|
install -m 700 -d "$ssh_root" "$artifact_directory"
|
|
test -n "$SSH_PRIVATE_KEY_BASE64"
|
|
printf '%s' "$SSH_PRIVATE_KEY_BASE64" | base64 --decode >"$key_path"
|
|
printf '%s\n' "$PRODUCTION_KNOWN_HOSTS" | tr -d '\r' >"$known_hosts_path"
|
|
chmod 600 "$key_path" "$known_hosts_path"
|
|
ssh-keygen -y -f "$key_path" >/dev/null
|
|
ssh_options=(-i "$key_path" -p "$DEPLOY_PORT" -o BatchMode=yes -o IdentitiesOnly=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=4 -o StrictHostKeyChecking=yes -o "UserKnownHostsFile=$known_hosts_path")
|
|
scp_options=(-i "$key_path" -P "$DEPLOY_PORT" -o BatchMode=yes -o IdentitiesOnly=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=4 -o StrictHostKeyChecking=yes -o "UserKnownHostsFile=$known_hosts_path")
|
|
remote="$DEPLOY_USER@$DEPLOY_HOST"
|
|
incoming="$(ssh "${ssh_options[@]}" "$remote" 'mktemp -d /tmp/jyotisha-production-daily.XXXXXXXXXX')"
|
|
[[ "$incoming" == /tmp/jyotisha-production-daily.* ]]
|
|
cleanup() {
|
|
ssh "${ssh_options[@]}" "$remote" "rm -rf -- '$incoming'" >/dev/null 2>&1 || true
|
|
rm -rf -- "$ssh_root"
|
|
}
|
|
trap cleanup EXIT
|
|
git init .
|
|
git remote remove origin 2>/dev/null || true
|
|
git remote add origin https://git.copse.top/root/Jyotisha.git
|
|
git fetch --no-tags origin "$GITEA_SHA"
|
|
git checkout --detach --force "$GITEA_SHA"
|
|
[[ "$(git rev-parse HEAD)" == "$GITEA_SHA" ]]
|
|
git show "$GITEA_SHA:deploy/run-production-recovery.sh" >"$ssh_root/run-production-recovery.sh"
|
|
git show "$GITEA_SHA:deploy/prune-production-daily-backups.sh" >"$ssh_root/prune-production-daily-backups.sh"
|
|
chmod 700 "$ssh_root/run-production-recovery.sh" "$ssh_root/prune-production-daily-backups.sh"
|
|
scp "${scp_options[@]}" \
|
|
"$ssh_root/run-production-recovery.sh" \
|
|
"$ssh_root/prune-production-daily-backups.sh" \
|
|
"$remote:$incoming/"
|
|
ssh "${ssh_options[@]}" "$remote" \
|
|
"bash '$incoming/prune-production-daily-backups.sh' '$DEPLOY_PATH/backups' '$local_retention'"
|
|
ssh "${ssh_options[@]}" "$remote" \
|
|
"DEPLOY_PATH='$DEPLOY_PATH' RECOVERY_RUN_ID='$GITHUB_RUN_ID' RECOVERY_BASENAME_PREFIX='production-daily' bash '$incoming/run-production-recovery.sh'" \
|
|
>"$ssh_root/recovery-output.env"
|
|
[[ "$(wc -l <"$ssh_root/recovery-output.env" | tr -d ' ')" == 7 ]]
|
|
recovery_reference="$(awk -F= '$1 == "RECOVERY_REFERENCE" {print $2}' "$ssh_root/recovery-output.env")"
|
|
recovery_created_at="$(awk -F= '$1 == "RECOVERY_CREATED_AT" {print $2}' "$ssh_root/recovery-output.env")"
|
|
recovery_verified_at="$(awk -F= '$1 == "RECOVERY_VERIFIED_AT" {print $2}' "$ssh_root/recovery-output.env")"
|
|
recovery_sha256="$(awk -F= '$1 == "RECOVERY_SHA256" {print $2}' "$ssh_root/recovery-output.env")"
|
|
restore_verified="$(awk -F= '$1 == "RESTORE_VERIFIED" {print $2}' "$ssh_root/recovery-output.env")"
|
|
backup_basename="$(awk -F= '$1 == "BACKUP_BASENAME" {print $2}' "$ssh_root/recovery-output.env")"
|
|
verify_basename="$(awk -F= '$1 == "VERIFY_BASENAME" {print $2}' "$ssh_root/recovery-output.env")"
|
|
[[ "$backup_basename" =~ ^production-daily-[0-9]{8}T[0-9]{6}Z\.dump\.enc$ ]]
|
|
[[ "$verify_basename" == "${backup_basename%.dump.enc}-restore-verify.json" ]]
|
|
[[ "$recovery_reference" == "gitea-actions-run-${GITHUB_RUN_ID}/${backup_basename}" ]]
|
|
[[ "$recovery_created_at" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$ ]]
|
|
[[ "$recovery_verified_at" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$ ]]
|
|
[[ "$recovery_sha256" =~ ^[0-9a-f]{64}$ ]]
|
|
[[ "$restore_verified" == "true" ]]
|
|
scp "${scp_options[@]}" \
|
|
"$remote:$DEPLOY_PATH/backups/$backup_basename" \
|
|
"$remote:$DEPLOY_PATH/backups/$verify_basename" \
|
|
"$artifact_directory/"
|
|
test -s "$artifact_directory/$backup_basename"
|
|
test -s "$artifact_directory/$verify_basename"
|
|
printf '%s %s\n' "$recovery_sha256" "$artifact_directory/$backup_basename" | sha256sum --check --status
|
|
jq -e \
|
|
--arg backup "$backup_basename" \
|
|
--arg sha "$recovery_sha256" \
|
|
--arg created "$recovery_created_at" \
|
|
--arg verified "$recovery_verified_at" '
|
|
.mode == "restore_verify" and .ok == true and
|
|
.backup == $backup and .sha256 == $sha and
|
|
.created_at == $created and .verified_at == $verified and
|
|
.restore_database_removed == true and
|
|
([.checks.identity_users, .checks.profiles, .checks.credit_transactions, .checks.public_tables] |
|
|
all(type == "number" and . >= 0 and floor == .))
|
|
' "$artifact_directory/$verify_basename" >/dev/null
|
|
printf 'RECOVERY_REFERENCE=%s\nRECOVERY_CREATED_AT=%s\nRECOVERY_VERIFIED_AT=%s\nRECOVERY_SHA256=%s\nRESTORE_VERIFIED=true\nBACKUP_BASENAME=%s\nVERIFY_BASENAME=%s\n' \
|
|
"$recovery_reference" "$recovery_created_at" "$recovery_verified_at" "$recovery_sha256" \
|
|
"$backup_basename" "$verify_basename" >"$artifact_directory/attestation.env"
|
|
chmod 600 "$artifact_directory"/*
|
|
{
|
|
echo "recovery_reference=$recovery_reference"
|
|
echo "recovery_created_at=$recovery_created_at"
|
|
echo "recovery_verified_at=$recovery_verified_at"
|
|
echo "recovery_sha256=$recovery_sha256"
|
|
} >>"$GITHUB_OUTPUT"
|
|
echo "Daily backup restore-verified: reference=$recovery_reference created_at=$recovery_created_at verified_at=$recovery_verified_at"
|
|
|
|
- name: Upload encrypted off-site daily backup
|
|
env:
|
|
ARTIFACT_RETENTION_DAYS: ${{ steps.backup.outputs.artifact_retention_days }}
|
|
run: |
|
|
set -euo pipefail
|
|
set +x
|
|
[[ "${ARTIFACT_RETENTION_DAYS:-}" =~ ^[1-9][0-9]{0,3}$ ]]
|
|
test -n "${ACTIONS_RUNTIME_TOKEN:-}"
|
|
test -n "${ACTIONS_RESULTS_URL:-}"
|
|
test -n "${GITHUB_RUN_ID:-}"
|
|
test -n "${GITHUB_REPOSITORY:-}"
|
|
workdir="$(pwd -P)"
|
|
docker run --rm \
|
|
--user "$(id -u):$(id -g)" \
|
|
--volume "$workdir:$workdir" \
|
|
--workdir "$workdir" \
|
|
--env HOME=/tmp \
|
|
--env "INPUT_NAME=production-daily-$GITHUB_RUN_ID" \
|
|
--env INPUT_PATH=artifacts/production-daily/ \
|
|
--env INPUT_OVERWRITE=false \
|
|
--env ACTIONS_RUNTIME_TOKEN \
|
|
--env ACTIONS_RESULTS_URL \
|
|
--env GITHUB_RUN_ID \
|
|
--env GITHUB_REPOSITORY \
|
|
--env "GITHUB_SHA=$GITEA_SHA" \
|
|
--env "GITHUB_WORKSPACE=$workdir" \
|
|
--env ARTIFACT_RETENTION_DAYS \
|
|
node:22-bookworm-slim \
|
|
node -e 'process.env["INPUT_IF-NO-FILES-FOUND"]="error"; process.env["INPUT_RETENTION-DAYS"]=process.env.ARTIFACT_RETENTION_DAYS; process.env["INPUT_COMPRESSION-LEVEL"]="0"; require("./.gitea/actions/upload-artifact/dist/index.js")'
|