@@ -70,3 +70,121 @@ Update the root configuration `.env` file and point it to the Redhat files.
7070DB_CONTEXT=db/postgres/rhel8
7171DB_VOLUME=/var/lib/pgsql/data
7272```
73+
74+ ## Backup Commands
75+
76+ ``` bash
77+ # Backup and zip
78+ pg_dump -h postgres -U admin -C -Fc -v -d tno | gzip > /mnt/data/dev.tar.gz
79+
80+ # Unzip
81+ gzip -dk dev.tar.gz
82+
83+ # Copy file to local
84+ oc -n 9b301c-dev rsync psql-4-zdbv6:/mnt/data/dev.tar.gz /D/db
85+
86+ # Copy file to database server
87+ scp -v /D/db/dev.tar.gz jerfos_a@142.34.249.231:/u02/data/postgres
88+ ```
89+
90+ ## Backup Database and Restore to Remote Server
91+
92+ Here are the steps to backup a full database and migrate to another location.
93+
94+ First create run a container with the same Postgres version as the one you want to backup.
95+
96+ ``` bash
97+ # Create a volume for the database backup
98+ docker volume create postgres-backup
99+
100+ # Start the container
101+ docker run \
102+ --name postgres \
103+ -l 15.10 \
104+ -p 5432:5432 \
105+ -e POSTGRES_USER=admin \
106+ -e POSTGRES_PASSWORD=password \
107+ -e POSTGRES_DB=mmi \
108+ -v postgres-backup:/var/lib/postgresql/data \
109+ -d --rm \
110+ postgres:15.10
111+
112+ # Create variable for environment
113+ podenv=" dev"
114+
115+ # Map a port to the remote database in Openshift
116+ oc port-forward postgres-0 22222:5432 -n 9b301c-${podenv}
117+
118+ # SSH into the container
119+ docker exec -it postgres bash
120+
121+ # Move to shared volume
122+ cd /var/lib/postgresql/data
123+
124+ # Within the postgres container connect to the remote database
125+ psql -U admin -h host.docker.internal -p 22222 -d tno
126+ \q
127+
128+ # Start backup. This will take 10-30 minutes.
129+ pg_dump -h host.docker.internal -p 22222 -U admin -C -Fc -v -d tno > backup.sql
130+
131+ # Connect to the government VPN
132+ # Connect to the destination database.
133+ psql -U mmiadmin -h 142.34.249.231 -d mmi
134+
135+ # Clear out the database if one exists.
136+ \c mmi
137+ \d t
138+ drop schema public cascade;
139+ create schema public;
140+ grant all on schema public to postgres;
141+ grant all on schema public to mmiadmin;
142+ \q
143+
144+ # Restore the database to the new remote database. This will take 10-30 minutes.
145+ pg_restore -U mmiadmin -h 142.34.249.231 -d mmi -v -Fc backup.sql
146+
147+ # Exit the local container
148+ exit
149+
150+ # Stop the local postgres container
151+ docker stop postgres
152+
153+ # Remove the volume when done to recover space
154+ docker volume rm postgres-backup -f
155+ ```
156+
157+ Configure Openshift environment to use remote database
158+
159+ Create a ` .env ` file that will contain your database secrets.
160+ This is to ensure it does not get added to source code.
161+
162+ ``` bash
163+
164+ # Encode username and password
165+ echo " username" | base64
166+ echo " password" | base64
167+
168+ # Place encoded values into yaml and create secret in openshift
169+ oc create -f db-secret.yaml.env -n 9b301c-${podenv}
170+ ```
171+
172+ Update the API environment variables to use the new secret.
173+
174+ ``` bash
175+ # Update the API ConfigMap connection string
176+ # Old value = Host=postgres:5432;Database=tno;Include Error Detail=true;Log Parameters=true;
177+ oc patch -n 9b301c-${podenv} configmap api --type=' merge' -p ' { "data": { "CONNECTION_STRING": "Host=142.34.249.231:5432;Database=mmi;Include Error Detail=true;Log Parameters=true;" }}'
178+
179+ # Update the statefulset
180+ oc patch -n 9b301c-${podenv} sts/api -p ' { "spec": { "template": { "spec": { "containers": [{ "name": "api", "env": [{ "name": "DB_POSTGRES_USERNAME", "valueFrom": { "secretKeyRef": { "name": "montford", "key": "USERNAME" }}}]}]}}}}'
181+ oc patch -n 9b301c-${podenv} sts/api -p ' { "spec": { "template": { "spec": { "containers": [{ "name": "api", "env": [{ "name": "DB_POSTGRES_PASSWORD", "valueFrom": { "secretKeyRef": { "name": "montford", "key": "PASSWORD" }}}]}]}}}}'
182+
183+ # Rollout change to statefulset
184+ oc rollout restart sts/api -n 9b301c-${podenv}
185+ oc rollout latest dc/api-services -n 9b301c-${podenv}
186+
187+ # Update the deployment config
188+ oc patch -n 9b301c-${podenv} dc/api-services -p ' { "spec": { "template": { "spec": { "containers": [{ "name": "api-services", "env": [{ "name": "DB_POSTGRES_USERNAME", "valueFrom": { "secretKeyRef": { "name": "montford", "key": "USERNAME" }}}]}]}}}}'
189+ oc patch -n 9b301c-${podenv} dc/api-services -p ' { "spec": { "template": { "spec": { "containers": [{ "name": "api-services", "env": [{ "name": "DB_POSTGRES_PASSWORD", "valueFrom": { "secretKeyRef": { "name": "montford", "key": "PASSWORD" }}}]}]}}}}'
190+ ```
0 commit comments