Reader small image

You're reading from  Professional Azure SQL Managed Database Administration - Third Edition

Product typeBook
Published inMar 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801076524
Edition3rd Edition
Languages
Tools
Concepts
Right arrow
Authors (2):
Ahmad Osama
Ahmad Osama
author image
Ahmad Osama

Ahmad Osama works for Pitney Bowes Pvt. Ltd. as a technical architect and is a former Microsoft Data Platform MVP. In his day job, he works on developing and maintaining high performant, on-premises and cloud SQL Server OLTP environments as well as deployment and automating tasks using PowerShell. When not working, Ahmad blogs at DataPlatformLabs and can be found glued to his Xbox.
Read more about Ahmad Osama

Shashikant Shakya
Shashikant Shakya
author image
Shashikant Shakya

Shashikant Shakya is a passionate technologist with decades of experience in the sphere of databases. He works for Microsoft as a senior support engineer. In his day job, he works on Azure SQL Database, Azure Database for MySQL, and PostgreSQL. Apart from his work, he is a regular speaker at the SQLBangalore community group.
Read more about Shashikant Shakya

View More author details
Right arrow

5. Restoration

In the previous chapter, we talked about performing database backups for Azure SQL Database and Azure SQL Managed Instance. Similar to a database backup, a restore is another housekeeping activity that a DBA performs, whether to move or copy a database from one server to another or to recover from an outage or an accidental update/delete operation. In this chapter, we'll look at different ways to restore an Azure SQL Database and an SQL Managed Instance database.

Azure SQL Database and SQL Managed Instance have the following restore options:

  • Point-in-time restore
  • Restore from long-term backup
  • Restore a deleted database
  • Geo-restore a database
  • Import a BACPAC – only for Azure SQL Database

Figure 5.1 shows the types of backup available with Azure SQL Database, the different restore options, and the ways to restore a database:

Database restoration types

Figure 5.1: Types of database restore

A restore can be performed...

Restore types

This section discusses the different types of restore available in Azure SQL Database and SQL Managed Instance.

Point-in-time restore

Point-in-time restore (PITR) isn't new in the world of SQL Server. On-premises SQL servers allow you to restore a database to a particular point in time by specifying the point-in-time option when restoring the database using the restore command.

Azure SQL Database and SQL Managed Instance backups are managed by the automatic backups feature of the PaaS offering. Point-in-time restores can only be performed using the Azure portal, PowerShell, the Azure CLI, or the Azure SDK. PITR uses the automatic Full, Differential, and Log backups.

For all new, restored, and copied databases on Azure SQL Database and SQL Managed Instance, the default retention period is 7 days. The retention period can be adjusted for existing or deleted databases (the latter is only available on SQL Managed Instance):

  • For active databases on...

Restoring deleted databases

Azure allows you to restore a deleted database to the time it was deleted, or to any time within the retention period. You can select the deleted database you wish to restore from the pool of deleted databases. You are able to restore a deleted database because the automatic backups are saved for a given retention period that depends on the service tier.

Restoring a deleted database on Azure SQL Database using the Azure portal

Let's restore a deleted database using the Azure portal:

  1. Log in to the Azure portal using your Azure credentials. Open All resources from the left-hand navigation pane.
  2. From the All resources pane, open the Azure SQL server that hosts the deleted database you wish to restore.
  3. In the Azure SQL Server pane, from the Settings section, select Deleted databases:
    Checking the deleted databases

    Figure 5.19: Navigating to deleted databases

  4. The Deleted databases pane lists the databases and their deletion times. Select the toystore database...

Geo-restoring databases

A geo-restore allows you to restore a database from a geo-redundant backup to any available Azure SQL servers and SQL Managed Instance, irrespective of the region.

The automatic backups of Azure SQL Database and SQL Managed Instance are geo-redundant by default, and are copied to a paired Azure region as and when they are taken. There is a maximum delay of one hour when copying a database to a paired geographical location. Therefore, in the case of a disaster, there can be up to an hour of data loss:

Geo-replication to a different geographical location

Figure 5.26: Geo-replication to a different geographical location

Geo-restore can be used to recover a database if an entire region is unavailable because of a disaster:

Using geo-restore in the case of the unavailability of an entire region

Figure 5.27: Use of geo-restore in the case of unavailability of an entire region

The most recent full and differential backups are used to perform a geo-restore.

Geo-restore doesn't support PITR. It is the most basic disaster...

Importing a database (Azure SQL Database only)

You can import a database into an Azure SQL server from a BACPAC or DACPAC file kept in Azure Storage. The import operation will create a new Azure SQL database from the BACPAC file.

The BACPAC file can be imported to any of the available Azure SQL servers in any given region. This can be useful for quickly creating new test environments.

The import can be done through the Azure portal, PowerShell, the Azure CLI, or the Azure SDK. Let's learn how to import a database from a BACPAC file kept in Azure Storage. Open the Azure portal, go to https://portal.azure.com, and log in with your Azure credentials:

  1. From the left-hand navigation pane, open the All resources section. Select the Azure SQL server you wish to import the database to.
  2. In the Azure SQL Server Overview pane, select Import database:
    Importing a database

    Figure 5.35: Selecting the Import database option

  3. In the Import database pane, under Subscription, select your Azure subscription...

Activity: Performing a PITR for an Azure SQL Database with PowerShell

Consider the following scenario: Mike is a new DBA, so his trainer is aware that there might be some misses at his end. Therefore, his trainer wants to configure PITR on the databases that Mike is working on. In this section, we will perform a PITR using PowerShell by following these steps:

  1. Press Windows key + R to open the Run dialog box. In the Run dialog box, type powershell ise to open a new PowerShell editor window.
  2. In PowerShell, click File from the top menu and then select New to create a new PowerShell script file:
    Creating a new PowerShell script

    Figure 5.40: Creating a new PowerShell script

  3. In the new PowerShell script file, copy and paste in the code from the following step.
  4. Define the PowerShell script parameters. The parameters are self-explanatory:
    param(
                [Parameter(Mandatory=$true)]
              ...

Activity: Performing a geo-restore of an Azure SQL Database with PowerShell

Let's once again consider our example of ToyStore Ltd. Mike is aware that, although on the cloud, his data is still physically stored on servers. Hence, there is a possibility of data loss due to natural disasters. In these instances, he would have to perform a geo-restore operation. This section makes use of PowerShell to perform a geo-restore:

  1. Press Windows key + R to open the Run dialog box. In the Run dialog box, type powershell ise to open a new PowerShell editor window.
  2. In PowerShell, click File from the top menu, and then select New to create a new PowerShell script file:
    Creating a new PowerShell script

    Figure 5.42: Creating a new PowerShell script

  3. In the new PowerShell script file, copy the code as instructed in the following steps.

    Define the PowerShell script parameters. The parameters are self-explanatory:

    param(
               [Parameter(Mandatory=$true)]
     ...

Activity: Performing Point-In-Time restore for SQL Managed Instance with PowerShell

In this two-part activity, we will be performing a point-in-time restore of a database in SQL Managed Instance using PowerShell and then a cross-instance restore of a database (from one SQL Managed Instance to another SQL Managed Instance).

Part 1: Restoring a database to a point in time using PowerShell on one managed instance

To perform a point-in-time restore operation on the same SQL Managed Instance using PowerShell commands, please follow these steps:

  1. Press Windows key + R to open the Run dialog box. In the Run dialog box, type powershell ise to open a new PowerShell editor window.
  2. In PowerShell, click File from the top menu and then select New to create a new PowerShell script file:
    Creating a new PowerShell script

    Figure 5.44: Creating a new PowerShell script

  3. In the new PowerShell script file, copy in the code from the following step.
  4. Define the PowerShell script parameters. The parameters are self...

Activity: Geo-restoring a database hosted on SQL Managed Instance using the Az PowerShell module

Geo-restoring allows you to recover a database to a different region. This can be used as a database recovery solution, but the recovery time depends on the database size. Geo-backups are only available when SQL Managed Instance is configured with geo-redundant backup storage.

Let's quickly go through how to perform a geo-restore using PowerShell.

This activity is similar to the previous point-in-time restore we did using the Az PowerShell module, so all the steps are not covered here. We are only covering changes in the script.

SQLMI_GeoRestore.ps1 is available in the source code and most of the code is similar to the point-in-time restore activity.

In our geo-restore PowerShell script, we are using the same Restore-AzSqlInstanceDatabase command, but instead of using the -FromPointInTimeBackup parameter, we are using the -FromGeoBackup parameter.

The following is a...

Activity: Restoring a deleted database on SQL Managed Instance using PowerShell

Restoring a deleted database on a managed instance can be done using the Azure portal, as we saw in the demonstrations earlier, and PowerShell. Cross-instance restoration for deleted databases can only be done using PowerShell.

Like active database point-in-time restore, cross-instance restore for deleted databases is only available in the same region and same subscription.

To restore deleted databases using PowerShell, you can use the following PowerShell scripts; these scripts are similar to the point-in-time restore and geo-restore scripts.

  • SQLMI_DeletedDatabaseOnSameInstancePITR.ps1
  • SQLMI_DeletedDatabaseOnCrossInstancePITR.ps1

This concludes the chapter. As we have seen, Azure SQL Managed Instance supports restoration for all possible scenarios. Use these PaaS capabilities to easily restore your managed databases to the same instance or across instances in the same region and...

Summary

Restoring an Azure SQL database and Azure SQL Managed Instance is different from restoring an on-premises SQL Server database. In this chapter, you learned about the following restore options:

  • Point-in-time restore
  • Restoring a deleted database
  • Geo-restoring a database
  • Restoring a database from a long-term backup
  • Importing a BACPAC file

Each of these options can be leveraged in different scenarios. For example, a PITR will help you recover from a corrupt database or accidental deletion in Azure SQL Database and SQL Managed Instance, whereas importing a BACPAC file in Azure SQL Database helps you set up a development environment with the same schema and data across development, testing, and integration.

In the next chapter, we will look at the security mechanisms available to secure an Azure SQL database and SQL Managed Instance.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Professional Azure SQL Managed Database Administration - Third Edition
Published in: Mar 2021Publisher: PacktISBN-13: 9781801076524
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Ahmad Osama

Ahmad Osama works for Pitney Bowes Pvt. Ltd. as a technical architect and is a former Microsoft Data Platform MVP. In his day job, he works on developing and maintaining high performant, on-premises and cloud SQL Server OLTP environments as well as deployment and automating tasks using PowerShell. When not working, Ahmad blogs at DataPlatformLabs and can be found glued to his Xbox.
Read more about Ahmad Osama

author image
Shashikant Shakya

Shashikant Shakya is a passionate technologist with decades of experience in the sphere of databases. He works for Microsoft as a senior support engineer. In his day job, he works on Azure SQL Database, Azure Database for MySQL, and PostgreSQL. Apart from his work, he is a regular speaker at the SQLBangalore community group.
Read more about Shashikant Shakya