我是靠谱客的博主 快乐宝马,最近开发中收集的这篇文章主要介绍[Azure]ARM模式托管磁盘的快照与还原[2]——删除与恢复,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

删除快照的脚本:

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;

脚本调用示例:

删除:


还原:

最后

以上就是快乐宝马为你收集整理的[Azure]ARM模式托管磁盘的快照与还原[2]——删除与恢复的全部内容,希望文章能够帮你解决[Azure]ARM模式托管磁盘的快照与还原[2]——删除与恢复所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部