A Plausible backup has three parts: PostgreSQL, ClickHouse and the .env with SECRET_KEY_BASE and TOTP_VAULT_KEY. If the .env is missing and the new server gets fresh keys, all data is there – but no API key works any more.
Symptom
K=$(cat ~/plausible/api-key)
Q='{"site_id":"itdrills.com","date_range":"all","metrics":["visitors"]}'
curl -s -H "Authorization: Bearer $K" -H 'Content-Type: application/json' \
http://127.0.0.1:8001/api/v2/query -d "$Q"; echo
{"error":"Invalid API key or site ID. Please make sure you're using a valid API key
with access to the site you've requested."}
The restore itself ran without errors and /api/health reports "ok" everywhere. Only the Stats API rejects every existing key.
Diagnosis
Check whether the key is in the database – by its prefix, never print the whole key.
cd ~/plausible/neu
K=$(cat ~/plausible/api-key)
echo "Präfix des Keys: ${K:0:6}"
docker compose exec -T plausible_db psql -U postgres -d plausible_db \
-tAc "select name, key_prefix, length(key_hash) from api_keys"
Präfix des Keys: Ozv356
labor|Ozv356|64
Causes
SECRET_KEY_BASENew secret, different hash
The key is there and the prefix matches. But Plausible doesn't store the key, it stores a hash of SECRET_KEY_BASE and key. With a new SECRET_KEY_BASE the hash comes out different, and no key matches any more.
TOTP_VAULT_KEYSame principle for 2FA
The 2FA secrets are encrypted with TOTP_VAULT_KEY. Without the old key they're worthless too.
Fix
cd ~/plausible/neu
install -m 600 ~/plausible/tag08/env .env
docker compose up -d 2>&1 | tail -1
until curl -sf http://127.0.0.1:8001/api/health >/dev/null; do sleep 2; done
K=$(cat ~/plausible/api-key)
Q='{"site_id":"itdrills.com","date_range":"all","metrics":["visitors"]}'
curl -s -H "Authorization: Bearer $K" -H 'Content-Type: application/json' \
http://127.0.0.1:8001/api/v2/query -d "$Q" | jq -c '.results'
[{"metrics":[16],"dimensions":[]}]
Don't: Forgetting the .env in the backup. New keys can be generated in a pinch – but then every API key has to be created again and 2FA set up from scratch.
Plausible Analytics, day 8: back up PostgreSQL, ClickHouse and the keys, restore onto a fresh stack and check the numbers.
Go to day 8 →
FAQ
What belongs in a complete Plausible backup?
PostgreSQL (accounts, sites, goals, shields, keys), ClickHouse (events and sessions) and the .env with SECRET_KEY_BASE and TOTP_VAULT_KEY.
How do I store the .env safely?
With permissions 600, encrypted wherever the backup goes, and additionally in a password manager – it contains secrets.