Article 553WT Bash script to manage OpenZFS snapshots

Bash script to manage OpenZFS snapshots

by
fotoguy
from LinuxQuestions.org on (#553WT)
Hey everyone, over the last couple of months I have using openZFS filesystem on debian buster/sid on my daily drivers for testing. Really loving ZFS so far, the snapshots for me are the biggest selling point.

But as time goes by, the snapshots are also growing, which can become a problem themselves if left for too long. So I looked around the net for some answer on how to remove the snapshots with a bash script based on age, but everything I found seem so complicated, and way too many lines of code.

In the end I managed to streamline it down to a few lines of code which seems to work well, and thought others might find it useful since zfs seems to be gaining more popularity over the last couple of years.

Code:#!/bin/bash

ZPOOL=zpool

# how many seconds
day=86400
week=604800
month=2592000
year=31536000

# list all snapshots
zfs list -Hpo creation,name -t snapshot -r $ZPOOL | sort -r > /tmp/found.txt

# print older snapshots to screen ( non-destructive)
awk -v time="$(date +"%s")" -v T="$week" '{if (time - $1 > T){print $1,$2}}' /tmp/found.txt

# delete older snapshots (destructive)
#awk -v time="$(date +"%s")" -v T="$week" '{if (time - $1 > T){cmd="zfs destroy" $2; system(cmd)}}' /tmp/found.txt
rm -rf /tmp/found.txt
exit 0You could even make it send snapshots to a back up server as well, with a little more tweaking. Let me know what you think.latest?d=yIl2AUoC8zA latest?i=GM4fv_WH41w:N-_7L3ncgO8:F7zBnMy latest?i=GM4fv_WH41w:N-_7L3ncgO8:V_sGLiP latest?d=qj6IDK7rITs latest?i=GM4fv_WH41w:N-_7L3ncgO8:gIN9vFwGM4fv_WH41w
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments