-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_db.sh
executable file
·50 lines (38 loc) · 1.4 KB
/
import_db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# EDIT DATABASE NAME TO CHOOSE WHICH ONE YOU WANT
DATABASE_NAME="parlameter_hrvaska"
K8S_NAMESPACE="parlameter-hrvaska"
SECRETS_NAME="parladata-credentials"
# DATABASE PASSWORD IS DYNAMICALLY RETRIEVED FROM THE CLUSTER
DATABASE_USER=$(kubectl get secret $SECRETS_NAME -n $K8S_NAMESPACE -o jsonpath="{.data.DJANGO_DATABASE_USERNAME}" | base64 --decode)
DATABASE_PASSWORD=$(kubectl get secret $SECRETS_NAME -n $K8S_NAMESPACE -o jsonpath="{.data.DJANGO_DATABASE_PASSWORD}" | base64 --decode)
echo
echo "PORT FORWARDING"
nohup kubectl port-forward pod/postgresql-15-0 54321:5432 --namespace=shared &>/dev/null &
# store the kubectl pid for later
KUBECTL_PID=$!
# wait for port forwarding to initialise
sleep 5
echo
echo "DUMPING DATABASE TO db.dump"
PGPASSWORD=$DATABASE_PASSWORD \
pg_dump -U $DATABASE_USER \
-f db.dump \
-d "$DATABASE_NAME" \
-p 54321 \
-h localhost
# echo
# echo "DROPPING THE DB VOLUME"
# docker-compose down -v
# docker-compose up -d
# sleep 5
# echo
# echo "LOADING DB INTO CONTAINER"
# docker container exec -i $(docker-compose ps -q db) psql -U postgres parladata < db.dump
# docker-compose down
echo "STOPPING PORT FORWARDING"
kill $KUBECTL_PID
echo
echo "ALL DONE, DATABASE DUMP SAVED to db.dump"
echo "DROPPING AND IMPORTING DB IS DISABLED, DO IT MANUALLY OR FIX THE SCRIPT :D"
# echo "ALL DONE, YOU CAN RUN docker-compose up AND/OR DELETE db.dump"