Skip to content

Restore from backup #11

Restore from backup

Restore from backup #11

name: Restore from backup
on:
workflow_dispatch:
# on:
# schedule:
# - cron: '0 0 * * *' # Runs at 00:00 UTC daily.
jobs:
backup_upload:
runs-on: ubuntu-latest
environment: staging
steps:
- name: Setup SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/private_key.pem
chmod 600 ~/.ssh/private_key.pem
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
- name: Update from backup
run: |
set -x
echo '${{ secrets.GCP_SA_KEY }}' > /tmp/gcp-sa-key.json
gcloud auth activate-service-account --key-file=/tmp/gcp-sa-key.json
rm /tmp/gcp-sa-key.json $BACKUP_NAME
BACKUP=$(gsutil ls -l gs://ocho-osai/track/pg_data/ | grep -v "TOTAL:" | sort -k2,2 -r | awk '{ $1=$2=""; print $0 }' | sed 's/^[ \t]*//' | head -n 1 | tr -d '\n')
BACKUP_NAME=$(basename $BACKUP)
BACKUP_FILE="backups/$BACKUP_NAME"
mkdir -p ./track/backups/
gsutil cp $BACKUP $BACKUP_FILE
echo "Most recent backup found: $BACKUP_NAME"
# Check if the backup file exists and is a gzip file
if [ -f "$BACKUP_FILE" ] && [[ $BACKUP_FILE == *.gz ]]; then
gunzip -k "$BACKUP_FILE"
BACKUP_FILE="${BACKUP_FILE%.gz}"
echo "Backup decompressed to $BACKUP_FILE"
else
echo "Backup file does not exist or is not a gzip file."
exit 1
fi
echo "Removing old backups."
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
"rm -r /home/${{ secrets.SERVER_USER }}/track/backups/ || true"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
"mkdir -p /home/${{ secrets.SERVER_USER }}/track/backups/"
scp -i ~/.ssh/private_key.pem $BACKUP_FILE ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:/home/${{ secrets.SERVER_USER }}/track/$BACKUP_FILE
echo "Available backups:"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
ls ./track/backups/
echo "Available backups:"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
ls ./track/$BACKUP_FILE
echo "Bringing down docker containers."
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml down
echo "Status:"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml ps
echo "Starting db service in isolation."
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml up -d db
echo "Status:"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml ps
echo "A"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml exec db echo "hello world!"
echo "B"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml exec db echo goodbyeworld!"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml exec -T db psql -U postgres -c "DROP DATABASE IF EXISTS trackdb;"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml exec -T db psql -U postgres -c "CREATE DATABASE trackdb;"
ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
docker compose -f track/base.yml -f track/prod.yml cp ./track/$BACKUP_FILE db:/backup
# ssh -i ~/.ssh/private_key.pem ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} << ENDSSH
# set -x
# ls ./track/backups/
# export APP_ENV=${{ vars.APP_ENV }}
# export HOST_PORT=${{ vars.HOST_PORT }}
# export POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
# # Restore from backup. The postgres docker service is called "db"
# echo "Bringing down services."
# docker compose -f track/base.yml -f track/prod.yml down
# echo "Status:"
# docker compose -f track/base.yml -f track/prod.yml ps
# echo "Starting db service in isolation."
# docker compose -f track/base.yml -f track/prod.yml up -d db
# echo "Status:"
# docker compose -f track/base.yml -f track/prod.yml ps
# echo "Preparing for restore."
# #docker compose -f track/base.yml -f track/prod.yml exec db echo "hello world!"
# echo "A"
# #docker compose -f track/base.yml -f track/prod.yml exec -T db echo "hello world!" || true
# output=$(docker compose -f track/base.yml -f track/prod.yml exec db echo "hello world!") || true
# echo "$output"
# echo "B"
# docker compose -f track/base.yml -f track/prod.yml exec -T db echo "hello world 2!"
# docker compose -f track/base.yml -f track/prod.yml exec -T db psql -U postgres -c "DROP DATABASE IF EXISTS trackdb;"
# docker compose -f track/base.yml -f track/prod.yml exec -T db psql -U postgres -c "CREATE DATABASE trackdb;"
# echo "Restoring from backup."
# docker compose -f track/base.yml -f track/prod.yml cp ./track/$BACKUP_FILE db:/backup
# docker compose -f track/base.yml -f track/prod.yml exec db psql -U postgres -d trackdb -f /backup
# echo "Bringing down db."
# docker compose -f track/base.yml -f track/prod.yml down
# echo "Bringing up all services."
# docker compose -f track/base.yml -f track/prod.yml up -d
# echo "Status:"
# docker compose -f track/base.yml -f track/prod.yml ps
# ENDSSH