Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm currently snapshotting my ZFS-based NAS nightly and weekly, a process that has saved my ass a few times. However, while the creation of the snapshot is automatic (from cron), the deletion of old snapshots is still a manual task. Obviously there's a risk that if I get hit by a bus, or the manual task isn't carried out, the NAS will run out of disk space.

Does anyone have any good ways / scripts they use to manage the number of snapshots stored on their ZFS systems? Ideally, I'd like a script that iterates through all the snapshots for a given ZFS filesystem and deletes all but the last n snapshots for that filesystem.

E.g. I've got two filesystems, one called tank and another called sastank. Snapshots are named with the date on which they were created: sastank@AutoD-2011-12-13 so a simple sort command should list them in order. I'm looking to keep the last 2 week's worth of daily snapshots on tank, but only the last two days worth of snapshots on sastank.

share|improve this question

5 Answers 5

up vote 6 down vote accepted

You may find something like this a little simpler

zfs list -t snapshot -o name -s name |grep ^tank@Auto | sed 1,15d | xargs -n 1 zfs destroy -r` 

Test it with ...|xargs -n 1 echo

share|improve this answer
1  
I think this needs a sort -r before the sed command. sed seems to output the bottom of the list beyond the first 15 lines, which in the default sort is the most recent. Flipping the list means I get the oldest snapshots at the bottom. –  growse Dec 14 '11 at 14:14

More general case of getting most recent snapshot based on creation date, not by name.

zfs list -H -t snapshot -o name -S creation | head -1

Scoped to a specific filesystem name TestOne

zfs list -H -t snapshot -o name -S creation -d1 TestOne | head -1

-H:No header so that first line is a snapshot name

-t snapshot: List snapshots (list can list other things like pools and volumes)

-o name: Display the snapshot name property.

-S creation: Capital S denotes descending sort, based on creation time. This puts most recent snapshot as the first line.

-d1 TestOne: Says include children, which seems confusing but its because as far as this command is concerned, snapshots of TestOne are children. This will NOT list snapshots of volumes within TestOne such as TestOne/SubVol@someSnapshot.

| head -1: Pipe to head and only return first line.

share|improve this answer

I may have solved this with some bash-fu.

 zfs list -t snapshot -o name | grep ^tank@AutoD- | sort -r | wc -l | xargs -n 1 expr -$NUM_TO_KEEP + | tr -d '\n' | xargs -0 -i bash -c "zfs list -t snapshot -o name | grep ^tank@AutoD- | sort -r | tail -n{} | sort |xargs -t -n 1 zfs destroy -r"

Wow. It feels so wrong.

share|improve this answer

growse's didn't work on OpenIndiana for me. It didn't understand -0 for xargs.

If using sort, be aware that it sorts alphabetically which may not be desired as you are probably wanting to find the most recent.

Here is code that will delete all but the last snapshots.

Remove the 'echo' to go live.

RETENTION=5
FS=tank1/test
SNAPNAME=daily-

zfs list -t snapshot -o name | grep ^$FS@${SNAPNAME} |  sed -n -e :a -e '1,${RETENTION}!{P;N;D;};N;ba' | xargs -n 1 echo zfs destroy -r

Sources: http://sed.sourceforge.net/sed1line.txt

share|improve this answer
    
Upvote because anyone who can use sed like that deserves it. –  growse May 25 '12 at 23:05
    
After a recent software update, that sed string stopped working for me and it started deleting all snapshots! bad sed! Luckily the production server was old and stayed safe. I now use sed -n -e :a -e '1,${RETENTION}!{P;N;D;};N;ba' –  Dan Buhler Aug 17 '12 at 19:58

The path for head is needed on Solaris, but should work without the path on other distros.

retention=14
dataset=vmstorage-17/824

zfs list -rt snap -H -o name ${dataset} | \
/usr/gnu/bin/head -n -${retention} | xargs -n 1 zfs destroy -r
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.