How to backup database in Azure devops pipeline

A devops story of taking a backup before you release to production.

Published 16th January 2019
Versions: OnPrem MS SQL Server 2005-2016

This blog post is part of a series of posts “A devops story – from Azure to on premise CI/CD” which will be published in future posts.

NOTE! this is one way of doing backup in a devops context, it is not an Azure official best practice. This post is meant as an inspiration for your implementation.

Even if this example is based on an “on premise” implementation, it could be implemented in a cloud scenario.

Scenario: In a devops pipeline, before releasing your artifact/code to Staging and Production, you want to take a database backup.

Prerequisites:

  • You have a build in Azure Devops or Github
  • You have a Release with x tasks
  • You have a windows agent for on prem commands

Main Concept

Before deploy, request an url on site (/DatabaseBackupBeforeRelease), that triggers a SQL stored proc in database, that in turn takes an instant backup file (.bak)

How?

  1. Use command line task to trigger a power shell script
  2. Power shell script to request an url
  3. Http trigger that trigger the stored procedure

Azure Devops Release Tasks

I’m using a the command line task to trigger the Power Shell script:

*Important* use Version 1

Power Shell script

We are using a local PS script to request the database backup to the application. We do run this script localy because we wanted to secure the url by IP restrictions. It would be posible to run the PS script inline in an PS task in Azure devops.

 

C# MVC code

API call example:

IMPORTANT: SECURING the MVC Request

I secured the the path /DatabaseBackupBeforeRelease by adding a folder “DatabaseBackupBeforeRelease”, and in “IIS/IP Address and Domain Restrictions” adding an IP restriction.

SQL Stored Procedure

Install this stored procedure in your database, which executes the backup with SQL.

The SQL procedure is taking three parameters:

  1. databaseName,
  2. dir to save the backup,
  3. Version > to version the database

SEO Terms
  • Automatic take database backup on release
  • backup database to disk example

About the author

Luc Gosso
– Independent Senior Web Developer
working with Azure and Episerver

Twitter: @LucGosso
LinkedIn: linkedin.com/in/luc-gosso/
Github: github.com/lucgosso

Like it? please up vote
0