2017-03-16 68 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

谢谢!我将它添加为天蓝色的自动化操作手册,并像魅力一样工作。 –