Skip to content

Commit 3d277cd

Browse files
committed
Pare down dbUpgrade.sh
1 parent 52fe0a7 commit 3d277cd

File tree

1 file changed

+14
-52
lines changed

1 file changed

+14
-52
lines changed

db/dbUpgrade.sh

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,34 @@
11
#!/bin/bash
22
#
3-
# Database Upgrade Script for OpenShift
4-
# Requirements
5-
# - Database template with a PersistentVolumeClaim with a different name from the existing one
6-
# - Database template loading a newer version of the database image
3+
# Database Upgrade/Migration Script for OpenShift
4+
# Usage:
5+
# ./dbUpgrade.sh <old-deployment-name> <new-deployment-name>
6+
#
7+
# This script takes a database dump from the OLD_DEPLOYMENT and restores it into the NEW_DEPLOYMENT.
8+
# Requirements:
9+
# - Both deployments must have running pods with the correct labels.
10+
# - The database template should use a PersistentVolumeClaim with a different name for the new deployment.
11+
# - The new deployment should be ready to accept a restore.
712

813
# Strict mode with verbose output
914
set -euxo pipefail
1015

11-
# Function - clone/replace the current database with a new name
12-
rename_deployment() {
13-
local old_deployment="$1"
14-
local new_deployment="$2"
15-
local manifest="/tmp/${old_deployment}_$(date +%Y%m%d).json" # Save in /tmp
16-
17-
# Check if the old deployment exists
18-
if ! oc get deployment "${old_deployment}" &>/dev/null; then
19-
echo "Deployment '${old_deployment}' does not exist. It must have already been renamed."
20-
return 0
21-
fi
22-
23-
# Export the existing deployment to JSON, update name, and clean metadata
24-
oc get deployment "${old_deployment}" -o json \
25-
| jq 'del(
26-
.metadata.uid,
27-
.metadata.resourceVersion,
28-
.metadata.selfLink,
29-
.metadata.creationTimestamp,
30-
.metadata.generation,
31-
.metadata.managedFields,
32-
.status
33-
) | .metadata.name = "'${new_deployment}'"' \
34-
> "${manifest}"
35-
36-
# Delete the old deployment
37-
oc delete deployment "${old_deployment}"
38-
39-
# Apply the new deployment
40-
oc apply -f "${manifest}"
41-
42-
# Clean up the temporary manifest file
43-
rm -f "${manifest}"
44-
}
45-
46-
if [[ $# -lt 1 ]]; then
47-
echo "Usage: $0 <deployment-name>"
16+
if [[ $# -lt 2 ]]; then
17+
echo "Usage: $0 <old-deployment-name> <new-deployment-name>"
4818
exit 1
4919
fi
5020

5121
# Vars
52-
NEW_DEPLOYMENT=${1}
53-
OLD_DEPLOYMENT=${NEW_DEPLOYMENT}-prev
22+
OLD_DEPLOYMENT=${1}
23+
NEW_DEPLOYMENT=${2}
5424
DUMP_FILE_PATH=/tmp/backup.dump
5525

56-
# Rename the deployment
57-
rename_deployment "${NEW_DEPLOYMENT}" "${OLD_DEPLOYMENT}"
58-
59-
exit 0
60-
61-
# Create and copy dump from the previous deployment
26+
# Create and copy dump from the old deployment
6227
OLD_POD=$(oc get po -l deployment=${OLD_DEPLOYMENT} -o name | head -n 1 | sed 's|pod/||')
6328
oc exec -it deployment/${OLD_DEPLOYMENT} -- bash -c "pg_dump -U \${POSTGRES_USER} -d \${POSTGRES_DB} -Fc -f ${DUMP_FILE_PATH} && ls -lh ${DUMP_FILE_PATH}"
6429
oc cp ${OLD_POD}:${DUMP_FILE_PATH} ${DUMP_FILE_PATH}
6530
ls -lh ${DUMP_FILE_PATH}
6631

67-
# Delete the previous deployment (optional, uncomment if desired)
68-
# oc delete deployment "${OLD_DEPLOYMENT}"
69-
7032
# Copy and restore dump to the new deployment
7133
NEW_POD=$(oc get po -l deployment=${NEW_DEPLOYMENT} -o name | head -n 1 | sed 's|pod/||')
7234
oc cp ${DUMP_FILE_PATH} ${NEW_POD}:${DUMP_FILE_PATH} -c fom

0 commit comments

Comments
 (0)