diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..8e10c0d --- /dev/null +++ b/.env.template @@ -0,0 +1,2 @@ +API_TOKEN= +TESTER_EMAILS='' diff --git a/.gitignore b/.gitignore index 616218e..1a76742 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .env +*.sql.gz +*.sql +backup # Logs logs diff --git a/scripts/.env.template b/scripts/.env.template new file mode 100644 index 0000000..bb5bf89 --- /dev/null +++ b/scripts/.env.template @@ -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 diff --git a/scripts/load_backup.sh b/scripts/load_backup.sh new file mode 100755 index 0000000..846f7b0 --- /dev/null +++ b/scripts/load_backup.sh @@ -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