Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 6.81 KB

sql-database-export-powershell.md

File metadata and controls

115 lines (81 loc) · 6.81 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.custom ms.devlang ms.date ms.author ms.workload ms.topic ms.tgt_pltfrm
PowerShell: Export an Azure SQL database to a BACPAC file | Microsoft Docs
Export an Azure SQL database to a BACPAC file by using PowerShell
sql-database
stevestein
jhubbard
9439dd83-812f-4688-97ea-2a89a864d1f3
sql-database
migrate and move
NA
02/07/2017
sstein
data-management
article
NA

Export an Azure SQL database or a SQL Server to a BACPAC file by using PowerShell

This article provides directions for exporting your Azure SQL database or a SQL Server database to a BACPAC file (stored in Azure Blob storage) using PowerShell. For an overview of exporting to a BACPAC file, see Export to a BACPAC.

Note

You can also export your Azure SQL database file to a BACPAC file using the Azure portal, SQL Server Management Studio, or SQLPackage.

Prerequisites

To complete this article, you need the following:

[!INCLUDE Start your PowerShell session]

Export your database

The New-AzureRmSqlDatabaseExport cmdlet submits an export database request to the service. Depending on the size of your database, the export operation may take some time to complete.

Important

To guarantee a transactionally consistent BACPAC file, you should first create a copy of your database, and then export the database copy.

 $exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
   -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
   -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

Monitor the progress of the export operation

After running New-AzureRmSqlDatabaseExport, you can check the status of the request by running Get-AzureRmSqlDatabaseImportExportStatus. Running this immediately after the request usually returns Status : InProgress. When you see Status: Succeeded the export is complete.

Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink

Export SQL database example

The following example exports an existing SQL database to a BACPAC and then shows how to check the status of the export operation.

To run the example, there are a few variables you need to replace with the specific values for your database and storage account. In the Azure portal, browse to your storage account to get the storage account name, blob container name, and key value. You can find the key by clicking Access keys on your storage account blade.

Replace the following VARIABLE-VALUES with values for your specific Azure resources. The database name is the existing database you want to export.

$subscriptionId = "YOUR AZURE SUBSCRIPTION ID"

Login-AzureRmAccount
Set-AzureRmContext -SubscriptionId $subscriptionId

# Database to export
$DatabaseName = "DATABASE-NAME"
$ResourceGroupName = "RESOURCE-GROUP-NAME"
$ServerName = "SERVER-NAME"
$serverAdmin = "ADMIN-NAME"
$serverPassword = "ADMIN-PASSWORD" 
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword

# Generate a unique filename for the BACPAC
$bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyyMMddHHmm") + ".bacpac"

# Storage account info for the BACPAC
$BaseStorageUri = "https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME/"
$BacpacUri = $BaseStorageUri + $bacpacFilename
$StorageKeytype = "StorageAccessKey"
$StorageKey = "YOUR STORAGE KEY"

$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
   -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
   -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password
$exportRequest

# Check status of the export
Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink

Automate export using Azure Automation

Azure SQL Database Automated Export is now in preview and will be retired on March 1, 2017. Starting December 1, 2016, you will no longer be able to configure automated export on any SQL database. All your existing automated export jobs will continue to work until March 1, 2017. After December 1, 2016, you can use long-term backup retention or Azure Automation to archive SQL databases periodically using PowerShell periodically according to a schedule of your choice. For a sample script, you can download the sample script from Github.

Next steps

Additional resources