如何通过设置保留期限来删除带有标签的快照

时间:2019-06-11 13:47:21

标签: python amazon-web-services snapshot

我有一个脚本,可以在保留期后删除快照。它运作良好,并删除了超过保留期限的快照。但是我必须用标签过滤它。意味着仅应删除具有特定标签的快照。

import matplotlib.colors as mcol
import matplotlib.pyplot as plt
import numpy as np

# dummy data, a sine, 0 in the origin, 1 close to NE corner
x = np.linspace(0, 1, N)
X, Y = np.meshgrid(x, x)
R = np.sqrt(X**2+Y**2)
Z = np.sin(R*3/2)

# the levels that I want to draw on the plot,
# the levels, except for the extremes, are quite arbitrary
# as arbitrary do they seem your level specifications
levels = [0.0, .5, .6, .75, .85, .95, 1.0]
# your colormap
cmap = mcol.ListedColormap(['#2c7bb6', '#0a793a', '#77a353',
                            '#f1d499', '#c96a33', '#975114'])
# the nuber of intervals must be equal to the number of listed colors
assert(len(levels)-1==cmap.N)
# the norm that we use to map values to colors, see the docs    
norm = mcol.BoundaryNorm(levels, cmap.N)

# we are ready to plot
plt.contourf(X, Y, Z, cmap=cmap, levels=levels, norm=norm)
plt.colorbar()

# this is not necessary for your problem but it;s nice in my example    
p = plt.contour(X, Y, Z, levels=levels, colors='k')
plt.clabel(p, inline=1)

#  T H E   E N D
plt.show()

我只想使用“ DeleteOn”标签删除快照。但是此脚本会删除所有通过保留期的内容。它不检查标签部分。

请检查并提供帮助。 谢谢。

1 个答案:

答案 0 :(得分:0)

如果您询问如何修复代码,以便仅删除以下快照:

  • 具有给定标签 AND
  • 已通过保留期

然后仔细查看您的代码。

此部分:

# Get list of Snaps with Tag 'globalVars['findNeedle']'
snaps_to_remove = ec2_client.describe_snapshots(OwnerIds=account_ids,Filters=filters)

正在按标签获取快照列表。太好了!

然后这部分:

# Get the snaps that doesn't have the tag and are older than Retention days
all_snaps = ec2_client.describe_snapshots(OwnerIds=account_ids)
for snap in all_snaps['Snapshots']:
    if snap['StartTime'].strftime('%Y-%m-%d') <= snap_older_than_RetentionDays:
        snaps_to_remove['Snapshots'].append(snap)

正在获取快照的新列表,并检查保留情况。

然后,结果snaps_to_remove包含两个结果。

您将需要组合您的逻辑,以便仅添加同时满足两个条件的快照,而不是分别编译快照列表。