Compare commits

...

3 Commits

Author SHA1 Message Date
Dave Eddy 89b9c8a416 validate datasets exist before running
- this allows for early exit as well as better error messaging
2022-06-12 15:05:07 -04:00
Dave Eddy 701762f1ec bump version 2022-06-12 14:34:55 -04:00
Ryan Petschek a9d24fc81a
Allow snapshot datasets to contain spaces (#18)
Previously, the `read -r` command ignored content after the first space in a dataset/snapshot name. For example, when trying to remove `MyPool/Media/TV Shows@snapshot-name`, the program would try to instead destroy `MyPool/Media/TV` which (probably) doesn't exist and is a dataset as opposed to a snapshot. This change should be safe because the `zfs list` command will not return any extra information after the creation time, space used, and snapshot name, in that order.
2022-06-12 14:33:29 -04:00
2 changed files with 49 additions and 8 deletions

View File

@ -6,6 +6,19 @@ Not Yet Released
(nothing yet)
`v1.5.0`
--------
- Sanity check datasets existence before running
- `-q` will hide warnings for datesets not existing
- Based on ([#9](https://github.com/bahamas10/zfs-prune-snapshots/pull/9))
`v1.4.1`
--------
- Allow snapshot datasets to contain spaces
([#18](https://github.com/bahamas10/zfs-prune-snapshots/pull/18))
`v1.4.0`
--------

View File

@ -6,7 +6,7 @@
# Date: November 20, 2015
# License: MIT
VERSION='v1.4.0'
VERSION='v1.4.1'
usage() {
local prog=${0##*/}
@ -69,7 +69,7 @@ EOF
}
debug() {
((verbosity >= 1)) && echo "$@" >&2
((verbosity >= 1)) && echo '>' "$@" >&2
return 0
}
@ -174,6 +174,7 @@ verbosity=0
prefix=
suffix=
invert=false
quiet=false
while getopts 'hniqlRp:s:vV' option; do
case "$option" in
h) usage; exit 0;;
@ -182,7 +183,7 @@ while getopts 'hniqlRp:s:vV' option; do
l) listonly=true;;
p) prefix=$OPTARG;;
s) suffix=$OPTARG;;
q) exec 1>/dev/null;;
q) quiet=true; exec 1>/dev/null;;
R) recursive=true;;
v) ((verbosity++));;
V) echo "$VERSION"; exit 0;;
@ -221,13 +222,10 @@ fi
shift
pools=("$@")
destroyargs=()
code=0
totalused=0
numsnapshots=0
humanpools=${pools[*]}
humanpools=${humanpools:-<all>}
if $recursive; then
destroyargs+=('-R')
@ -243,10 +241,40 @@ if ! command -v zfs &>/dev/null; then
exit 1
fi
# validate pools given, print warnings if not in quiet mode
pools=()
for arg in "$@"; do
debug "checking '$arg' exists"
error=$(zfs list "$arg" 2>&1 1>/dev/null)
code=$?
debug "zfs list '$arg' -> exited $code"
if ((code == 0)) && [[ -z $error ]]; then
debug "adding dataset '$arg'"
pools+=("$arg")
else
msg="dataset '$arg' invalid: $error"
debug "$msg"
if ! $quiet; then
echo "$msg" >&2
fi
fi
done
# it is an error if arguments were given but all datasets were invalidated
if [[ -n $1 && -z ${pools[0]} ]]; then
echo 'no valid dataset names provided' >&2
exit 1
fi
humanpools=${pools[*]}
humanpools=${humanpools:-<all>}
# first pass of the pools (to calculate totals and filter unwanted datasets
lines=()
while read -r line; do
read -r creation used snapshot _ <<< "$line"
read -r creation used snapshot <<< "$line"
# ensure optional prefix matches
snapname=${snapshot#*@}
@ -317,7 +345,7 @@ echo "found $numsnapshots snapshots ($humantotal) on pools: $humanpools"
# process snapshots found
i=0
for line in "${lines[@]}"; do
read -r creation used snapshot _ <<< "$line"
read -r creation used snapshot <<< "$line"
((i++))