【问题标题】:Azure Database Overnight CopyAzure 数据库隔夜复制
【发布时间】:2017-03-16 13:08:57
【问题描述】:

我在 Azure 中托管了 2 个数据库,一个生产环境和一个暂存环境。一夜之间,我希望使用生产数据库的副本清除和恢复暂存数据库。

有谁知道这是否可以通过 azure 实现,或者我需要编写自己的自定义脚本吗?我查看了 Azure 数据同步,但看起来它会合并数据。

谢谢,

【问题讨论】:

    标签: sql sql-server azure azure-sql-database


    【解决方案1】:

    先发database backup of production to a storage account

    $subscriptionId = "YOUR AZURE SUBSCRIPTION ID"
    
    Login-AzureRmAccount
    Set-AzureRmContext -SubscriptionId $subscriptionId
    
    # Database to export
    $DatabaseName = "DATABASE-NAME"
    $ResourceGroupName = "RESOURCE-GROUP-NAME"
    $ServerName = "SERVER-NAME"
    $serverAdmin = "ADMIN-NAME"
    $serverPassword = "ADMIN-PASSWORD" 
    $securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force
    $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword
    
    # Generate a unique filename for the BACPAC
    $bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyyMMddHHmm") + ".bacpac"
    
    # Storage account info for the BACPAC
    $BaseStorageUri = "https://STORAGE-NAME.blob.core.windows.net/BLOB-CONTAINER-NAME/"
    $BacpacUri = $BaseStorageUri + $bacpacFilename
    $StorageKeytype = "StorageAccessKey"
    $StorageKey = "YOUR STORAGE KEY"
    
    $exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
       -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
       -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password
    $exportRequest
    
    # Check status of the export
    Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink
    

    现在try restoring using below powershell

    # Restore a database to a previous point in time
    
    #$resourceGroupName = {resource-group-name}
    #$serverName = {server-name}
    $newRestoredDatabaseName = "{new-database-name}"
    $localTimeToRestoreTo = "{12/9/2016 12:00:00 PM}" # change to a valid restore point for your database
    $restorePointInTime = (Get-Date $localTimeToRestoreTo).ToUniversalTime()
    $newDatabaseEdition = "Basic"
    $newDatabaseServiceLevel = "Basic"
    
    Write-Host "Restoring database '$databaseName' to its state at:"(Get-Date $restorePointInTime).ToLocalTime()", to a new database named '$newRestoredDatabaseName'."
    
    $restoredDb = Restore-AzureRmSqlDatabase -FromPointInTimeBackup -PointInTime $restorePointInTime -ResourceGroupName $resourceGroupName `
     -ServerName $serverName -TargetDatabaseName $newRestoredDatabaseName -Edition $newDatabaseEdition -ServiceObjectiveName $newDatabaseServiceLevel `
     -ResourceId $databaseToRestore.ResourceID
    
    $restoredDb
    

    这些entire things can be automated as well

    【讨论】:

    • 谢谢你!我将它添加为 azure 中的自动化运行手册,并像魅力一样工作。
    【解决方案2】:

    您可以使用“复制数据库”功能复制到同一个或另一个逻辑服务器:

    https://docs.microsoft.com/en-us/azure/sql-database/sql-database-copy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2014-07-27
      • 2015-09-11
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      相关资源
      最近更新 更多