Skip to content

Commit

Permalink
feat(dev): Backup script
Browse files Browse the repository at this point in the history
  • Loading branch information
Veikkosuhonen committed Oct 24, 2023
1 parent 3c24ca8 commit 0db4d14
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
API_TOKEN=
TESTER_EMAILS=''
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.env
*.sql.gz
*.sql
backup

# Logs
logs
Expand Down
10 changes: 10 additions & 0 deletions scripts/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copy contents to .env in this directory and fill in the values

# Your ad-username
USERNAME=

APP_NAME=toppi
DB_NAME=toppi
CONTAINER_NAME=toppi_db
LOCAL_DB_USER=toppi
LOCAL_DB_NAME=toppi
39 changes: 39 additions & 0 deletions scripts/load_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
set -e

PROJECT_ROOT=$(git rev-parse --show-toplevel)
BACKUPS=$PROJECT_ROOT/backups/

echo "Found project root" $PROJECT_ROOT

# Get variables from .env file
export $(grep -v '^#' $PROJECT_ROOT/scripts/.env | xargs)

echo "Found env variables from .env file"

# Login to OC
oc login -u $USERNAME https://api.ocp-prod-0.k8s.it.helsinki.fi:6443

# Print login success
echo "Login successful"

# Read db password from user
DB_PASSWORD=""
echo "Enter the possu database password \n(see https://version.helsinki.fi/toska/dokumentaatio/-/blob/master/passwords.md?ref_type=heads)"
read -s DB_PASSWORD

# Download backup
oc exec -it $(oc get pods -l deployment=db-tools -o jsonpath='{.items[0].metadata.name}') -- pg_dump postgres://$DB_NAME:$DB_PASSWORD@possu-toska.it.helsinki.fi:5432/$APP_NAME | gzip -c > $DB_NAME.sql.gz

# Remove and recreate the database, then load the backup
docker-compose down db -v
docker-compose up -d db

# Wait for the database to be ready
while ! docker-compose exec db pg_isready --dbname=$DB_NAME; do
echo "Waiting for the database to be ready..."
sleep 1
done

# Load the backup
docker exec -i $CONTAINER_NAME /bin/bash -c "gunzip | psql -U $LOCAL_DB_USER -d $LOCAL_DB_NAME" < $DB_NAME.sql.gz

0 comments on commit 0db4d14

Please sign in to comment.