Skip to content

Commit 1fa2177

Browse files
author
Neill Turner
committed
add workflow for aks database backup and restore
1 parent ffc7760 commit 1fa2177

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

.github/workflows/backup-db.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Backup database
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: Environment to backup
8+
required: true
9+
default: test
10+
type: choice
11+
options:
12+
- test
13+
- preprod
14+
- production
15+
backup-file:
16+
description: |
17+
Backup file name (without extension). Default is rsm_[env]_adhoc_YYYY-MM-DD. Set it explicitly when backing up a point-in-time (PTR) server. (Optional)
18+
required: false
19+
type: string
20+
default: default
21+
db-server:
22+
description: |
23+
Name of the database server. Default is the live server. When backing up a point-in-time (PTR) server, use the full name of the PTR server. (Optional)
24+
25+
# uncomment to enable after production migration
26+
#schedule:
27+
# - cron: "0 4 * * *" # 04:00 UTC
28+
29+
env:
30+
SERVICE_NAME: refer-serious-misconduct
31+
SERVICE_SHORT: rsm
32+
TF_VARS_PATH: terraform/application/config
33+
34+
jobs:
35+
backup:
36+
name: Backup database
37+
runs-on: ubuntu-latest
38+
environment:
39+
name: aks-${{ inputs.environment || 'production' }}
40+
env:
41+
DEPLOY_ENV: ${{ inputs.environment || 'production' }}
42+
BACKUP_FILE: ${{ inputs.backup-file || 'schedule' }}
43+
44+
steps:
45+
- uses: actions/checkout@v4
46+
47+
- uses: azure/login@v2
48+
with:
49+
creds: ${{ secrets.AZURE_CREDENTIALS }}
50+
51+
- name: Set environment variables
52+
run: |
53+
source global_config/${DEPLOY_ENV}.sh
54+
tf_vars_file=${TF_VARS_PATH}/${DEPLOY_ENV}.tfvars.json
55+
echo "CLUSTER=$(jq -r '.cluster' ${tf_vars_file})" >> $GITHUB_ENV
56+
echo "RESOURCE_GROUP_NAME=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-rg" >> $GITHUB_ENV
57+
echo "STORAGE_ACCOUNT_NAME=${AZURE_RESOURCE_PREFIX}${SERVICE_SHORT}dbbkp${CONFIG_SHORT}sa" >> $GITHUB_ENV
58+
TODAY=$(date +"%F")
59+
echo "DB_SERVER=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-pg" >> $GITHUB_ENV
60+
if [ "${BACKUP_FILE}" == "schedule" ]; then
61+
BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}
62+
elif [ "${BACKUP_FILE}" == "default" ]; then
63+
BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_adhoc_${TODAY}
64+
else
65+
BACKUP_FILE=${BACKUP_FILE}
66+
fi
67+
echo "BACKUP_FILE=${BACKUP_FILE}" >> $GITHUB_ENV
68+
echo "KEYVAULT_NAME=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-inf-kv" >> $GITHUB_ENV
69+
70+
- name: Fetch secrets from key vault
71+
uses: azure/CLI@v2
72+
id: key-vault-secrets
73+
with:
74+
inlineScript: |
75+
SLACK_WEBHOOK=$(az keyvault secret show --name "SLACK-WEBHOOK" --vault-name ${KEYVAULT_NAME} --query "value" -o tsv)
76+
echo "::add-mask::$SLACK_WEBHOOK"
77+
echo "SLACK_WEBHOOK=$SLACK_WEBHOOK" >> $GITHUB_OUTPUT
78+
79+
- name: Backup AKS ${{ env.DEPLOY_ENV }} postgres
80+
uses: DFE-Digital/github-actions/backup-postgres@master
81+
with:
82+
storage-account: ${{ env.STORAGE_ACCOUNT_NAME }}
83+
resource-group: ${{ env.RESOURCE_GROUP_NAME }}
84+
app-name: refer-serious-misconduct-${{ env.DEPLOY_ENV }}
85+
cluster: ${{ env.CLUSTER }}
86+
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
87+
backup-file: ${{ env.BACKUP_FILE }}.sql
88+
db-server-name: ${{ inputs.db-server }}
89+
slack-webhook: ${{ steps.key-vault-secrets.outputs.SLACK_WEBHOOK }}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Restore database from Azure storage
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: Environment to restore
8+
required: true
9+
default: test
10+
type: choice
11+
options:
12+
- test
13+
- preprod
14+
- production
15+
confirm-production:
16+
description: Must be set to true if restoring production
17+
required: true
18+
default: "false"
19+
type: choice
20+
options:
21+
- "false"
22+
- "true"
23+
backup-file:
24+
description: Name of the backup file in Azure storage. e.g. rsm_prod_2024-08-09.sql.gz. The default value is today's scheduled backup.
25+
type: string
26+
required: false
27+
28+
env:
29+
SERVICE_NAME: refer-serious-misconduct
30+
SERVICE_SHORT: rsm
31+
TF_VARS_PATH: terraform/application/config
32+
33+
jobs:
34+
restore:
35+
name: Restore AKS Database
36+
if: ${{ inputs.environment != 'production' || (inputs.environment == 'production' && github.event.inputs.confirm-production == 'true' ) }}
37+
runs-on: ubuntu-latest
38+
environment: aks-${{ inputs.environment }}
39+
concurrency: deploy_${{ inputs.environment }}
40+
41+
steps:
42+
- uses: actions/checkout@v4
43+
name: Checkout
44+
45+
- name: Set environment variables
46+
run: |
47+
source global_config/${{ inputs.environment }}.sh
48+
tf_vars_file=${{ env.TF_VARS_PATH }}/${{ inputs.environment }}.tfvars.json
49+
echo "CLUSTER=$(jq -r '.cluster' ${tf_vars_file})" >> $GITHUB_ENV
50+
echo "RESOURCE_GROUP_NAME=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-rg" >> $GITHUB_ENV
51+
echo "STORAGE_ACCOUNT_NAME=${AZURE_RESOURCE_PREFIX}${SERVICE_SHORT}dbbkp${CONFIG_SHORT}sa" >> $GITHUB_ENV
52+
echo "DB_SERVER=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-pg" >> $GITHUB_ENV
53+
TODAY=$(date +"%F")
54+
echo "BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql" >> $GITHUB_ENV
55+
if [ "${{ inputs.backup-file }}" != "" ]; then
56+
BACKUP_FILE=${{ inputs.backup-file }}
57+
else
58+
BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql.gz
59+
fi
60+
echo "BACKUP_FILE=$BACKUP_FILE" >> $GITHUB_ENV
61+
62+
- name: Restore ${{ inputs.environment }} postgres
63+
uses: DFE-Digital/github-actions/restore-postgres-backup@master
64+
with:
65+
storage-account: ${{ env.STORAGE_ACCOUNT_NAME }}
66+
resource-group: ${{ env.RESOURCE_GROUP_NAME }}
67+
app-name: ${{ env.SERVICE_NAME }}-${{ inputs.environment }}
68+
cluster: ${{ env.CLUSTER }}
69+
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
70+
backup-file: ${{ env.BACKUP_FILE }}

0 commit comments

Comments
 (0)