4

我们需要备份 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-任务调度器.html

有人可以指导/帮助如何使用powershell实现上述目标。

4

3 回答 3

0

Azure DB 不支持备份到 WA Blob Store,而是使用 PITR 功能为您自动备份。您会发现以下文档很有用:

http://msdn.microsoft.com/en-us/library/azure/hh852669.aspx

http://msdn.microsoft.com/en-us/library/azure/jj650016.aspx

希望这可以帮助。

于 2014-05-21T22:48:33.663 回答
0

首先完成您的 Azure 自动化设置(请参阅此处)。

编辑打击脚本并将其保存为 .ps1 文件。当您第一次运行它时,它会询问您的 Azure 自动化帐户和数据库凭据。在此过程中,它会将您的凭据安全地保存在本地文件中(请参阅此处如何完成)。在病房的这段时间之后,它使用保存的凭据。

.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
于 2016-03-02T11:17:46.150 回答
0

这是我的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

于 2015-11-20T14:23:02.827 回答