#!/bin/bash set -e # clickhouse-backup output lives inside the event-data volume (snapshotted via # backupbot.backup.volumes.event-data.path). Restoring the raw data files under a # running server is unsafe, so restore performs a logical restore instead. BACKUP_DIR=/var/lib/clickhouse/backup/events MIGRATIONS_TSV="$BACKUP_DIR/schema_migrations.tsv" backup() { clickhouse-backup create events # schema_migrations is a TinyLog table — clickhouse-backup only FREEZEs MergeTree # data, so its rows aren't captured. Export them alongside the backup, else a restore # leaves the ledger empty and the next boot re-runs every migration (DUPLICATE_COLUMN). clickhouse-client --query "SELECT * FROM plausible_events_db.schema_migrations FORMAT TSV" > "$MIGRATIONS_TSV" } backup_cleanup() { rm -rf "$BACKUP_DIR" } restore() { clickhouse-backup restore --rm events clickhouse-client --query "TRUNCATE TABLE plausible_events_db.schema_migrations" clickhouse-client --query "INSERT INTO plausible_events_db.schema_migrations FORMAT TSV" < "$MIGRATIONS_TSV" rm -rf "$BACKUP_DIR" } "$@"