-
Notifications
You must be signed in to change notification settings - Fork 6
211 lines (181 loc) · 7.33 KB
/
database-backup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
name: Backup Database to Azure Storage
concurrency: build_and_deploy_main
on:
workflow_dispatch:
inputs:
overwriteThisMorningsBackup:
required: true
type: boolean
default: false
restoreToProductionDataEnv:
required: true
type: boolean
default: false
restoreToProductionAnalysisEnv:
required: true
type: boolean
default: false
restoreToStagingEnv:
required: true
type: boolean
default: false
schedule: # 03:00 UTC
- cron: '0 3 * * *'
jobs:
backup:
name: Backup AKS Database (production)
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.overwriteThisMorningsBackup == 'true') }}
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11.10
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
name: Checkout
- name: Set KV environment variables
run: |
tf_vars_file=terraform/aks/workspace-variables/production.tfvars.json
echo "key_vault_name=$(jq -r '.key_vault_name' ${tf_vars_file})" >> $GITHUB_ENV
echo "key_vault_infra_secret_name=$(jq -r '.key_vault_infra_secret_name' ${tf_vars_file})" >> $GITHUB_ENV
echo "namespace=$(jq -r '.namespace' ${tf_vars_file})" >> $GITHUB_ENV
echo "cluster=$(jq -r '.cluster' ${tf_vars_file})" >> $GITHUB_ENV
echo "app_environment=$(jq -r '.app_environment' ${tf_vars_file})" >> $GITHUB_ENV
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS_PRODUCTION }}
- name: Install kubectl
uses: DFE-Digital/github-actions/set-kubectl@master
- uses: DFE-Digital/github-actions/set-kubelogin-environment@master
with:
azure-credentials: ${{ secrets.AZURE_CREDENTIALS_PRODUCTION }}
- name: K8 setup
shell: bash
run: |
make ci production get-cluster-credentials
make install-konduit
- name: Setup postgres client
uses: DFE-Digital/github-actions/install-postgres-client@master
- name: Set environment variable
run: echo "BACKUP_FILE_NAME=register_prod_$(date +"%F")" >> $GITHUB_ENV
- name: Backup Prod DB
run: |
bin/konduit.sh -t 7200 register-production -- pg_dump -E utf8 --clean --if-exists --no-owner --verbose --no-password -f ${BACKUP_FILE_NAME}.sql
tar -cvzf ${BACKUP_FILE_NAME}.tar.gz ${BACKUP_FILE_NAME}.sql
- name: Set Connection String
run: |
STORAGE_CONN_STR="$(az keyvault secret show --name REGISTER-BACKUP-STORAGE-CONNECTION-STRING-AKS --vault-name ${{ env.key_vault_name }} | jq -r .value)"
echo "::add-mask::$STORAGE_CONN_STR"
echo "STORAGE_CONN_STR=$STORAGE_CONN_STR" >> $GITHUB_ENV
- name: Upload Backup to Azure Storage
run: |
az storage blob upload --container-name database-backup \
--file ${BACKUP_FILE_NAME}.tar.gz --name ${BACKUP_FILE_NAME}.tar.gz --overwrite \
--connection-string '${{ env.STORAGE_CONN_STR }}'
rm ${BACKUP_FILE_NAME}.tar.gz
- name: Restore backup to aks productiondata database
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.restoreToProductionDataEnv == 'true') }}
shell: bash
run: |
bin/konduit.sh -i ${{ env.BACKUP_FILE_NAME }}.sql -t 7200 register-productiondata -- psql
- name: Restore backup to aks analysis database
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.restoreToProductionAnalysisEnv == 'true') }}
shell: bash
run: |
bin/konduit.sh -i ${{ env.BACKUP_FILE_NAME }}.sql -p ANALYSIS_DATABASE_URL -t 7200 register-production -- psql
bin/konduit.sh -p ANALYSIS_DATABASE_URL -t 300 register-production -- psql -c 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "${{ env.SQLPAD_USER }}"'
bin/konduit.sh -p ANALYSIS_DATABASE_URL -t 300 register-production -- psql -c 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO "${{ env.SQLPAD_USER }}"'
env:
SQLPAD_USER: ${{ secrets.SQLPAD_USER }}
- name: Disk cleanup
shell: bash
run: |
sudo rm -rf /usr/local/lib/android || true
sudo rm -rf /usr/share/dotnet || true
sudo rm -rf /opt/ghc || true
- name: Create local Sanitised Database
run: |
createdb ${DATABASE_NAME} && psql -f ${{ env.BACKUP_FILE_NAME }}.sql -d ${DATABASE_NAME}
env:
DATABASE_NAME: register_trainee_teachers
PGUSER: postgres
PGPASSWORD: postgres
PGHOST: localhost
PGPORT: 5432
- name: Remove backup file
shell: bash
run: |
rm ${{ env.BACKUP_FILE_NAME }}.sql
- name: Sanitise the local Database
run: |
psql -d ${DATABASE_NAME} -f db/scripts/sanitise.sql
env:
DATABASE_NAME: register_trainee_teachers
PGUSER: postgres
PGPASSWORD: postgres
PGHOST: localhost
PGPORT: 5432
- name: Dump the Sanitised Database
run: |
pg_dump --encoding utf8 --compress=1 --clean --no-owner --if-exists -d ${DATABASE_NAME} -f backup_sanitised.sql.gz
env:
DATABASE_NAME: register_trainee_teachers
PGUSER: postgres
PGPASSWORD: postgres
PGHOST: localhost
PGPORT: 5432
- name: Upload Sanitised Backup
uses: actions/upload-artifact@v4
with:
name: backup_sanitised
path: backup_sanitised.sql.gz
retention-days: 3
- name: Check for Failure
uses: ./.github/actions/send-slack-notification/
if: ${{ failure() }}
with:
slack-title: Backup Failure
slack-message: ':alert: Backup failure :sadparrot:'
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}
restore_staging:
needs: [backup]
name: restore Database (staging)
if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && github.event.inputs.restoreToStagingEnv == 'true') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
name: Checkout
- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS_STAGING }}
- name: Download Sanitised Backup
uses: actions/download-artifact@v4
with:
name: backup_sanitised
- name: Install kubectl
uses: DFE-Digital/github-actions/set-kubectl@master
- uses: DFE-Digital/github-actions/set-kubelogin-environment@master
with:
azure-credentials: ${{ secrets.AZURE_CREDENTIALS_STAGING }}
- name: K8 setup
shell: bash
run: |
make ci staging get-cluster-credentials
make install-konduit
- name: Restore backup to aks env database
shell: bash
run: |
bin/konduit.sh -i backup_sanitised.sql.gz -c -t 7200 register-staging -- psql
- name: Check for Failure
uses: ./.github/actions/send-slack-notification/
if: ${{ failure() }}
with:
slack-title: Staging Restore Failure
slack-message: ':alert: Daily restore failure for staging :sadparrot:'
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}