0
votes

I use Azure CLI to export backup the databases in my resource group to the blobstorage, so i want to use same command on python script.

For example, I use the following command in Azure CLI to export the DB's in my resource group:

az sql db export -s (sql server) -n (database) -g (group) -p (password)
  -u login --storage-key " key "
  --storage-key-type
  --storage-uri (url blob)

How can I achieve this using a Python script instead?

2

2 Answers

0
votes

You can use REST API from Python to export an Azure SQL Database as a bacpac.

This is a sample request.

POST https://management.azure.com/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/sqlcrudtest-4799/providers/Microsoft.Sql/servers/sqlcrudtest-5961/databases/testdb/export?api-version=2014-04-01

Request body.

{
  "storageKeyType": "SharedAccessKey",
  "storageKey": "?sr=b&sp=rw&se=2018-01-01T00%3A00%3A00Z&sig=sdfsdfklsdjflSLIFJLSIEJFLKSDJFDd/%2wdfskdjf3%3D&sv=2015-07-08",
  "storageUri": "https://test.blob.core.windows.net/bacpacs/testbacpac.bacpac",
  "administratorLogin": "dummyLogin",
  "administratorLoginPassword": "Un53cuRE!",
  "authenticationType": "SQL"
}

Sample response.

{
  "id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/sqlcrudtest-4799/providers/Microsoft.Sql/servers/sqlcrudtest-5961/importExportOperationResult/f01d7bfe-7162-44e7-9350-f1c85ce83e4c",
  "name": "f01d7bfe-7162-44e7-9350-f1c85ce83e4c",
  "type": "Microsoft.Sql/servers/importExportOperationResults",
  "properties": {
    "requestId": "f01d7bfe-7162-44e7-9350-f1c85ce83e4c",
    "requestType": "Export",
    "queuedTime": "3/1/2017 12:14:25 AM",
    "lastModifiedTime": "3/1/2017 12:16:33 AM",
    "blobUri": "https://test.blob.core.windows.net/bacpacs/test.bacpac",
    "serverName": "test",
    "databaseName": "testdb",
    "status": "Completed",
    "errorMessage": null
  }
}
0
votes

Make sure to install the azure-mgmt-sql==0.16.0 package using pip.

Adjust parameters in the following code snippet and you should be good to go.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.sql import SqlManagementClient
from azure.mgmt.sql.models import StorageKeyType, AuthenticationType
from msrestazure.azure_exceptions import CloudError


TENANT_ID = '<your_tennant_id>'
CLIENT = '<your_client_id>'
KEY = '<your_client_key>'
SUBSCRIPTION_ID = '<your_subscription_id>'

credentials = ServicePrincipalCredentials(
    client_id=CLIENT,
    secret=KEY,
    tenant=TENANT_ID
)

sql_client = SqlManagementClient(credentials, SUBSCRIPTION_ID)

kwargs = dict()
kwargs['storage_key_type'] = 'StorageAccessKey'
kwargs['storage_key'] = '<your_storage_account_key'
kwargs['storage_uri'] = 'https://<name>.blob.core.windows.net/<container>/<name>.bacpac'
kwargs['administrator_login'] = '<admin_name>'
kwargs['administrator_login_password'] = '<admin_password>'
kwargs['authentication_type'] = 'SQL'

try:
    poller = sql_client.databases.export(
        database_name='<your_db_name>',
        server_name='<your_server_name>',
        resource_group_name='<your_rg_name>',
        storage_key_type='StorageAccessKey',
        storage_key='<your_storage_account_key',
        parameters=kwargs)

    print(poller.result())

except CloudError as err:
    print(err.error)
    print(err.message)