This guide demonstrates how to automate the backup of your Azure Database for MySQL flexible server instances using PowerShell. Backups are crucial for data protection, disaster recovery, and compliance.
Install-Module -Name Az -AllowClobber -Scope CurrentUserAzure Database for MySQL automatically takes backups of your server. These backups are stored in Azure storage. You can perform manual backups as well, which are point-in-time backups that can be restored to a new server. This script focuses on performing manual full backups.
The following PowerShell script will create a manual full backup of your specified Azure Database for MySQL flexible server.
param(
[string]$ResourceGroupName,
[string]$ServerName,
[string]$BackupName,
[string]$StorageAccountName,
[string]$StorageContainerName,
[string]$StorageBlobName = "$BackupName.bak"
)
# Connect to Azure (if not already connected)
if (-not (Get-AzContext)) {
Connect-AzAccount
}
# Set the context to your Azure subscription (optional if you have only one)
# Set-AzContext -SubscriptionId "your-subscription-id"
# Get the MySQL server object
$mysqlServer = Get-AzMySqlFlexibleServer -ResourceGroupName $ResourceGroupName -Name $ServerName
if (-not $mysqlServer) {
Write-Error "Azure MySQL Flexible Server '$ServerName' not found in resource group '$ResourceGroupName'."
exit 1
}
# Get the storage account object
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
if (-not $storageAccount) {
Write-Error "Storage account '$StorageAccountName' not found in resource group '$ResourceGroupName'."
exit 1
}
# Get the storage container object
$storageContainer = Get-AzStorageContainer -Context $storageAccount.Context -Name $StorageContainerName -ErrorAction SilentlyContinue
if (-not $storageContainer) {
Write-Warning "Storage container '$StorageContainerName' not found. Creating it..."
New-AzStorageContainer -Context $storageAccount.Context -Name $StorageContainerName -PublicAccess Off
$storageContainer = Get-AzStorageContainer -Context $storageAccount.Context -Name $StorageContainerName
}
# Create the backup parameters
$backupParams = @{
ResourceGroupName = $ResourceGroupName
ServerName = $ServerName
StorageAccountName = $StorageAccountName
StorageContainerName = $StorageContainerName
StorageBlobName = $StorageBlobName
}
# Perform the backup
Write-Host "Starting backup of server '$ServerName' to blob '$StorageBlobName'..."
$backupResult = Invoke-AzMySqlFlexibleServerBackup @backupParams
if ($backupResult) {
Write-Host "Backup operation initiated successfully."
Write-Host "Backup Name: $($backupResult.BackupName)"
Write-Host "Operation Status: $($backupResult.Status)"
Write-Host "Backup will be stored in Azure Blob Storage container: '$StorageContainerName' with blob name: '$StorageBlobName'"
} else {
Write-Error "Failed to initiate backup operation."
}
# To monitor backup status, you can use:
# Get-AzMySqlFlexibleServerBackupOperation -ResourceGroupName $ResourceGroupName -ServerName $ServerName -OperationId $backupResult.OperationId
$ResourceGroupName: The name of the resource group containing your MySQL server and storage account.$ServerName: The name of your Azure Database for MySQL flexible server instance.$BackupName: A descriptive name for your backup. This will be used to construct the blob name.$StorageAccountName: The name of the Azure Storage Account where the backup will be stored.$StorageContainerName: The name of the blob container within the storage account. The script will create it if it doesn't exist.$StorageBlobName: The full name of the blob file where the backup will be saved. Defaults to $BackupName.bak..ps1 file (e.g., Backup-AzureMySql.ps1)..\Backup-AzureMySql.ps1 -ResourceGroupName "myResourceGroup" -ServerName "myAzureMySqlServer" -BackupName "daily-backup-$(Get-Date -Format 'yyyyMMdd')" -StorageAccountName "mybackupstorage" -StorageContainerName "mysql-backups"
By implementing a robust backup strategy with PowerShell, you can ensure the availability and recoverability of your Azure Database for MySQL instances.