Skip to content

[Storage] Support Point in Time Restore #12799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@ public void TestStorageBlobORS()
{
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobORS");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestStorageBlobRestore()
{
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobRestore");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,66 @@ function Test-StorageBlobServiceProperties
}
}

<#
.SYNOPSIS
Test StorageAccount Blob Restore
.DESCRIPTION
SmokeTest
#>
function Test-StorageBlobRestore
{
# Setup
$rgname = Get-StorageManagementTestResourceName;

try
{
# Test
$stoname = 'sto' + $rgname;
$stotype = 'Standard_LRS';
$loc = Get-ProviderLocation ResourceManagement;
$kind = 'StorageV2'

Write-Verbose "RGName: $rgname | Loc: $loc"
New-AzResourceGroup -Name $rgname -Location $loc;

$loc = Get-ProviderLocation_Stage ResourceManagement;
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype -Kind $kind
$stos = Get-AzStorageAccount -ResourceGroupName $rgname;

# Enable Blob Delete Retension Policy, Enable Changefeed, then enabled blob restore policy, then get blob service proeprties and check the setting
Enable-AzStorageBlobDeleteRetentionPolicy -ResourceGroupName $rgname -StorageAccountName $stoname -RetentionDays 5
Update-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname -EnableChangeFeed $true
# If record, need sleep before enable the blob restore policy, or will get server error
#sleep 100
Enable-AzStorageBlobRestorePolicy -ResourceGroupName $rgname -StorageAccountName $stoname -RestoreDays 4
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
Assert-AreEqual $true $property.ChangeFeed.Enabled
Assert-AreEqual $true $property.DeleteRetentionPolicy.Enabled
Assert-AreEqual 5 $property.DeleteRetentionPolicy.Days
Assert-AreEqual $true $property.RestorePolicy.Enabled
Assert-AreEqual 4 $property.RestorePolicy.Days

# restore blobs by -asjob
$range1 = New-AzStorageBlobRangeToRestore -StartRange container1/blob1 -EndRange container2/blob2
$range2 = New-AzStorageBlobRangeToRestore -StartRange container3/blob3 -EndRange ""
$job = Restore-AzStorageBlobRange -ResourceGroupName $rgname -StorageAccountName $stoname -TimeToRestore (Get-Date).AddSeconds(-1) -BlobRestoreRange $range1,$range2 -asjob

# Get Storage Account with Blob Restore Status
$stos = Get-AzStorageAccount -ResourceGroupName $rgname -StorageAccountName $stoname -IncludeBlobRestoreStatus

# wait for restore job finish, and check Blob Restore Status in Storage Account
$job | Wait-Job
$stos = Get-AzStorageAccount -ResourceGroupName $rgname -StorageAccountName $stoname -IncludeBlobRestoreStatus
Assert-AreEqual "Complete" $stos.BlobRestoreStatus.Status

Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.3.0-preview.2" />
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.3.0-preview.2" />
<PackageReference Include="Azure.Storage.Queues" Version="12.4.0-preview.6" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="17.1.0" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="17.2.0" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion src/Storage/Storage.Management/Az.Storage.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey',
'New-AzStorageObjectReplicationPolicyRule',
'Set-AzStorageObjectReplicationPolicy',
'Get-AzStorageObjectReplicationPolicy',
'Remove-AzStorageObjectReplicationPolicy'
'Remove-AzStorageObjectReplicationPolicy',
'Enable-AzStorageBlobRestorePolicy','Disable-AzStorageBlobRestorePolicy',
'New-AzStorageBlobRangeToRestore','Restore-AzStorageBlobRange'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Management.Storage
{
using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

/// <summary>
/// Modify Azure Storage service properties
/// </summary>
[CmdletOutputBreakingChange(typeof(PSRestorePolicy), ChangeDescription = "The deprecated proeprty LastEnabledTime will be removed in a future release.")]
[Cmdlet("Disable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobRestorePolicy, SupportsShouldProcess = true, DefaultParameterSetName = AccountNameParameterSet), OutputType(typeof(PSRestorePolicy))]
public class DisableAzStorageBlobRestorePolicyCommand : StorageBlobBaseCmdlet
{

/// <summary>
/// AccountName Parameter Set
/// </summary>
private const string AccountNameParameterSet = "AccountName";

/// <summary>
/// Account object parameter set
/// </summary>
private const string AccountObjectParameterSet = "AccountObject";

/// <summary>
/// BlobServiceProperties ResourceId parameter set
/// </summary>
private const string PropertiesResourceIdParameterSet = "BlobServicePropertiesResourceId";

[Parameter(
Position = 0,
Mandatory = true,
HelpMessage = "Resource Group Name.",
ParameterSetName = AccountNameParameterSet)]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Parameter(
Position = 1,
Mandatory = true,
HelpMessage = "Storage Account Name.",
ParameterSetName = AccountNameParameterSet)]
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
[Alias(AccountNameAlias, NameAlias)]
[ValidateNotNullOrEmpty]
public string StorageAccountName { get; set; }

[Parameter(Mandatory = true,
HelpMessage = "Storage account object",
ValueFromPipeline = true,
ParameterSetName = AccountObjectParameterSet)]
[ValidateNotNullOrEmpty]
public PSStorageAccount StorageAccount { get; set; }

[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Input a Storage account Resource Id, or a Blob service properties Resource Id.",
ParameterSetName = PropertiesResourceIdParameterSet)]
[ValidateNotNullOrEmpty]
public string ResourceId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
if (ShouldProcess("BlobRestorePolicy", "Disable"))
{
switch (ParameterSetName)
{
case AccountObjectParameterSet:
this.ResourceGroupName = StorageAccount.ResourceGroupName;
this.StorageAccountName = StorageAccount.StorageAccountName;
break;
case PropertiesResourceIdParameterSet:
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(ResourceId);
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromResourceId(ResourceId);
break;
default:
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
break;
}
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);

serviceProperties.RestorePolicy = new RestorePolicyProperties();
serviceProperties.RestorePolicy.Enabled = false;
serviceProperties.RestorePolicy.Days = null;

serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);

if (PassThru)
{
WriteObject(new PSRestorePolicy(serviceProperties.RestorePolicy));
}

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Management.Storage
{
using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

/// <summary>
/// Modify Azure Storage service properties
/// </summary>
[CmdletOutputBreakingChange(typeof(PSRestorePolicy), ChangeDescription = "The deprecated proeprty LastEnabledTime will be removed in a future release.")]
[Cmdlet("Enable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobRestorePolicy, DefaultParameterSetName = AccountNameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSRestorePolicy))]
public class EnableAzStorageBlobRestorePolicyCommand : StorageBlobBaseCmdlet
{

/// <summary>
/// AccountName Parameter Set
/// </summary>
private const string AccountNameParameterSet = "AccountName";

/// <summary>
/// Account object parameter set
/// </summary>
private const string AccountObjectParameterSet = "AccountObject";

/// <summary>
/// BlobServiceProperties ResourceId parameter set
/// </summary>
private const string PropertiesResourceIdParameterSet = "BlobServicePropertiesResourceId";

[Parameter(
Position = 0,
Mandatory = true,
HelpMessage = "Resource Group Name.",
ParameterSetName = AccountNameParameterSet)]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Parameter(
Position = 1,
Mandatory = true,
HelpMessage = "Storage Account Name.",
ParameterSetName = AccountNameParameterSet)]
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
[Alias(AccountNameAlias, NameAlias)]
[ValidateNotNullOrEmpty]
public string StorageAccountName { get; set; }

[Parameter(Mandatory = true,
HelpMessage = "Storage account object",
ValueFromPipeline = true,
ParameterSetName = AccountObjectParameterSet)]
[ValidateNotNullOrEmpty]
public PSStorageAccount StorageAccount { get; set; }

[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Input a Storage account Resource Id, or a Blob service properties Resource Id.",
ParameterSetName = PropertiesResourceIdParameterSet)]
[ValidateNotNullOrEmpty]
public string ResourceId { get; set; }

[Parameter(Mandatory = true, HelpMessage = "Sets the number of days for the blob can be restored..")]
[Alias("Days")]
public int RestoreDays { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
if (ShouldProcess("BlobRestorePolicy", "Enable"))
{
switch (ParameterSetName)
{
case AccountObjectParameterSet:
this.ResourceGroupName = StorageAccount.ResourceGroupName;
this.StorageAccountName = StorageAccount.StorageAccountName;
break;
case PropertiesResourceIdParameterSet:
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(ResourceId);
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromResourceId(ResourceId);
break;
default:
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
break;
}
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);

serviceProperties.RestorePolicy = new RestorePolicyProperties();
serviceProperties.RestorePolicy.Enabled = true;
serviceProperties.RestorePolicy.Days = RestoreDays;

serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);

if (PassThru)
{
WriteObject(new PSRestorePolicy(serviceProperties.RestorePolicy));
}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ namespace Microsoft.Azure.Commands.Management.Storage
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

/// <summary>
/// Modify Azure Storage service properties
/// </summary>
[CmdletOutputBreakingChange(typeof(PSBlobServiceProperties), ChangeDescription = "The deprecated proeprty RestorePolicy.LastEnabledTime will be removed in a future release.")]
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobServiceProperty, DefaultParameterSetName = AccountNameParameterSet), OutputType(typeof(PSBlobServiceProperties))]
public class GetAzStorageBlobServicePropertyCommand : StorageBlobBaseCmdlet
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class StorageBlobBaseCmdlet : AzureRMCmdlet
protected const string StorageContainerLeaseNounStr = StorageContainerNounStr + "Lease";
protected const string StorageBlobServiceProperty = "StorageBlobServiceProperty";
protected const string StorageBlobDeleteRetentionPolicy = "StorageBlobDeleteRetentionPolicy";
protected const string StorageBlobRestorePolicy = "StorageBlobRestorePolicy";

public const string StorageAccountResourceType = "Microsoft.Storage/storageAccounts";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ namespace Microsoft.Azure.Commands.Management.Storage
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

/// <summary>
/// Modify Azure Storage service properties
/// </summary>
[CmdletOutputBreakingChange(typeof(PSBlobServiceProperties), ChangeDescription = "The deprecated proeprty RestorePolicy.LastEnabledTime will be removed in a future release.")]
[Cmdlet("Update", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobServiceProperty, SupportsShouldProcess = true, DefaultParameterSetName = AccountNameParameterSet), OutputType(typeof(PSBlobServiceProperties))]
public class UpdateAzStorageBlobServicePropertyCommand : StorageBlobBaseCmdlet
{
Expand Down
Loading