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

9 thoughts on “How to backup database in Azure devops pipeline”

      1. I saw that now 🙂

        I have a similar thing, but it all happens inside SQL Server as a job where I take a bak-file and restore it and update users and update site settings and all.

        I also download all VPP-files from Azure with AzCopy and I do this every night so every day I have an environment that looks like yesterday production.

        One thing I see in your SQL script is that you are missing av very important thing since it is a onPrem database and that is to take a Copy Only database backup.

        If you do not take CopyOnly then you will mess with the maintenance job for backups.
        Read more about it here:
        https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/copy-only-backups-sql-server?view=sql-server-2017

        1. Out of simplicity, to run SQL module i would need to install an agent on the server running SQL Server too, and the SQL server is not open to internet. With this example I only need an agent on the webserver. It is a matter of taste or practical options.

Leave a Reply

Your email address will not be published. Required fields are marked *