~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
47 lines · 2.0 KB Raw History
#!/bin/sh
# The container's command. Without ZDS_LITESTREAM_BUCKET it is zds and
# nothing else. With it, Litestream runs zds as its child and streams the
# database to the bucket until zds exits; see docs/backups.md.
set -eu

if [ -z "${ZDS_LITESTREAM_BUCKET:-}" ]; then
  exec zds "$@"
fi

export ZDS_DB="${ZDS_DB:-/data/zds.sqlite3}"
export ZDS_LITESTREAM_PATH="${ZDS_LITESTREAM_PATH:-zds.sqlite3}"
export ZDS_LITESTREAM_SYNC_INTERVAL="${ZDS_LITESTREAM_SYNC_INTERVAL:-10s}"
export ZDS_LITESTREAM_RETENTION="${ZDS_LITESTREAM_RETENTION:-168h}"
export AWS_ENDPOINT_URL_S3="${AWS_ENDPOINT_URL_S3:-}"
export AWS_REGION="${AWS_REGION:-auto}"
config="${ZDS_LITESTREAM_CONFIG:-/etc/zds/litestream.yml}"

# A machine that comes up without its database must not replicate an empty
# one over the backup: Litestream would take the new database as the
# replica's latest state. With ZDS_LITESTREAM_RESTORE_IF_MISSING=true the
# replica is restored first; otherwise zds starts empty only when the replica
# is empty too (a new install), and refuses to start when it is not.
restore=""
if [ ! -e "$ZDS_DB" ]; then
  if [ "${ZDS_LITESTREAM_RESTORE_IF_MISSING:-false}" = true ]; then
    restore="-restore-if-db-not-exists"
  elif plan=$(litestream restore -config "$config" -dry-run -o /dev/null "$ZDS_DB" 2>&1); then
    echo "zds: $ZDS_DB is missing but the replica at $ZDS_LITESTREAM_BUCKET/$ZDS_LITESTREAM_PATH has a backup." >&2
    echo "zds: refusing to replicate an empty database over it. Restore it (ZDS_LITESTREAM_RESTORE_IF_MISSING=true)" >&2
    echo "zds: or point ZDS_LITESTREAM_PATH at a new replica; see docs/backups.md." >&2
    exit 1
  else
    case "$plan" in
      *"no matching backup files"*) ;;
      *)
        echo "zds: $ZDS_DB is missing and the replica could not be checked:" >&2
        echo "$plan" >&2
        exit 1
        ;;
    esac
  fi
fi

# Litestream's -exec takes one string that it splits on spaces, so arguments
# containing spaces are not supported here.
exec litestream replicate -config "$config" $restore -exec "zds${*:+ $*}"