Simple script to expire ZFS snapshots older than a certain age

This commit is contained in:
Kris Kennaway 2008-07-26 15:06:41 +00:00
parent 17885ef52d
commit 4663e0b500
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=217602

View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
#
# Expire old snapshots
import zfs, commands, datetime, os
# List of filesystems to expire
expirelist=(("a", 14),
("a/nfs", 14),
("a/src", 14),
("a/local", 14),
("a/ports", 14),
("a/portbuild", 14),
("a/portbuild/amd64", 14),
("a/portbuild/i386", 14),
("a/portbuild/sparc64", 14),
("a/portbuild/ia64", 14),
("a/snap", 7),
("a/snap/ports", 7),
("a/snap/src-5", 7),
("a/snap/src-6", 7),
("a/snap/src-7", 7),
("a/snap/src-HEAD", 7),
("a/snap/world-amd64-HEAD", 7),
("a/snap/world-i386-HEAD", 7))
now = datetime.datetime.now()
for (fs, maxage) in expirelist:
snapdata = zfs.getallsnaps(fs)
snaps = (i[0] for i in snapdata)
for snap in snaps:
try:
snapdate = datetime.datetime.strptime(snap, "%Y%m%d%H%M")
except ValueError:
try:
snapdate = datetime.datetime.strptime(snap, "%Y%m%d%H%M%S")
except ValueError:
continue
if (now - snapdate) > datetime.timedelta(days=maxage):
print "Snapshot %s@%s too old" % (fs, snap)
(err, out) = commands.getstatusoutput("zfs destroy %s@%s" % (fs,snap))
if err:
print "Error deleting snapshot", out