-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from pvcy/jc/restore-from-backup
Optionally restore database during startup
- Loading branch information
Showing
9 changed files
with
101 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,55 @@ | ||
name: Create Preview and Test Migration | ||
name: Standup Database and Test Migration | ||
on: | ||
pull_request: | ||
branches: [ main ] | ||
paths: | ||
- 'users-api/src/migrations/**' | ||
# paths: | ||
# - 'users-api/src/migrations/**' | ||
|
||
jobs: | ||
preview: | ||
check-migrations: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Login | ||
uses: okteto/[email protected] | ||
with: | ||
token: ${{ secrets.OKTETO_GCP_TOKEN }} | ||
url: ${{ secrets.OKTETO_GCP_URL }} | ||
|
||
- name: Deploy preview environment | ||
uses: okteto/[email protected] | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
name: anonymize-pr-${{ github.event.number }} | ||
scope: global | ||
timeout: 15m | ||
|
||
load-data: | ||
name: Load data from snapshot | ||
runs-on: ubuntu-latest | ||
steps: | ||
- id: load-data-from-snapshot | ||
run: | | ||
echo "some bash command goes here" | ||
env: | ||
GCP_PROEJCT_ID: "database-migration-anonymized" | ||
DB_HOST: "users-db" | ||
|
||
tests: | ||
name: Verify migration works | ||
runs-on: ubuntu-latest | ||
needs: preview | ||
steps: | ||
- id: run-migration | ||
name: Run Sequelize migration | ||
run: npx sequelize-cli db:migrate | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Authenticate with GCP | ||
uses: google-github-actions/[email protected] | ||
with: | ||
project_id: $GCP_PROEJCT_ID | ||
service_account_key: ${{ secrets.GCP_SA_KEY }} | ||
export_default_credentials: true | ||
|
||
- name: Pull PostgreSQL image from GCP | ||
run: | | ||
docker pull gcr.io/$GCP_PROEJCT_ID/$DB_HOST:latest | ||
- name: Start PostgreSQL container | ||
env: | ||
POSTGRES_PASSWORD: ${DB_PASSWORD:-dMVZFeBWLOzYRV71} | ||
POSTGRES_DB: "postgres" | ||
run: | | ||
docker run -d --name $DB_HOST -e POSTGRES_PASSWORD=$DB_PASSWORD: -e POSTGRES_DB=$POSTGRES_DB -p 5432:5432 gcr.io/$GCP_PROEJCT_ID/$DB_HOST:latest | ||
- name: Install Sequelize CLI and dependencies | ||
run: | | ||
npm install sequelize-cli | ||
npm install # Assuming you have package.json with necessary dependencies | ||
- name: Wait for Postgres to be ready | ||
run: | | ||
for i in {1..10}; do | ||
nc -z localhost 5432 && echo "Postgres is up" && break | ||
echo "Waiting for Postgres..." | ||
sleep 10 | ||
done | ||
# - name: Run Sequelize migrations | ||
# run: | | ||
# npx sequelize-cli db:migrate --config path-to-your-sequelize-config | ||
|
||
- name: Cleanup | ||
run: docker stop $DB_HOST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
FROM postgres:latest | ||
|
||
# SSH | ||
RUN apt-get update && apt-get install -y openssh-server | ||
RUN echo "root:Docker!" | chpasswd | ||
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config | ||
RUN mkdir /var/run/sshd | ||
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* | ||
|
||
EXPOSE 22 | ||
# Add the loading script to the container | ||
COPY ./db/restore-backup.sh /restore-backup.sh | ||
|
||
CMD service ssh start && docker-entrypoint.sh postgres | ||
# Make the script executable | ||
RUN chmod +x /restore-backup.sh | ||
|
||
# Use the script as the entrypoint | ||
ENTRYPOINT ["/restore-backup.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
# Start Postgres in the background to ensure it's ready to accept connections | ||
docker-entrypoint.sh postgres & | ||
|
||
# The process ID of the PostgreSQL process, so we can wait for it later | ||
POSTGRES_PID=$! | ||
|
||
# Wait for Postgres to be ready | ||
until pg_isready -h localhost -U $POSTGRES_USER; do | ||
sleep 1 | ||
done | ||
|
||
if [ "$RESTORE_FROM_BACKUP" == "True" ]; then | ||
# Download the dump from the public GCS bucket using curl | ||
echo "Download backup" | ||
curl -o /tmp/dump.backup $DB_BACKUP_URL | ||
|
||
# Restore the dump using pg_restore (or psql depending on the dump format) | ||
echo "Restore backup" | ||
pg_restore -U $POSTGRES_USER -d $POSTGRES_DB /tmp/dump.backup | ||
fi | ||
|
||
# Wait for the PostgreSQL process to complete | ||
wait $POSTGRES_PID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters