Support suffix matching in the same style as prefix matching.

This commit is contained in:
Andrew J. Hesford 2019-12-16 11:19:53 -05:00
parent 2a01496c41
commit e7aa72160f
4 changed files with 39 additions and 2 deletions

View File

@ -26,6 +26,11 @@ 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
Timespec
--------
@ -64,6 +69,10 @@ Usage
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"
timespec
the first argument denotes how old a snapshot must be for it to
be considered for deletion - possible specifiers are
@ -80,6 +89,7 @@ Usage
-h print this message and exit
-n dry-run, don't actually delete snapshots
-p <prefix> snapshot prefix string to match
-s <suffix> snapshot suffix string to match
-q quiet, do not printout removed snapshots
-v increase verbosity
-V print the version number and exit

View File

@ -21,6 +21,9 @@ dry\-run, don't actually delete snapshots
\fB\fC\-p <prefix>\fR
snapshot prefix string to match
.TP
\fB\fC\-s <suffix>\fR
snapshot suffix string to match
.TP
\fB\fC\-q\fR
quiet, do not printout removed snapshots
.TP
@ -65,6 +68,10 @@ tank2/backup
\fB\fCzfs\-prune\-snapshots \-p 'autosnap_' 1M zones\fR
Remove snapshots older than a month on the zones pool that start with the
string \fB\fC"autosnap_"\fR
.TP
\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
.SH BUGS
.PP
\[la]https://github.com/bahamas10/zfs-prune-snapshots\[ra]

View File

@ -30,6 +30,9 @@ OPTIONS
`-p <prefix>`
snapshot prefix string to match
`-s <suffix>`
snapshot suffix string to match
`-q`
quiet, do not printout removed snapshots
@ -77,6 +80,10 @@ EXAMPLES
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"`
BUGS
----

View File

@ -11,7 +11,7 @@ VERSION='v1.0.1'
usage() {
local prog=${0##*/}
cat <<-EOF
usage: $prog [-hnv] [-p <prefix] <time> [[dataset1] ...]
usage: $prog [-hnv] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
remove snapshots from one or more zpools that match given criteria
@ -32,6 +32,10 @@ usage() {
remove snapshots older than a month on the zones pool that start
with the string "autosnap_"
# $prog -s '_frequent' 2M tank
remove snapshots older than two months on the tank pool that end
with the string "_frequent"
timespec
the first argument denotes how old a snapshot must be for it to
be considered for deletion - possible specifiers are
@ -48,6 +52,7 @@ usage() {
-h print this message and exit
-n dry-run, don't actually delete snapshots
-p <prefix> snapshot prefix string to match
-s <suffix> snapshot suffix string to match
-q quiet, do not printout removed snapshots
-v increase verbosity
-V print the version number and exit
@ -93,12 +98,14 @@ human() {
dryrun=false
verbosity=0
prefix=
suffix=
quiet=false
while getopts 'hnqp:vV' option; do
while getopts 'hnqp:s:vV' option; do
case "$option" in
h) usage; exit 0;;
n) dryrun=true;;
p) prefix=$OPTARG;;
s) suffix=$OPTARG;;
q) quiet=true;;
v) ((verbosity++));;
V) echo "$VERSION"; exit 0;;
@ -148,6 +155,12 @@ while read -r creation snapshot; do
continue
fi
# ensure optional suffix matches
if [[ -n $suffix && $suffix != "${snapname: -${#suffix}}" ]]; then
debug "skipping $snapshot: doesn't match suffix $suffix"
continue
fi
# ensure snapshot is older than the cutoff time
delta=$((now - creation))
human=$(human "$delta")