使用 Powershell 将 Azure 数据库备份到 blob
Posted
技术标签:
【中文标题】使用 Powershell 将 Azure 数据库备份到 blob【英文标题】:Azure database backup to blob using Powershell 【发布时间】:2013-03-21 16:57:47 【问题描述】:我们需要备份 azure 数据库并将其存储在 blob 中,以便可以恢复它。我看过这个博客,但它使用第三方 cmdlet。
http://weblogs.thinktecture.com/cweyer/2011/01/automating-backup-of-a-sql-azure-database-to-azure-blob-storage-with-the-help-of-powershell-and-task-scheduler.html
有人可以指导/帮助如何使用 powershell 实现上述目标。
【问题讨论】:
【参考方案1】:首先完成 Azure 自动化设置(请参阅 here)。
编辑打击脚本并将其保存为 .ps1 文件。当你运行它时 第一次,它会询问您的 azure 自动化帐户和 您的数据库凭据。在此过程中,它将保存您的 安全地保存在本地文件中的凭据(请参阅here 它是如何完成的)。在这段时间之后,它会使用保存的凭据。
.psl 文件和加密的凭据文件应存储在一个文件中 目录
一旦您满意,您可以安排它在任务调度程序中运行。
function Get-MyCredential
param(
$CredPath,
[switch]$Help
)
$HelpText = @"
Get-MyCredential
Usage:
Get-MyCredential -CredPath `$CredPath
If a credential is stored in $CredPath, it will be used.
If no credential is found, Export-Credential will start and offer to
Store a credential at the location specified.
"@
if($Help -or (!($CredPath)))write-host $Helptext; Break
if (!(Test-Path -Path $CredPath -PathType Leaf))
Export-Credential (Get-Credential) $CredPath
$cred = Import-Clixml $CredPath
$cred.Password = $cred.Password | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PsCredential($cred.UserName, $cred.Password)
Return $Credential
function Export-Credential($cred, $path)
$cred = $cred | Select-Object *
$cred.password = $cred.Password | ConvertFrom-SecureString
$cred | Export-Clixml $path
#Create a directory with you azure server name to isolate configurations
$FileRootPath = "C:\PowerShellScripts\AzureServerName"
Write-Host "Getting Azure credentials"
$AzureCred = Get-MyCredential ($FileRootPath + "AzureSyncred.txt")
#Use Azure Automation Account
#(If You do not have it will not work with other accounts)
Add-AzureAccount -Credential $AzureCred
Select-AzureSubscription -SubscriptionId "myAzureSubscriptionId"
#DO NOT use tcp:myServerName.database.windows.net,1433 but only myServerName
$ServerName = "myServerName"
$Date = Get-Date -format "yyyy-MM-dd-HH-mm"
$DatabaseName = "myTargetDatabaseName"
$BlobName = $Date + "-" + $DatabaseName.bacpac"
$StorageName = "myStorageAccountName"
$ContainerName = "myContainerNameToStoreBacpacFiles"
$StorageKey = "myStorageAccountKey"
Write-Host "Getting database user credential"
#DO NOT use myDatabaseUsername@myServerName but only myDatabaseUsername
$credential = Get-MyCredential ($FileRootPath + "DbSyncred.xml")
Write-Host "Connecting to Azure database"
$SqlCtx = New-AzureSqlDatabaseServerContext -ServerName $ServerName -Credential $credential
Write-Host "Connecting to Blob storage"
$StorageCtx = New-AzureStorageContext -StorageAccountName $StorageName -StorageAccountKey $StorageKey
$Container = Get-AzureStorageContainer -Name $ContainerName -Context $StorageCtx
Write-Host "Exporting data to blob"
$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlCtx -StorageContainer $Container -DatabaseName $DatabaseName -BlobName $BlobName
Get-AzureSqlDatabaseImportExportStatus -Request $exportRequest
# use the below script in powershell to execute the script
# powershell -ExecutionPolicy ByPass –File C:\PowerShellScripts\AzureServerName\mySavedScript.ps1 –noexit
【讨论】:
【参考方案2】:这是我的 powershell 脚本
https://gist.github.com/voxon2/be29a3fd6dabbb9155ca
这是一篇描述除 powershell 之外的许多不同方法的文章
http://blogs.msdn.com/b/mast/archive/2013/03/04/different-ways-to-backup-your-windows-azure-sql-database.aspx
【讨论】:
【参考方案3】:Azure DB 不支持备份到 WA Blob 存储,而是该服务使用 PITR 功能为您自动备份。您会发现以下文档很有用:
http://msdn.microsoft.com/en-us/library/azure/hh852669.aspx
http://msdn.microsoft.com/en-us/library/azure/jj650016.aspx
希望这会有所帮助。
【讨论】:
以上是关于使用 Powershell 将 Azure 数据库备份到 blob的主要内容,如果未能解决你的问题,请参考以下文章
将 Azure Powershell 命令转换为 Azure CLI
使用 powershell 将两个 azure blob 合并为单个 blob
如何使用 Azure Devops 将 powershell 列表变量传输到 terraform?
使用 PowerShell 生成 Azure 存储帐户 SAS 密钥