-1

I am trying to backup an Azure sql database using sqlpackage within powershell. So far i have :

  $url =  az storage container generate-sas  --account-name <storage_account> --name <container> --permissions rwl  --expiry 2025-06-01T00:00:00Z  --output tsv

  $url2 = "https://<storage_account>.blob.core.windows.net/<container>/<file_name>.bacpac?" + $url 
  & "sqlpackage.exe"  /Action:Export  /SourceConnectionString:$env:ConnStr  /TargetFile:$url2

The problem is that it gives an error :

   *** The TargetFile argument must refer to a file with a '.bacpac' extension.

How can i fix this ?

4
  • 1
    The error indicates that the sqlpackage.exe command was expecting a specific format for the TargetFile argument, which must be a local file path ending with .bacpac. Commented Apr 14 at 2:18
  • yes , but from my research that is all i could get. Is the statement correct or do i need to change . if so what can i change it to ? Commented Apr 14 at 2:58
  • This question is similar to: make a backup for azure database. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 14 at 4:31
  • I dont think it is similar , i just need help in the command format ? Commented Apr 14 at 7:31

1 Answer 1

0

According to this Document,

/TargetFile argument expects a local file path, not a URL. It doesn't support exporting directly to a URL with a SAS token.

You can achieve by first export to local environment and upload the .bacpac to your Azure blob storage.

Command:

$localFile = "C:\temp\mybackup.bacpac"
$storageAccount = "<storage_account>"
$container = "<container>"
$fileName = "mybackup.bacpac"
$connectionString = $env:ConnStr

& "sqlpackage.exe" /Action:Export /SourceConnectionString:$connectionString /TargetFile:$localFile

az storage blob upload `
    --account-name $storageAccount`
    --sas-token $sastoken `
    --container-name $container `
    --name $fileName `
    --file $localFile `
    --overwrite

The above PowerShell script first exports an Azure SQL database to a local .bacpac file using sqlpackage.exe. It then uploads the .bacpac file to a specified Azure Blob Storage container using the Azure CLI, ensuring the file is overwritten if it already exists. This process enables a reliable backup of the database to cloud storage.

Alternatively, you can follow this link to back up the SQL database to blob storage.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.