#!/usr/bin/env bash # Load the data in ~/railway/data into the VM's services: Postgres from # pg_dump, Redis from its RDB file, and volume files from tar. # From https://exe.dev/deploy/migrate-railway # # railway/restore everything handed over # railway/restore SERVICE one Railway service, by name # # It replaces what the VM's copies hold now. Stop the app's services first # (railway/move does). set -euo pipefail cd ~/app data=~/railway/data log() { printf '\033[1m%s\033[0m\n' "$*" >&2; } pg_ready() { for _ in $(seq 60); do docker compose exec -T "$1" sh -c 'pg_isready -q -h localhost -U "$PGUSER"' 2>/dev/null && return 0 sleep 2 done log "$1: Postgres doesn't answer"; return 1 } while IFS=$'\t' read -r name slug kind mount _ <&3; do [ -z "${1:-}" ] || [ "$1" = "$name" ] || continue case $kind in postgres) [ -f "$data/$name.pgdump" ] || continue log "$slug: restoring $name.pgdump" docker compose up -d --no-deps "$slug" >&2 pg_ready "$slug" docker compose exec -T "$slug" sh -c 'psql -q -h localhost -U "$PGUSER" -d postgres -v ON_ERROR_STOP=1 \ -c "DROP DATABASE IF EXISTS \"$PGDATABASE\" WITH (FORCE)" -c "CREATE DATABASE \"$PGDATABASE\""' docker compose exec -T "$slug" sh -c 'pg_restore -h localhost -U "$PGUSER" -d "$PGDATABASE" --no-owner --no-privileges --exit-on-error' \ < "$data/$name.pgdump" docker compose exec -T "$slug" sh -c 'psql -h localhost -U "$PGUSER" -d "$PGDATABASE" -Atc \ "select count(*) || '"' tables'"' from pg_tables where schemaname not in ('"'pg_catalog'"', '"'information_schema'"')"' ;; redis) [ -f "$data/$name.rdb" ] || continue log "$slug: restoring $name.rdb" docker compose stop "$slug" >&2 2>/dev/null || true sudo install -m 600 "$data/$name.rdb" "volumes/$slug/dump.rdb" sudo rm -f "volumes/$slug/appendonly.aof"; sudo rm -rf "volumes/$slug/appendonlydir" docker compose up -d --no-deps "$slug" >&2 sleep 2 docker compose exec -T "$slug" sh -c 'redis-cli -a "$REDIS_PASSWORD" --no-auth-warning dbsize | sed "s/$/ keys/"' ;; *) [ -f "$data/$name.tar" ] && [ "$mount" != - ] || continue log "$slug: restoring the files in $mount" docker compose stop "$slug" >&2 2>/dev/null || true sudo find "volumes/$slug" -mindepth 1 -delete sudo tar --numeric-owner -xpf "$data/$name.tar" -C "volumes/$slug" sudo du -sh "volumes/$slug" ;; esac done 3< <(~/railway/import list)