Skip to content

Commit

Permalink
Adding restore for database
Browse files Browse the repository at this point in the history
  • Loading branch information
shaheislamdfe committed Nov 13, 2024
1 parent 43d36b1 commit 3a786bf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/backup_nightly_database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
options:
- staging
- production
- migration
backup-file:
description: |
Backup file name (without extension). Default is ecf_[env]_adhoc_YYYY-MM-DD. Set it explicitly when backing up a point-in-time (PTR) server. (Optional)
Expand Down Expand Up @@ -79,7 +80,7 @@ jobs:
with:
storage-account: ${{ env.STORAGE_ACCOUNT_NAME }}
resource-group: ${{ env.RESOURCE_GROUP_NAME }}
app-name: ${{ env.SERVICE_NAME }}-${{ env.DEPLOY_ENV }}
app-name: ${{ env.SERVICE_NAME }}-${{ env.DEPLOY_ENV }}-web
cluster: ${{ env.CLUSTER }}
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
backup-file: ${{ env.BACKUP_FILE }}.sql
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/restore_azure_database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Restore database from Azure storage

on:
workflow_dispatch:
inputs:
environment:
description: Environment to restore
required: true
default: development
type: choice
options:
- separation
- migration
- staging
- production
confirm-production:
description: Must be set to true if restoring production
required: true
default: "false"
type: choice
options:
- "false"
- "true"
backup-file:
description: Name of the backup file in Azure storage. e.g. afqts_prod_2024-08-09.sql.gz. The default value is today's scheduled backup.
type: string
required: false

env:
SERVICE_NAME: cpd-ecf
SERVICE_SHORT: cpdecf
TF_VARS_PATH: terraform/application/workspace_variables

jobs:
restore:
name: Restore AKS Database
if: ${{ inputs.environment != 'production' || (inputs.environment == 'production' && github.event.inputs.confirm-production == 'true' ) }}
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
concurrency: deploy_${{ inputs.environment }}

steps:
- uses: actions/checkout@v4
name: Checkout

- name: Set environment variables
run: |
source global_config/${{ inputs.environment }}.sh
tf_vars_file=${{ env.TF_VARS_PATH }}/${CONFIG}/variables.tfvars.json
echo "CONFIG=${CONFIG}" >> $GITHUB_ENV
echo "CLUSTER=$(jq -r '.cluster' ${tf_vars_file})" >> $GITHUB_ENV
echo "RESOURCE_GROUP_NAME=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-rg" >> $GITHUB_ENV
echo "STORAGE_ACCOUNT_NAME=${AZURE_RESOURCE_PREFIX}${SERVICE_SHORT}dbbkp${CONFIG_SHORT}sa" >> $GITHUB_ENV
echo "DB_SERVER=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-pg" >> $GITHUB_ENV
TODAY=$(date +"%F")
echo "BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql" >> $GITHUB_ENV
if [ "${{ inputs.backup-file }}" != "" ]; then
BACKUP_FILE=${{ inputs.backup-file }}
else
BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql.gz
fi
echo "BACKUP_FILE=$BACKUP_FILE" >> $GITHUB_ENV
- name: Restore ${{ inputs.environment }} postgres
uses: DFE-Digital/github-actions/restore-postgres-backup@master
with:
storage-account: ${{ env.STORAGE_ACCOUNT_NAME }}
resource-group: ${{ env.RESOURCE_GROUP_NAME }}
app-name: ${{ env.SERVICE_NAME }}-${CONFIG}-web
cluster: ${{ env.CLUSTER }}
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
backup-file: ${{ env.BACKUP_FILE }}
69 changes: 11 additions & 58 deletions .github/workflows/restore_snapshot_database.yml
Original file line number Diff line number Diff line change
@@ -1,72 +1,25 @@
name: Restore database from Azure storage

name: Restore Snapshot DB from production DB
on:
workflow_dispatch:
inputs:
environment:
description: Environment to restore
required: true
default: development
description: GitHub environment to backup and restore
type: choice
default: production
options:
- development
- test
- preprod
- production
confirm-production:
description: Must be set to true if restoring production
required: true
default: "false"
type: choice
options:
- "false"
- "true"
backup-file:
description: Name of the backup file in Azure storage. e.g. afqts_prod_2024-08-09.sql.gz. The default value is today's scheduled backup.
type: string
required: false

env:
SERVICE_NAME: cpd-ecf
SERVICE_SHORT: cpdecf
TF_VARS_PATH: terraform/application/workspace_variables

jobs:
restore:
name: Restore AKS Database
if: ${{ inputs.environment != 'production' || (inputs.environment == 'production' && github.event.inputs.confirm-production == 'true' ) }}
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
concurrency: deploy_${{ inputs.environment }}

backup-and-restore-production:
runs-on: ubuntu-20.04
environment: production
steps:
- uses: actions/checkout@v4
name: Checkout

- name: Set environment variables
run: |
source global_config/${{ inputs.environment }}.sh
tf_vars_file=${{ env.TF_VARS_PATH }}/${CONFIG}/variables.tfvars.json
echo "CONFIG=${CONFIG}" >> $GITHUB_ENV
echo "CLUSTER=$(jq -r '.cluster' ${tf_vars_file})" >> $GITHUB_ENV
echo "RESOURCE_GROUP_NAME=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-rg" >> $GITHUB_ENV
echo "STORAGE_ACCOUNT_NAME=${AZURE_RESOURCE_PREFIX}${SERVICE_SHORT}dbbkp${CONFIG_SHORT}sa" >> $GITHUB_ENV
echo "DB_SERVER=${AZURE_RESOURCE_PREFIX}-${SERVICE_SHORT}-${CONFIG_SHORT}-pg" >> $GITHUB_ENV
TODAY=$(date +"%F")
echo "BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql" >> $GITHUB_ENV
if [ "${{ inputs.backup-file }}" != "" ]; then
BACKUP_FILE=${{ inputs.backup-file }}
else
BACKUP_FILE=${SERVICE_SHORT}_${CONFIG_SHORT}_${TODAY}.sql.gz
fi
echo "BACKUP_FILE=$BACKUP_FILE" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v3

- name: Restore ${{ inputs.environment }} postgres
uses: DFE-Digital/github-actions/restore-postgres-backup@master
- name: Backup and restore snapshot
uses: ./.github/actions/backup-and-restore-snapshot-database
with:
storage-account: ${{ env.STORAGE_ACCOUNT_NAME }}
resource-group: ${{ env.RESOURCE_GROUP_NAME }}
app-name: ${{ env.SERVICE_NAME }}-${CONFIG}-web
cluster: ${{ env.CLUSTER }}
environment: production
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
backup-file: ${{ env.BACKUP_FILE }}

0 comments on commit 3a786bf

Please sign in to comment.