Skip to content

Commit

Permalink
Merge pull request #2 from mhaswell-bcgov/feature/DESCW-1780-db-backu…
Browse files Browse the repository at this point in the history
…p-restore

Created backup-restore.yaml Job Template to make restoring from a bac…
  • Loading branch information
mhaswell-bcgov authored Jan 26, 2024
2 parents ae8a3a1 + 7d76c48 commit 4955df1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.1.1
version: 2.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
14 changes: 14 additions & 0 deletions helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ oc project 12345-tools
# step 4 - Apply deployment file
oc apply -f deployments/openshift/notify-build.yaml
```

## Restoring from database backups
If database backups are enabled (`notify-bc.cronJob.enabled: true`), backups will be created automatically on a schedule set in `notify-bc.cronJob.schedule`. Backups are saved as date-stamped .gz (gzip) files to a PersistentVolumeClaim `...notify-bc-cronjob-mongodb-backup`.
To restore from a backup:
1. Copy your OpenShift login command and paste into a terminal. Switch to the desired project (`oc project ...`).
2. Find the `RELEASE_NAME` of the app you want to restore to. This should match the name of the PersistentVolumeClaim that contains the database backups, for example if your PVC is called `example-notifybc-dev-notify-bc-cronjob-mongodb-backup`, the `RELEASE_NAME` should be `example-notifybc-dev`.
3. Determine the `BACKUP_DATE`, the date of the backup you want to restore from. For example if you want to restore from a backup created on January 25th, 2024, the `BACKUP_DATE` should be `20240125`.
- Depending on the backup schedule there may be multiple backups performed per day in which case the latest backup from the given `BACKUP_DATE` will be used.
4. From the root of this project, run the following command:
```bash
oc process -f helm/deployments/openshift/backup-restore.yaml -p RELEASE_NAME="example-notifybc-dev" -p BACKUP_DATE="20240125" | oc apply -f -
```
(replacing the example values `example-notifybc-dev` and `20240125` with the values found earlier) to run the database restore job.
5. A Job and a Pod will be created to perform the restore process which can be monitored in OpenShift. Both will be cleaned up automatically a few minutes after the process is complete.
54 changes: 54 additions & 0 deletions helm/deployments/openshift/backup-restore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: template.openshift.io/v1
kind: Template
metadata:
name: notify-bc-mongodb-restore-template
parameters:
- name: RELEASE_NAME
displayName: "Release Name"
description: "The NotifyBC release name"
required: true
- name: MONGO_IMAGE
displayName: "Mongo Image"
description: "The image used by the mongodb pods. This should match the version used by the mongodb pods."
required: true
value: "docker.io/bitnami/mongodb:7.0.4-debian-11-r0"
- name: BACKUP_DATE
displayName: "Backup Date"
description: "The date of the backup file to restore from (YYYYMMDD format, eg. 20240125)"
required: true
objects:
- apiVersion: batch/v1
kind: Job
metadata:
name: ${RELEASE_NAME}-mongodb-restore-${BACKUP_DATE}
spec:
parallelism: 1
completions: 1
backoffLimit: 1
ttlSecondsAfterFinished: 100
template:
spec:
containers:
- name: mongodb
image: ${MONGO_IMAGE}
imagePullPolicy: IfNotPresent
volumeMounts:
- name: import
mountPath: /import
env:
- name: DATABASE_SERVICE_NAME
value: ${RELEASE_NAME}-mongodb-headless
- name: MONGODB_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
name: ${RELEASE_NAME}-mongodb
key: mongodb-root-password
command:
- /bin/bash
- -vc
- mongorestore --username=root --password="$MONGODB_ADMIN_PASSWORD" --host="rs0/$DATABASE_SERVICE_NAME" --gzip --drop --archive="`find /import -name "mongodb-backup-${BACKUP_DATE}*.gz" | tail -n 1`"
restartPolicy: Never
volumes:
- name: import
persistentVolumeClaim:
claimName: ${RELEASE_NAME}-notify-bc-cronjob-mongodb-backup

0 comments on commit 4955df1

Please sign in to comment.