Azure MySQL Backup with PowerShell

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.

Prerequisites
Understanding Azure MySQL Backups

Azure 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.

PowerShell Script Example

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
            
Explanation of Parameters
Tip: Ensure your storage account is in the same region as your Azure Database for MySQL server for optimal performance and to avoid cross-region data transfer costs.
How to Run the Script
  1. Save the script above as a .ps1 file (e.g., Backup-AzureMySql.ps1).
  2. Open Azure PowerShell.
  3. Execute the script, providing the necessary parameters:
    .\Backup-AzureMySql.ps1 -ResourceGroupName "myResourceGroup" -ServerName "myAzureMySqlServer" -BackupName "daily-backup-$(Get-Date -Format 'yyyyMMdd')" -StorageAccountName "mybackupstorage" -StorageContainerName "mysql-backups"
Important Considerations

By implementing a robust backup strategy with PowerShell, you can ensure the availability and recoverability of your Azure Database for MySQL instances.