Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39549
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation in the Cloud
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
24
25
# Delete all snapshots of a given ZFS filesystem but **NOT** of descendant filesystems
26
# Or -- if filesystem='90d', delete all snapshots of all filesystems whose name ends in "--90d".
27
28
import sys, time
29
from subprocess import Popen, PIPE
30
31
32
def cmd(v):
33
t = time.time()
34
print ' '.join(v),
35
sys.stdout.flush()
36
out = Popen(v,stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
37
x = out.stdout.read()
38
y = out.stderr.read()
39
e = out.wait()
40
if e:
41
raise RuntimeError(y)
42
print " (%.2f seconds)"%(time.time()-t)
43
return x
44
45
def delete_snapshots(filesystem):
46
47
if filesystem == '90d':
48
print "deleting all snapshots of any filesystem in any pool that contain '--90d\\t'"
49
x = cmd(['zfs', 'list', '-H', '-r', '-t', 'snapshot'])
50
51
# take only those ending in --90d
52
lines = [t for t in x.splitlines() if '--90d\t'in t]
53
54
else:
55
print "deleting snapshots of filesystem %s"%filesystem
56
x = cmd(['zfs', 'list', '-H', '-r', '-t', 'snapshot', filesystem])
57
58
# get rid of descendant filesystems in list.
59
lines = [t for t in x.splitlines() if filesystem+"@" in t]
60
61
total = len(lines)
62
print "%s snapshots to delete"%total
63
64
i = 0
65
for a in lines:
66
if a:
67
snapshot = a.split()[0]
68
print snapshot
69
cmd(['zfs', 'destroy', snapshot])
70
i += 1
71
print "%s/%s"%(i,total)
72
73
for filesystem in sys.argv[1:]:
74
delete_snapshots(filesystem)
75
76
77