客户有这样的需求:每天需要对VM的数据进行备份,但如果备份的时间超过一定的天数,需要进行清除。

本文也是在前一篇Azure Blob Snapshot上的优化。

"Azure blob Storage Snapshot"里,删除Snapshot的思路是遍历所有Storage Account,找到所有的vhd文件,看看是否有Snapshot,如果有判断是否超过14天,如果超过则删除,如果没有超过,则保留。

本文的思路是:

获取所有的虚拟机,判断虚拟机是否运行,如果运行,获取这台虚拟机上所有的硬盘。

如果硬盘的Snapshot超过我们给定的时间,删除,否则不做操作。

具体的脚本如下:

 function DeleteOlderSnapshot {
[CmdletBinding()]
param (
[Parameter (Mandatory=$True,
ValueFromPipeLine=$True,
HelpMessage="BeforeTheDayWillBeDelete")] [String] $Day,
[switch] $LogErrors
)
BEGIN{
Write-Verbose "Error Log will Be $ErrorLog"
}
Process
{ $vms = Get-AzureVM
$now = Get-Date
foreach ($vm in $vms)
{
if($vm.Status -eq "ReadyRole")
{
$osblobname = ($vm.VM.OSVirtualHardDisk).MediaLink.ToString().Split("/")[-1]
$osblobcontainer = ($vm.VM.OSVirtualHardDisk).MediaLink.ToString().Split("/")[-2]
$osblobsa = ($vm.VM.OSVirtualHardDisk).MediaLink.ToString().Split("/")[-3].Split(".")[0] $storagekey = (Get-AzureStorageKey -StorageAccountName $osblobsa).Primary
$ctr = New-AzureStorageContext -StorageAccountName $osblobsa -StorageAccountKey $storagekey -Environment AzureChinaCloud $blobs = Get-AzureStorageBlob -Container $osblobcontainer -Context $ctr | Where-Object {$_.Name -match $osblobname} foreach ($blob in $blobs)
{
if($blob.ICloudBlob.IsSnapshot)
{ $diff = [datetime]::FromBinary($now.Ticks-$blob.SnapshotTime.Ticks)
write-host $blob.name "is a snapshot" "snapshot time is" $diff.DayOfYear "days"
if ( $diff.dayofyear -ge $Day)
{ write-host "delete"
$blob.ICloudBlob.Delete()
} }else{
write-host $blob.Name "is original disk"
}
} $count = $vm.VM.DataVirtualHardDisks.Count for($i=0;$i -lt $count;$i++)
{
$datablobname = ($vm.VM.DataVirtualHardDisks)[$i].MediaLink.ToString().Split("/")[-1]
$datablobcontainer = ($vm.VM.DataVirtualHardDisks)[$i].MediaLink.ToString().Split("/")[-2]
$datablobsa = ($vm.VM.DataVirtualHardDisks)[$i].MediaLink.ToString().Split("/")[-3].Split(".")[0]
$storagekey = (Get-AzureStorageKey -StorageAccountName $datablobsa).Primary
$ctr = New-AzureStorageContext -StorageAccountName $datablobsa -StorageAccountKey $storagekey -Environment AzureChinaCloud $blobs = Get-AzureStorageBlob -Container $datablobcontainer -Context $ctr | Where-Object {$_.Name -match $datablobname}
foreach ($blob in $blobs)
{
if($blob.ICloudBlob.IsSnapshot)
{ $diff = [datetime]::FromBinary($now.Ticks-$blob.SnapshotTime.Ticks)
write-host $blob.name "is a snapshot" "snapshot time is" $diff.DayOfYear "days"
if ( $diff.dayofyear -ge $Day)
{ write-host "delete"
$blob.ICloudBlob.Delete()
} }else{
write-host $blob.Name "is original disk"
}
}
}
}
} }
end {}
}
DeleteOlderSnapshot -Day 2

删除老的Azure Blob Snapshot的更多相关文章

  1. 创建Azure Blob Snapshot的脚本

    在前面的文章中介绍了如何创建Azure Blob Snapshot.那篇文章中创建的脚本思路是:遍历所有Storage Account,找到所有vhd文件,进行Snapshot的创建. 但这种方式不够 ...

  2. Azure blob Storage Snapshot

    用户虚拟机硬盘的备份是客户在部署云应用中是一个非常重要的部分. 目前有多种平台的备份方法: 捕获镜像:可以采用Capture的方式(powershell命令为Save-AzureVMImage)捕获虚 ...

  3. Azure Blob Storage 基本用法 -- Azure Storage 之 Blob

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure Table storage ...

  4. [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传

    <Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...

  5. Azure Blob Storage从入门到精通

    今天推荐的是一个系列文章,让读者阅读完成后可以对Azure Blob Storage的开发有一个全面的了解,可谓是从入门到精通. Azure在最初的版本里面就提供了非结构化数据的存储服务,也即Blob ...

  6. Python 操作 Azure Blob Storage

    笔者在<Azure 基础:Blob Storage>一文中介绍了 Azure Blob Storage 的基本概念,并通过 C# 代码展示了如何进行基本的操作.最近笔者需要在 Linux ...

  7. Windows Azure Storage (24) 启用Azure Blob日志

    <Windows Azure Platform 系列文章目录> 之前有一个业务需求,客户想知道Azure Storage是否有日志功能,可以检查某个Azure Blob文件在某个时间点被删 ...

  8. [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage

    这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...

  9. Azure上MySQL的离线备份:将备份拷贝到Azure Blob上

    公司在Azure的Iaas虚拟机上部署有好几台MySQL数据库,至于没有选择Azure Database for MySQL,是因为预算有限(钱不够啊!说多了也是泪,坑的还是DBA自己).选择了Iaa ...

随机推荐

  1. python __name__及__main()__的妙处

    #hello.py def sayHello(): str="hello" print(str); if __name__ == "__main__": pri ...

  2. vs+opencv

    vs + opencv 配置见网页 https://blog.csdn.net/qq_17550379/article/details/78201442 统一所有工程配置见下图:

  3. python2 生成验证码图片

    使用pillow或者pil库编写 #coding:utf-8 #use pillow or pil try: from PIL import Image, ImageDraw, ImageFont, ...

  4. 为什么下了android 4.1 的SDK后在本地用浏览器看api说明文档时,浏览器打开api的html文件很慢?试了好几款浏览器都一样。为什么?

    http://www.oschina.net/question/436724_61401 http://www.google.com/jsapi  他惹的祸 注释掉就可以了- <!-- < ...

  5. 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array

    描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...

  6. spring-boot2

    1.Spring Boot 1.1.什么是Spring Boot Java是静态语言,先变异后运行都是静态语言,不编译直接运行是动态语言(js是动态语言不需要编译,因为浏览器可以直接解析).Java笨 ...

  7. 汇编语言入门(在debug中编辑和调试程序)

    2013-06-02 17:09 4252人阅读 评论(2) 收藏 举报  分类: 汇编语言(1)  版权声明:本文为博主原创文章,未经博主允许不得转载. 我们在Windows中进入的Dos方式,实际 ...

  8. mac工作软件推荐-iterm + zsh + tmux

    原文链接: http://ju.outofmemory.cn/entry/57244 tmux安装https://blog.csdn.net/nmgzywd/article/details/50915 ...

  9. EntityFramework 学习 一 Entity Relationships 实体的关系

    下面,我们学习Entity Framework怎么管理实体间的关系 Entity Framework支持三种关系:一对一的关系.一对多的关系.多对多的关系 前面我们创建SchoolDB的实体数据模型, ...

  10. 矩阵内积和Schur补

    > Many problems in the field of signal processing have been expended into matrix problems.So it's ...