72 lines
1.8 KiB
Plaintext
72 lines
1.8 KiB
Plaintext
|
#!/usr/bin/python
|
||
|
|
||
|
import os
|
||
|
from datetime import timedelta,datetime
|
||
|
import dateutil.parser as dparser
|
||
|
|
||
|
class Filter:
|
||
|
def __init__(self, in_str, delta):
|
||
|
self.in_str = in_str
|
||
|
self.delta = delta
|
||
|
|
||
|
def filter(self, snapshots):
|
||
|
return filter(self._filter, snapshots)
|
||
|
|
||
|
def _filter(self, s):
|
||
|
if self.in_str in s:
|
||
|
ds = dparser.parse(s.replace("mx01","mxXX").replace("G0","go"), fuzzy=True)
|
||
|
return (datetime.now()+self.delta) > ds
|
||
|
return True
|
||
|
|
||
|
def filter_order(snapshots, word_list = ["hourly", "daily", "weekly", "monthly"]):
|
||
|
ds = ""
|
||
|
ds_pos = 0
|
||
|
snapshots_ret = []
|
||
|
snapshots.reverse()
|
||
|
for a in snapshots:
|
||
|
ds_a = a.split("@")[0]
|
||
|
if ds != ds_a:
|
||
|
ds = ds_a
|
||
|
ds_pos = 0
|
||
|
for idx,item in enumerate(word_list):
|
||
|
if item in a:
|
||
|
if ds_pos <= idx:
|
||
|
ds_pos = idx
|
||
|
else:
|
||
|
snapshots_ret.append(a)
|
||
|
break;
|
||
|
snapshots_ret.reverse()
|
||
|
return snapshots_ret
|
||
|
|
||
|
def zfs_destroy(name):
|
||
|
cmd = f"zfs destroy {name}"
|
||
|
print(cmd)
|
||
|
stream = os.popen(cmd)
|
||
|
print(stream.read())
|
||
|
|
||
|
snapshots = []
|
||
|
|
||
|
try:
|
||
|
with open("zfs-snaphosts.txt", "r") as f:
|
||
|
snapshots = [l.strip() for l in f.readlines()]
|
||
|
except IOError:
|
||
|
print("read live")
|
||
|
stream = os.popen('zfs list -r -t snapshot -o name')
|
||
|
snapshots = stream.read().split()[1:]
|
||
|
|
||
|
for a in filter_order(snapshots):
|
||
|
zfs_destroy(a)
|
||
|
|
||
|
f = Filter("hourly", timedelta(hours=-25))
|
||
|
snapshots = list(f.filter(snapshots))
|
||
|
|
||
|
f = Filter("daily", timedelta(days=-8))
|
||
|
snapshots = list(f.filter(snapshots))
|
||
|
|
||
|
f = Filter("weekly", timedelta(weeks=-5))
|
||
|
snapshots = list(f.filter(snapshots))
|
||
|
|
||
|
|
||
|
for a in filter(lambda s: "znap" in s and not "monthly" in s, snapshots):
|
||
|
zfs_destroy(a)
|