在Azure中备份/恢复Blob

时间:2016-05-11 06:10:52

标签: azure azure-storage azure-storage-blobs

客户端意外地从Azure Blob存储中的Container中删除了一些文件(通过CMS)。有没有办法在azure portal中管理容器的备份和恢复,以便我可以防止这种情况再次发生?

1 个答案:

答案 0 :(得分:1)

是的,您可以创建另一个存储帐户并将容器blob复制到该帐户。您可以使用powershell来执行此操作。

将所有容器/ blob复制到另一个存储帐户的代码:

$SourceStorageAccount = "sourceStorageAccount"
$SourceStorageKey = "sourceKey"
$DestStorageAccount = "destStorageAccount"
$DestStorageKey = "destKey"

$SourceStorageContext = New-AzureStorageContext –StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
$DestStorageContext = New-AzureStorageContext –StorageAccountName $DestStorageAccount -StorageAccountKey $DestStorageKey

$containers = Get-AzureStorageContainer -Context $SourceStorageContext

foreach($container in $containers) {  
    New-AzureStorageContainer -Context $DestStorageContext -Name $container.name -Permission Off

    $Blobs = Get-AzureStorageBlob -Context $SourceStorageContext -Container $container.name

    #Do the copy of everything
    foreach ($Blob in $Blobs) {
       Write-Output "Moving $Blob.Name"
       Start-CopyAzureStorageBlob -Context $SourceStorageContext -SrcContainer $container.name -SrcBlob $Blob.Name `
          -DestContext $DestStorageContext -DestContainer $container.name -DestBlob $Blob.Name
    } 
}

将容器复制到另一个存储帐户的代码:

$SourceStorageAccount = "sourceStorageAccount"
$SourceStorageKey = "sourceKey"
$DestStorageAccount = "destStorageAccount"
$DestStorageKey = "destKey"

$containerName = 'SourceContainerName'; 

$SourceStorageContext = New-AzureStorageContext –StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
$DestStorageContext = New-AzureStorageContext –StorageAccountName $DestStorageAccount -StorageAccountKey $DestStorageKey

New-AzureStorageContainer -Context $DestStorageContext -Name $containerName -Permission Off

$Blobs = Get-AzureStorageBlob -Context $SourceStorageContext -Container $containerName

#Do the copy of everything
foreach ($Blob in $Blobs) {
   Write-Output "Moving $Blob.Name"
   Start-CopyAzureStorageBlob -Context $SourceStorageContext -SrcContainer $containerName -SrcBlob $Blob.Name `
       -DestContext $DestStorageContext -DestContainer $containerName -DestBlob $Blob.Name
}

您可以在同一存储帐户中备份容器:

$StorageAccount = "yourStorageAccount"
$StorageKey = "yourStorageKey"

$SourceContainerName = 'yourContainerName'; 
$DestContainerName = 'yourBackupContainerName';

$StorageContext = New-AzureStorageContext –StorageAccountName $StorageAccount -StorageAccountKey $StorageKey

New-AzureStorageContainer -Context $StorageContext -Name $DestContainerName -Permission Off

$Blobs = Get-AzureStorageBlob -Context $StorageContext -Container $SourceContainerName

#Do the copy of everything
foreach ($Blob in $Blobs) {
   Write-Output "Moving $Blob.Name"
   Start-CopyAzureStorageBlob -Context $StorageContext -SrcContainer $SourceContainerName -SrcBlob $Blob.Name `
       -DestContext $StorageContext -DestContainer $DestContainerName -DestBlob $Blob.Name
}