|
1 | 1 | #!/bin/bash |
2 | 2 | # |
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. |
7 | 12 |
|
8 | 13 | # Strict mode with verbose output |
9 | 14 | set -euxo pipefail |
10 | 15 |
|
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>" |
48 | 18 | exit 1 |
49 | 19 | fi |
50 | 20 |
|
51 | 21 | # Vars |
52 | | -NEW_DEPLOYMENT=${1} |
53 | | -OLD_DEPLOYMENT=${NEW_DEPLOYMENT}-prev |
| 22 | +OLD_DEPLOYMENT=${1} |
| 23 | +NEW_DEPLOYMENT=${2} |
54 | 24 | DUMP_FILE_PATH=/tmp/backup.dump |
55 | 25 |
|
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 |
62 | 27 | OLD_POD=$(oc get po -l deployment=${OLD_DEPLOYMENT} -o name | head -n 1 | sed 's|pod/||') |
63 | 28 | 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}" |
64 | 29 | oc cp ${OLD_POD}:${DUMP_FILE_PATH} ${DUMP_FILE_PATH} |
65 | 30 | ls -lh ${DUMP_FILE_PATH} |
66 | 31 |
|
67 | | -# Delete the previous deployment (optional, uncomment if desired) |
68 | | -# oc delete deployment "${OLD_DEPLOYMENT}" |
69 | | - |
70 | 32 | # Copy and restore dump to the new deployment |
71 | 33 | NEW_POD=$(oc get po -l deployment=${NEW_DEPLOYMENT} -o name | head -n 1 | sed 's|pod/||') |
72 | 34 | oc cp ${DUMP_FILE_PATH} ${NEW_POD}:${DUMP_FILE_PATH} -c fom |
|
0 commit comments