保持保留并删除旧的
keep n retention and delete old one
我有下面的代码,它查找 2 个键并删除旧的。这意味着我只有一个数据保留
def delete_old_snap(self, volumeid):
list_snap = self.snapshots()
def doubles(l):
keys = [i["volume_id"] for i in l if i["volume_id"] == volumeid]
keys = {k for k in keys if keys.count(k) > 1}
return zip([[d for d in l if d["volume_id"] == k] for k in keys])
for t in doubles(list_snap):
snap_id_to_delete = t[0][0]['id'] if (
t[0][0]['created_at'] < t[0][1]['created_at']
) else t[0][1]['id']
我的目标是允许像
这样的示例 5 数据保留
def delete_old_snap(self, volumeid, retention=5):
list_snap = self.snapshots()
#keep retention keys (based on ['created_at'] )
#loop for deleting the old one if found.
一个数据样本:
[
{u'status': u'available', u'os-extended-snapshot-attributes:progress': u'100%', u'description': u'Daily snapshot',
u'name': u'snap-DAILY-WEB-OCS_HOME', u'created_at': u'2015-01-22T14:09:30.000000',
u'id': u'02ee7feb-6919-4732-9eb3-8c6f721dc426', u'volume_id': u'edcaac08-5f6a-4bf7-906c-d6ed9cb20b22', u'size': 2,
u'os-extended-snapshot-attributes:project_id': u'a0998a6710f84dc78550393119b41721', u'metadata': {}},
....]
首先创建一个字典,其中每个键都是一个卷 ID
import dateutil.parser as p
d = {}
for snapshot in snapshots:
snapshot["created_at"] = p.parse(snapshot["created_at"])
try:
d[snapshot["volume_id"]].append(snapshot)
except KeyError:
d[snapshot["volume_id"]]=[snapshot]
现在您应该可以更轻松地使用它了
from operator import itemgetter
d2 = {}
for volume,data_list in d.items():
d2[volume] = sorted(data_list,key=itemgetter("created_at"),reverse=True)[:5]
d2 现在应该只包含任何给定卷的 5 个最新快照
我有下面的代码,它查找 2 个键并删除旧的。这意味着我只有一个数据保留
def delete_old_snap(self, volumeid):
list_snap = self.snapshots()
def doubles(l):
keys = [i["volume_id"] for i in l if i["volume_id"] == volumeid]
keys = {k for k in keys if keys.count(k) > 1}
return zip([[d for d in l if d["volume_id"] == k] for k in keys])
for t in doubles(list_snap):
snap_id_to_delete = t[0][0]['id'] if (
t[0][0]['created_at'] < t[0][1]['created_at']
) else t[0][1]['id']
我的目标是允许像
这样的示例 5 数据保留def delete_old_snap(self, volumeid, retention=5):
list_snap = self.snapshots()
#keep retention keys (based on ['created_at'] )
#loop for deleting the old one if found.
一个数据样本:
[
{u'status': u'available', u'os-extended-snapshot-attributes:progress': u'100%', u'description': u'Daily snapshot',
u'name': u'snap-DAILY-WEB-OCS_HOME', u'created_at': u'2015-01-22T14:09:30.000000',
u'id': u'02ee7feb-6919-4732-9eb3-8c6f721dc426', u'volume_id': u'edcaac08-5f6a-4bf7-906c-d6ed9cb20b22', u'size': 2,
u'os-extended-snapshot-attributes:project_id': u'a0998a6710f84dc78550393119b41721', u'metadata': {}},
....]
首先创建一个字典,其中每个键都是一个卷 ID
import dateutil.parser as p
d = {}
for snapshot in snapshots:
snapshot["created_at"] = p.parse(snapshot["created_at"])
try:
d[snapshot["volume_id"]].append(snapshot)
except KeyError:
d[snapshot["volume_id"]]=[snapshot]
现在您应该可以更轻松地使用它了
from operator import itemgetter
d2 = {}
for volume,data_list in d.items():
d2[volume] = sorted(data_list,key=itemgetter("created_at"),reverse=True)[:5]
d2 现在应该只包含任何给定卷的 5 个最新快照