使用Powershell将Azure数据库备份到blob

时间:2013-03-31 12:52:32

标签: backup azure-sql-database database-backups

我们需要备份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实现上述目标。

3 个答案:

答案 0 :(得分:0)

Azure数据库不支持备份到WA Blob Store,而是服务使用PITR功能为您自动备份。您会发现以下文档非常有用:

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

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

希望这有帮助。

答案 1 :(得分:0)

答案 2 :(得分:0)

首先完成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