A backup of a user's virtual machine hard disk is a very important part of the customer's deployment of cloud applications.
There are multiple platforms for backup methods:
- Capture Image: The virtual machine system disk and data disk can be captured in capture mode (PowerShell command for Save-azurevmimage). However, this approach takes up too much space and the number of image capture is limited. So it is not the preferred scheme.
- Copy the hard disk VHD file: Azcopy The VM's VHD to the container of the partial blob. This approach is also very space-intensive. Not suitable for large-scale backup, only suitable for use during disaster recovery.
- VHD file for blob snapshot: Blob snapshot for VHD files of VMs via BLOB snapshot feature. This saves space, is easy to operate, and is a very good way for a virtual machine to be backed up.
Azure's snapshot is billed based on the actual usage space. Because snapshot follows the "copy-on-write" approach, if the content of the base blob does not change, there is no content in snapshot, and only the base BLOB changes, snapshot will copy the old content from the base blob.
The specific billing methods are as follows:
Case one: If the content of the snapshot is the same as the content of the base blob, it charges only the base BLOB (3 units).
Scenario Two: If the CCC content in the base blob is updated, snapshot copies the old CCC portion of the base blob. Charge will be 4 units
Scenario Three: If the CCC content in the base blob is removed, the old CCC portion of the base blob is duplicated in the ddd,snapshot. Charge will be 4 units
Situation four: Multiple blob blocks are changed, multiple snapshot are billed according to actual usage. The billing unit in the figure is 8
The implementation of the specific BLOB snapshot has three parts: 1. Create; 2. Delete the snapshot;3 that is more than XX days. Restore snapshot. This article describes the script for these three features:
- Create snapshot:
$storageaccounts = Get-azurestorageaccount
foreach ($storageaccount in $storageaccounts)
{
Set-azuresubscription-subscriptionname xxxx-currentstorageaccountname $storageaccount. Storageaccountname
$pageblobs = Get-azurestorageblob-container VHDs | Where-object {$_. Name-match ". vhd"}
foreach ($blob in $pageblobs)
{
if (! $blob. Icloudblob.issnapshot)
{
}
}
}
- Delete Snapshot for more than 14 days
$storageaccounts = Get-azurestorageaccount
foreach ($storageaccount in $storageaccounts)
{
$pageblobs = Get-azurestorageblob-container VHDs | Where-object {$_. Name-match ". vhd"}
$now = Get-date
foreach ($pageblob in $pageblobs)
{
$diff = [Datetime]::frombinary ($now. ticks-$pageblob. Snapshottime.ticks)
if ($pageblob. Icloudblob.issnapshot)
{
if ($diff. Dayofyear-ge 14)
{
Write-host "The snapshot is" $diff. DayOfYear ' Days '
Write-host "Delete"
$pageblob. Icloudblob.delete ()
}else
{
Write-host "The snapshot is" $diff. DayOfYear ' Days '
Write-host "Not delete snapshot"
}
}else
{
Write-host "is not snapshot, does not delete"
}
}
}
3. Restore the snapshot 7 days ago
$StorageAccount = "XXXX"
$StorageKey = "xxx=="
$CTR = New-azurestoragecontext-storageaccountname $StorageAccount-storageaccountkey $StorageKey
$SrcContainer = "Container1"
$DestContainer = "Container2"
$blobname = "Xxx.vhd"
$Date =date
$DestBlob = "R" + $Date. dayofyear+ $blobname
$DayOfYear = 7
$times = Get-azurestorageblob-container $SrcContainer | Where-object {$_. Name-match "Xxx.vhd"} | Select-expandproperty Snapshottime
foreach ($time in $times)
{
if ($time. Dayofyear-eq $DayOfYear)
{
$snaptime = $time
}
Else
{
Continue
}
}
$srcsnap = Get-azurestorageblob-container $SrcContainer | Where-object {$_. Snapshottime-eq $snaptime}
Start-azurestorageblobcopy-cloudblob $srcsnap. Icloudblob-destcontainer $DestContainer-destblob $DestBlob
Azure blob Storage Snapshot