LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-27-2020, 07:14 PM   #1
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Rep: Reputation: 62
Bash script to manage OpenZFS snapshots


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 0
You could even make it send snapshots to a back up server as well, with a little more tweaking. Let me know what you think.

Last edited by fotoguy; 06-28-2020 at 12:35 AM. Reason: error in script
 
Old 06-29-2020, 03:59 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,907
Blog Entries: 3

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
I'd check to see if you could do the dates using the date utility's own calculations. That might deal with time quirks like leap years better and simplify the AWK part.

Code:
awk -v time="$(date +'%s')" -v T="$(date -d '-1 week' +'%s')" ...
 
Old 06-29-2020, 04:58 AM   #3
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Original Poster
Rep: Reputation: 62
Quote:
Originally Posted by Turbocapitalist View Post
I'd check to see if you could do the dates using the date utility's own calculations. That might deal with time quirks like leap years better and simplify the AWK part.

Code:
awk -v time="$(date +'%s')" -v T="$(date -d '-1 week' +'%s')" ...
That's something worth looking into, I haven't used awk and date command enough to get to know it all that well, so any feedback is greatly appreciated.

Cheers

Last edited by fotoguy; 06-29-2020 at 05:07 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Btrfs vs OpenZFS LXer Syndicated Linux News 0 12-27-2018 05:12 PM
OpenZFS on Linux and BSD zdb command gives space map refcount mismatch User9 Linux - Server 1 01-24-2018 09:00 PM
ZFS / OpenZFS update please? dcnblues Linux - Distributions 3 07-31-2017 06:13 PM
LXer: LinuxCon: OpenZFS moves Open Source Storage Forward LXer Syndicated Linux News 0 09-20-2013 10:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration