【问题标题】:Azure database backup to blob using Powershell使用 Powershell 将 Azure 数据库备份到 blob
【发布时间】: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 实现上述目标。

【问题讨论】:

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


    【解决方案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】:

      【讨论】:

        【解决方案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

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-23
          • 1970-01-01
          • 2017-05-25
          • 2021-09-18
          • 2021-07-02
          • 1970-01-01
          • 2021-03-04
          相关资源
          最近更新 更多