2017-03-16 66 views
0

我有2個數據庫託管在Azure中,一個生產環境和一個臨時環境。一夜之間,我希望清除臨時數據庫並使用生產數據庫的副本進行恢復。Azure數據庫隔夜複製

有誰知道這是否可能通過天藍色或我需要寫我自己的自定義腳本?我看着Azure Data Sync,但看起來它會合並數據。

感謝,

回答

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

+0

謝謝!我將它添加爲天藍色的自動化操作手冊,並像魅力一樣工作。 –