[Azure]ARM模式托管磁盘的快照与还原[2]——删除与恢复

60 篇文章 0 订阅
59 篇文章 0 订阅

本文中包含两个脚本,一个是删除托管磁盘快照的脚本,一个是使用托管磁盘快照还原磁盘的脚本。

比较简单,只是几个命令的调用,这里就不详细解释具体的语句意义了,大家直接看脚本吧:

删除快照的脚本:

param(  
    [Parameter(Mandatory = $true)]
    [string]$SubscriptionName,
   
    [Parameter(Mandatory = $true)]
    [string]$ResourceGroupName,
  
    [Parameter(Mandatory = $true)]
    [string]$ManagedDiskName
)

[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);
$disk = Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $ManagedDiskName;
if ($disk -eq $null)
{
    Write-Host "Disk not found";
    return;
}

$snapshots = Get-AzureRmSnapshot;
$filteredSnapshots = @($snapshots | where {$_.CreationData.SourceUri -eq $disk.Id});
Write-Host ("Snapshot found for disk {0}:" -f $ManagedDiskName) -ForegroundColor Yellow;
$filteredSnapshots | select @{Name="Id"; Expression={$filteredSnapshots.IndexOf($_) + 1}}, @{Name="TimeCreated"; Expression={$_.TimeCreated.ToLocalTime()}}, Name | ft;
$selection = (Read-Host "Please select the snapshot you want to delete, input 'a' if you want to delete all snapshots");
If ($PSCmdlet.ShouldContinue("Confirm?", "=== Confirm delete Opeartion ==="))
{
    if ($selection -eq "a")
    {
        foreach ($snapshot in $filteredSnapshots)
        {
            Write-Host ("Deleting snapshot {0}" -f $snapshot.Name) -ForegroundColor Yellow;
            [void](Remove-AzureRmSnapshot -ResourceGroupName $snapshot.ResourceGroupName -SnapshotName $snapshot.Name -Force);
            Write-Host ("Snapshot {0} deleted" -f $snapshot.Name) -ForegroundColor Green;
        }
    }
    else
    {
        $selection = $selection - 1;
        $snapshot = $filteredSnapshots[$selection];
        Write-Host ("Deleting snapshot {0}" -f $snapshot.Name) -ForegroundColor Yellow;
        [void](Remove-AzureRmSnapshot -ResourceGroupName $snapshot.ResourceGroupName -SnapshotName $snapshot.Name -Force);
        Write-Host ("Snapshot {0} deleted" -f $snapshot.Name) -ForegroundColor Green;
    }
}

还原磁盘的脚本:

param(  
    [Parameter(Mandatory = $true)]
    [string]$SubscriptionName,
   
    [Parameter(Mandatory = $true)]
    [string]$ResourceGroupName,
  
    [Parameter(Mandatory = $true)]
    [string]$ManagedDiskName,

    [Parameter(Mandatory = $true)]
    [string]$RestoredDiskName
)

[void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName);
$disk = Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $ManagedDiskName;
if ($disk -eq $null)
{
    Write-Host "Disk not found";
    return;
}

$snapshots = Get-AzureRmSnapshot;
$filteredSnapshots = @($snapshots | where {$_.CreationData.SourceUri -eq $disk.Id});
Write-Host ("Snapshot found for disk {0}:" -f $ManagedDiskName) -ForegroundColor Yellow;
$filteredSnapshots | select @{Name="Id"; Expression={$filteredSnapshots.IndexOf($_) + 1}}, @{Name="TimeCreated"; Expression={$_.TimeCreated.ToLocalTime()}}, Name | ft;
$selection = (Read-Host "Please select the snapshot you want to restore");
$selection = $selection - 1;
$snapshot = $filteredSnapshots[$selection];
$restoredDiskConfig = $null;
if ($snapshot.OsType -eq $null)
{
    $restoredDiskConfig = New-AzureRmDiskConfig -AccountType $snapshot.AccountType -Location $snapshot.Location -CreateOption Copy -SourceResourceId $snapshot.Id;
}
else
{
    $restoredDiskConfig = New-AzureRmDiskConfig -AccountType $snapshot.AccountType -OsType $snapshot.OsType -Location $snapshot.Location -CreateOption Copy -SourceResourceId $snapshot.Id;
}
Write-Host ("Restoring data to managed disk {0}" -f $RestoredDiskName) -ForegroundColor Yellow;
[void](New-AzureRmDisk -DiskName $RestoredDiskName -Disk $restoredDiskConfig -ResourceGroupName $ResourceGroupName);
Write-Host "Finished" -ForegroundColor Yellow;

脚本调用示例:

删除:


还原:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值