Skip to content

Instantly share code, notes, and snippets.

@krin-san
Last active February 4, 2026 19:16
Show Gist options
  • Select an option

  • Save krin-san/d12837b957de22aa10590c15a9fb3a5d to your computer and use it in GitHub Desktop.

Select an option

Save krin-san/d12837b957de22aa10590c15a9fb3a5d to your computer and use it in GitHub Desktop.
gramps-web automatic backup script
#!/usr/bin/env bash
# This script backs up the Gramps-Web database in ".gramps" format.
# Back them up together with the "media" volume to have a restorable app backup.
set -euo pipefail
# Access
BASE_URL='https://demo.grampsweb.org' # without trailing slash
USERNAME='owner'
PASSWORD='owner'
# Config
EXTENSION="gramps" # exporter extension
FILENAME="gramps-web_$(date '+%Y-%m-%d_%H-%M-%S').${EXTENSION}"
TASK_TIMEOUT=10 # attempts, one per second
# https://gramps-project.github.io/gramps-web-api/#/authentication/login
ACCESS_TOKEN=$(curl -sS --fail --json '{"username":"'"$USERNAME"'","password":"'"$PASSWORD"'"}' "${BASE_URL}/api/token/" | jq -r '.access_token // ""')
if [ -z "$ACCESS_TOKEN" ]; then
echo "error: Failed to obtain access token. Please check if auth credentials are correct."
exit 401
fi
# List available exporters
#curl -sS --fail -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE_URL}/api/exporters/" | jq
# https://gramps-project.github.io/gramps-web-api/#/exporters/postExportFile
URL_PATH="/api/exporters/${EXTENSION}/file"
echo -n "${URL_PATH}: "
OUTPUT=$(curl -sS --fail -H "Authorization: Bearer $ACCESS_TOKEN" -X POST "${BASE_URL}${URL_PATH}")
echo $OUTPUT | jq -C
# TODO: Support exporters which imediately return "url": https://gramps-project.github.io/gramps-web-api/#/exporters/postExportFile
TASK_ID=$(echo "${OUTPUT}" | jq -r '.task.id // ""')
if [ -z "$TASK_ID" ]; then
echo "error: No task identifier found in output. Maybe exporter generated a file without using async tasks?"
exit 404
fi
attempts=$TASK_TIMEOUT
while [ $attempts -gt 0 ]; do
sleep 1
URL_PATH="/api/tasks/$TASK_ID"
echo -n "${URL_PATH}: "
OUTPUT=$(curl -sS --fail -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE_URL}${URL_PATH}")
echo $OUTPUT | jq -C
FILE_PATH=$(echo "$OUTPUT" | jq -r '.result_object.url // ""')
if [ -z "$FILE_PATH" ]; then
attempts=$((attempts - 1))
else
break
fi
done
if [ -z "$FILE_PATH" ]; then
echo "error: Timed out waiting ${TASK_TIMEOUT}s for async task results."
exit 408
fi
echo "Downloading ${FILE_PATH}"
curl -o "$FILENAME" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE_URL}${FILE_PATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment