Git Product home page Git Product logo

zfs-prune-snapshots's Introduction

ZFS Prune Snapshots

Remove snapshots from one or more zpools that match given criteria

Examples

Remove snapshots older than a week across all zpools

zfs-prune-snapshots 1w

Same as above, but with increased verbosity and without actually deleting any snapshots (dry-run)

zfs-prune-snapshots -vn 1w

Remove snapshots older than 3 weeks on tank1 and tank2/backup. Note that this script will recurse through all of tank1 and all datasets below tank2/backup

zfs-prune-snapshots 3w tank1 tank2/backup

Remove snapshots older than a month on the zones pool that start with the string "autosnap_"

zfs-prune-snapshots -p 'autosnap_' 1M zones

Remove snapshots older than two months on the tank pool that end with the string "_frequent"

zfs-prune-snapshots -s '_frequent' 2M tank

Remove snapshots older than a month on the zones pool that do not start with the string "autosnap_"

zfs-prune-snapshots -i -p 'autosnap_' 1M zones

Timespec

The first argument denotes how old a snapshot must be for it to be considered for deletion - possible specifiers are

  • s seconds
  • m minutes
  • h hours
  • d days
  • w weeks
  • M months
  • y years

Usage

usage: zfs-prune-snapshots [-hnliqRvV] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]

remove snapshots from one or more zpools that match given criteria

examples
    # zfs-prune-snapshots 1w
    remove snapshots older than a week across all zpools

    # zfs-prune-snapshots -vn 1w
    same as above, but with increased verbosity and without
    actually deleting any snapshots (dry-run)

    # zfs-prune-snapshots 3w tank1 tank2/backup
    remove snapshots older than 3 weeks on tank1 and tank2/backup.
    note that this script will recurse through *all* of tank1 and
    *all* datasets below tank2/backup

    # zfs-prune-snapshots -p 'autosnap_' 1M zones
    remove snapshots older than a month on the zones pool that start
    with the string "autosnap_"

    # zfs-prune-snapshots -s '_frequent' 2M tank
    remove snapshots older than two months on the tank pool that end
    with the string "_frequent"

    # zfs-prune-snapshots -i -p 'autosnap_' 1M zones
    remove snapshots older than a month on the zones pool that do not
    start with the string "autosnap_"

timespec
    the first argument denotes how old a snapshot must be for it to
    be considered for deletion - possible specifiers are

        s seconds
        m minutes
        h hours
        d days
        w weeks
        M months
        y years

options
    -h             print this message and exit
    -n             dry-run, don't actually delete snapshots
    -l             list only mode, just list matching snapshots names
                   without deleting (like dry-run mode with machine-parseable output)
    -p <prefix>    snapshot prefix string to match
    -s <suffix>    snapshot suffix string to match
    -i             invert matching of prefix and suffix
    -q             quiet, do not printout removed snapshots
    -R             recursively delete, pass '-R' directly to 'zfs destroy'
    -v             increase verbosity
    -V             print the version number and exit

Development Notes

Style

shellcheck is used to check bash style. Use make check to run the style checker:

$ make check
awk 'length($0) > 80 { exit(1); }' zfs-prune-snapshots
shellcheck zfs-prune-snapshots

Bash style guide: https://www.daveeddy.com/bash/

Manpage

Use make man to regenerate the manpage:

$ make man
md2man-roff man/zfs-prune-snapshots.md > man/zfs-prune-snapshots.1
$ ./man/zfs-prune-snapshots.1
<man page opens>

Manpages are generated with md2man which requires ruby-devel to be installed. Once ruby is installed, md2man can be installed locally with:

$ gem install --user-install md2man

License

MIT License

zfs-prune-snapshots's People

Contributors

ahesford avatar bahamas10 avatar hrio avatar lehoczkics avatar lepokle avatar petschekr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zfs-prune-snapshots's Issues

Feature request - dry-run

It'd be great if we had an option to run dry-run or something else that would show an amount of space taken by zfs snapshots.

Error executing the script

Hi team,

I'm trying to use this script, i do not have any coding experience.
my SunOS version is 11.4. when i'm running the script i got below error

./zfs-prune-snapshots.sh -vn 2M

./zfs-prune-snapshots.sh: line 161: %s: syntax error: operand expected (error token is "%s")

Please help to suggest what went wrong.
Thanks
Rohit

Enhancement - check permissions during dry run

A nifty improvement might be if the dry run option also confirmed whether you had the permissions to actually do the deletion.

e.g. you can do a dry run without sufficient rights and it's a bit misleading.

I do note that the 'native' zfs destroy has the same issue, so perhaps this is either everyone's preferred behaviour, or it's difficult to implement.

Dealing with snapshots that have dependent clones

Hi,
Love the project, extremely useful.

When cleaning up, I am getting alot of snapshot has dependent clones use '-R' to destroy the following datasets:

Would be really helpful if there was a safe was to deal with this in batch.

Thanks!

Tried it and it helped, thank you !

[Feature request] Protect last `x` number of snapshots

When using syncoid, or other send-based backup solution, the last snapshot is used to calculate the delta to send an incremental stream. Using your script in that application would run the risk of destroying snapshots that zfs send/recv needs for incremental backup. It'd be nice if there was a feature to protect the last x number of snapshosts as to ensure their existence regardless of time cut-off point.

Add install procedure

Currently, there is no way to know how to install your code. Is it possible to add a "how to install" section in the readme ? It would be even better to have GitHub releases used (well, and in the best of the worlds a PPA for ubuntu noobs like me 😅, but I do understand if ou don't find that mandatory)

Syntax error with space in pool name.

./zfs-prune-snapshots: line 145: Shows@data_1534448870 1534448871: syntax error: invalid arithmetic operator (error token is "@data_1534448870 1534448871")

I know it's old and I shouldn't have a space, but any chance you could help fix it? I looked thru the code, but I'm not super good with bash.

time for a release?

What do you think, is it time for a first release of this?

I would like to include this script in Alpine Linux and it would help if its tagged.

Dry Run Output shows wrong units

If I ask to prune a number of days, it correctly picks the snapshots to remove, but labels them all '1 week old' rather than 'X days old'

»»»» date
Wed 22 May 2024 10:03:24 AM EDT
»»»» zfs-prune-snapshots -n -R 10d
found 28 snapshots (165 GB) on pools: <all>
[dry-run] [1/28] removing tank/backup@autosnap_2024-05-09_18:02:17_daily: 1 week old (56 KB)
[dry-run] [2/28] removing tank/backup@autosnap_2024-05-10_00:00:20_daily: 1 week old (0 B)
[dry-run] [3/28] removing tank/backup@autosnap_2024-05-11_00:00:17_daily: 1 week old (0 B)
[dry-run] [4/28] removing tank/backup@autosnap_2024-05-12_00:00:27_daily: 1 week old (56 KB)
#snipped...

Feature Request - Counter

It would be nice to have a running counter of how many snapshots are left to remove.

Something like:
removing 81 of 9512 tank/dataset@autosnap_date

Thanks.

How to get it working in cron

I've tried using the correct path /usr/local/bin/zfs-prune-snapshots 1M but then I get the message Error! zfs command not found. Are you on the right machine?

Any clue as to what I'm doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.