Created
September 18, 2019 02:26
-
-
Save GustavoAmerico/81cf97e4ce43baf1a62945ba7bd73eec to your computer and use it in GitHub Desktop.
Script para executar a exportação do Azure SQL Database para um Azure Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Esse script pode ser utilizado em tarefa do AzureDevOps para executar o backup da base de dados dentro do processo de Build ou Release | |
param( | |
$BaseStorageUri = "https://<your-storage>.blob.core.windows.net/backup-database", | |
$StorageKey = "<Shared key>", | |
$DatabaseName = @( "db1","db2","db3"), | |
$ResourceGroupName = "<database-resource-group-name>", | |
$ServerName = "<database-server-name>", | |
$serverAdmin = "<database-user-with-permission-login>", | |
$serverPassword = "<database-user-with-permission-password>" | |
) | |
# Cria as credenciais de armazenamento do azure Storage | |
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force | |
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword | |
function ExportWithModule(){ | |
param( | |
$dbName, | |
$BacpacUri, | |
$creds, | |
$ResourceGroupName , | |
$ServerName | |
) | |
Write-Host "Iniciando a exportacao para da base $dbName ($ServerName - $ResourceGroupName) para o diretorio $BacpacUri" | |
$exportRequest = New-AzureRmSqlDatabaseExport -DatabaseName $dbName -ServerName $ServerName -StorageKeyType "StorageAccessKey" -StorageKey $StorageKey -StorageUri $BacpacUri -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password -ResourceGroupName $ResourceGroupName | |
return $exportRequest; | |
} | |
function ExportWithAzureCli(){ | |
param( | |
$dbName, | |
$BacpacUri | |
) | |
return &az sql db export --resource-group $ResourceGroupName --server $ServerName ` | |
-n $dbName --storage-key-type $StorageKeytype --storage-key $StorageKey --storage-uri $BacpacUri ` | |
-u $creds.UserName -p $creds.Password | |
} | |
function FormatExport(){ | |
param($dbName) | |
# Cria o nome do arquivo que será utilizado para armazenar o backup | |
$bacpacFilename = ($dbName +(Get-Date).ToString("/yyyy/")+(Get-Date).ToString("MM_MMMM/")+$dbName+(Get-Date).ToString("_dd_MM_yyyy_HH-mm")+".bacpac"); | |
# Concatena o nome do arquivo com a URL do container | |
$BacpacUri = $BaseStorageUri +"/"+ $bacpacFilename ; | |
$exportRequest = ExportWithModule -dbName $dbName -BacpacUri $BacpacUri -creds $creds -ResourceGroupName $ResourceGroupName -ServerName $ServerName | |
return $exportRequest; | |
} | |
$DatabaseName | %{ FormatExport -dbName $_ } | |
# ExportWithAzureCli $dbName | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment