Compare commits

...

4 Commits

Author SHA1 Message Date
Dave Eddy 68e65a66a3 bump to v1.3.0 2021-11-27 19:18:19 -05:00
Dave Eddy 482420faba add -R option for recursive deleting 2021-11-27 19:17:32 -05:00
Dave Eddy 975ed41681 bump to v1.2.0 2021-11-27 18:58:45 -05:00
lepokle 1502ce3c80
Support inverting of prefix/suffix match (#17) 2021-11-27 18:53:51 -05:00
5 changed files with 76 additions and 11 deletions

View File

@ -4,7 +4,18 @@ ZFS Prune Snapshots Changes
Not Yet Released
----------------
- (nothing)
`v1.3.0`
--------
- Add recursive deletion option (-R) (482420faba)
`v1.2.0`
--------
- Add zfs binary check ([#8](https://github.com/bahamas10/zfs-prune-snapshots/pull/8))
- Support inverting of prefix/suffix match ([#17](https://github.com/bahamas10/zfs-prune-snapshots/pull/17))
`v1.1.0`
--------

View File

@ -31,6 +31,11 @@ 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
--------
@ -48,7 +53,7 @@ be considered for deletion - possible specifiers are
Usage
-----
usage: zfs-prune-snapshots [-hnv] [-p <prefix] <time> [[dataset1] ...]
usage: zfs-prune-snapshots [-hniqvV] [-p <prefix] <time> [[dataset1] ...]
remove snapshots from one or more zpools that match given criteria
@ -90,6 +95,7 @@ Usage
-n dry-run, don't actually delete snapshots
-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
-v increase verbosity
-V print the version number and exit

View File

@ -24,9 +24,15 @@ snapshot prefix string to match
\fB\fC\-s <suffix>\fR
snapshot suffix string to match
.TP
\fB\fC\-i\fR
invert matching of prefix and suffix
.TP
\fB\fC\-q\fR
quiet, do not printout removed snapshots
.TP
\fB\fC\-R\fR
recursively delete, pass '\-R' directly to 'zfs destroy'
.TP
\fB\fC\-v\fR
increase verbosity
.TP
@ -72,6 +78,10 @@ string \fB\fC"autosnap_"\fR
\fB\fCzfs\-prune\-snapshots \-s '_frequent' 2M tank\fR
Remove snapshots older than two months on the tank pool that end with the
string \fB\fC"_frequent"\fR
.TP
\fB\fCzfs\-prune\-snapshots \-i \-p 'autosnap_' 1M zones\fR
Remove snapshots older than a month on the zones pool that do not start
with the string \fB\fC"autosnap_"\fR
.SH BUGS
.PP
\[la]https://github.com/bahamas10/zfs-prune-snapshots\[ra]

View File

@ -33,9 +33,15 @@ OPTIONS
`-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
@ -84,6 +90,10 @@ EXAMPLES
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_"`
BUGS
----

View File

@ -6,12 +6,12 @@
# Date: November 20, 2015
# License: MIT
VERSION='v1.1.0'
VERSION='v1.3.0'
usage() {
local prog=${0##*/}
cat <<-EOF
usage: $prog [-hnv] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
usage: $prog [-hniqRvV] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
remove snapshots from one or more zpools that match given criteria
@ -36,6 +36,10 @@ usage() {
remove snapshots older than two months on the tank pool that end
with the string "_frequent"
# $prog -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
@ -53,7 +57,9 @@ usage() {
-n dry-run, don't actually delete snapshots
-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
EOF
@ -100,18 +106,22 @@ if ! type -P zfs &>/dev/null; then
exit 1
fi
recursive=false
dryrun=false
verbosity=0
prefix=
suffix=
invert=false
quiet=false
while getopts 'hnqp:s:vV' option; do
while getopts 'hniqRp:s:vV' option; do
case "$option" in
h) usage; exit 0;;
n) dryrun=true;;
i) invert=true;;
p) prefix=$OPTARG;;
s) suffix=$OPTARG;;
q) quiet=true;;
R) recursive=true;;
v) ((verbosity++));;
V) echo "$VERSION"; exit 0;;
*) usage; exit 1;;
@ -150,20 +160,38 @@ fi
shift
pools=("$@")
destroyargs=()
if $recursive; then
destroyargs+=('-R')
fi
now=$(date +%s)
code=0
while read -r creation snapshot; do
# ensure optional prefix matches
snapname=${snapshot#*@}
if [[ -n $prefix && $prefix != "${snapname:0:${#prefix}}" ]]; then
debug "skipping $snapshot: doesn't match prefix $prefix"
continue
if $invert; then
if [[ -n $prefix && $prefix == "${snapname:0:${#prefix}}" ]]; then
debug "skipping $snapshot: does match prefix $prefix"
continue
fi
else
if [[ -n $prefix && $prefix != "${snapname:0:${#prefix}}" ]]; then
debug "skipping $snapshot: doesn't match prefix $prefix"
continue
fi
fi
# ensure optional suffix matches
if [[ -n $suffix && $suffix != "${snapname: -${#suffix}}" ]]; then
debug "skipping $snapshot: doesn't match suffix $suffix"
continue
if $invert; then
if [[ -n $suffix && $suffix == "${snapname: -${#suffix}}" ]]; then
debug "skipping $snapshot: does match suffix $suffix"
continue
fi
else
if [[ -n $suffix && $suffix != "${snapname: -${#suffix}}" ]]; then
debug "skipping $snapshot: doesn't match suffix $suffix"
continue
fi
fi
# ensure snapshot is older than the cutoff time
@ -184,7 +212,7 @@ while read -r creation snapshot; do
if ! $quiet; then
echo
fi
zfs destroy "$snapshot" || code=1
zfs destroy "${destroyargs[@]}" "$snapshot" || code=1
fi
done < <(zfs list -Hpo creation,name -t snapshot -r "${pools[@]}")
exit "$code"