Often we need a fresh copy of the production database in another environment (eg DEV/TEST/UAT).
Previously this was a tedious task involving getting a backup file, copying it to another location, restoring the database. Here is a solution to automate this process using Azure Pipelines.
Given a production database exists in subscription 1
When we do a release of the Azure Pipeline named ‘Refresh Database – DEV’
Then an copy of production is available in the DEV environment in subscription 2
And permissions are correct for the DEV environment
For each environment that you wish to restore into create an Azure Pipeline with 3 stages.
Create variable groups that are scoped to specific stages
Each variable group contains deployment credentials that the specific stage will require to perform operations within the specific Azure Subscription.
Create a PowerShell task to run a script and pass it the information for the production environment.
This runs against the production environment and creates a blog storage container that holds the exported BACPAC
Under the ‘import’ stage create a task that will import the BACPAC from the storage container in the production subscription. This uses both production and the environment credentials.
Create a 3rd task in the ‘Sanitise’ stage.
This will scramble any information you do not want in that environment (eg emails).
Also remove any Production SQL user account and replace them with environment specific
Running the pipeline now copies the database to the DEV environment. Typically after this will run a software build which will automatically apply schema changes currently in DEV in the database. Happy restoring!