Skip to content

Instantly share code, notes, and snippets.

@koliadych
Created May 11, 2026 09:21
Show Gist options
  • Select an option

  • Save koliadych/21e6ab8496ea73a75becf3765b4a5e49 to your computer and use it in GitHub Desktop.

Select an option

Save koliadych/21e6ab8496ea73a75becf3765b4a5e49 to your computer and use it in GitHub Desktop.
fintechner restore env + watch backfill
#!/bin/bash
# 1) restore connector.env now, 2) tell user the deploy gist is patched.
set -euo pipefail
export AWS_PAGER=""
REGION=ap-northeast-1
INSTANCE_ID=i-089ad4a6b76132ba2
ADMIN_EMAIL="koliadych@gmail.com"
echo "=== Restoring connector.env from Secrets Manager ==="
DB_PWD=$(aws --region "$REGION" secretsmanager get-secret-value --secret-id fintechner/db-password --query SecretString --output text)
MEK=$(aws --region "$REGION" secretsmanager get-secret-value --secret-id fintechner/master-encryption-key --query SecretString --output text)
ENV=$(cat <<EOF
LOG_LEVEL=info
HTTP_ADDR=:8080
DATABASE_URL=postgres://fintechner:${DB_PWD}@127.0.0.1:5432/fintechner?sslmode=disable
MASTER_ENCRYPTION_KEY=${MEK}
ADMIN_EMAIL=${ADMIN_EMAIL}
EOF
)
ENV_B64=$(printf '%s' "$ENV" | base64 -w0)
unset DB_PWD MEK ENV
cat > /tmp/ssm.json <<JSON
{"commands":[
"echo '${ENV_B64}' | base64 -d | sudo tee /etc/fintechner/connector.env >/dev/null",
"sudo chmod 600 /etc/fintechner/connector.env",
"sudo systemctl reset-failed fintechner-connector",
"sudo systemctl restart fintechner-connector",
"sleep 10",
"echo === connector status ===",
"sudo systemctl is-active fintechner-connector",
"echo === backfill log (10s window) ===",
"sudo journalctl -u fintechner-connector --since \"15 sec ago\" --no-pager | grep -iE 'closedtrades|backfill|closed trade|fatal' | head -20",
"echo === closed_trades count ===",
"sudo docker exec timescale psql -U fintechner -d fintechner -tA -c \"SELECT COUNT(*) FROM closed_trades;\""
]}
JSON
CMD_ID=$(aws --region "$REGION" ssm send-command --instance-ids "$INSTANCE_ID" --document-name AWS-RunShellScript --parameters file:///tmp/ssm.json --query Command.CommandId --output text)
rm -f /tmp/ssm.json
for i in $(seq 1 25); do
sleep 3
STATUS=$(aws --region "$REGION" ssm get-command-invocation --command-id "$CMD_ID" --instance-id "$INSTANCE_ID" --query Status --output text 2>/dev/null || echo Pending)
case "$STATUS" in Success|Failed|Cancelled|TimedOut) break ;; esac
done
aws --region "$REGION" ssm get-command-invocation --command-id "$CMD_ID" --instance-id "$INSTANCE_ID" --query StandardOutputContent --output text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment