add -l for "list" mode, '-q' now fully squelches stdout

This commit is contained in:
Dave Eddy 2021-11-30 22:55:57 -05:00
parent b5f0f8f097
commit e4ba772f2c
1 changed files with 23 additions and 15 deletions

View File

@ -65,10 +65,6 @@ usage() {
EOF EOF
} }
log() {
$quiet || echo "$@"
}
debug() { debug() {
((verbosity >= 1)) && echo "$@" ((verbosity >= 1)) && echo "$@"
return 0 return 0
@ -133,19 +129,20 @@ fi
recursive=false recursive=false
dryrun=false dryrun=false
listonly=false
verbosity=0 verbosity=0
prefix= prefix=
suffix= suffix=
invert=false invert=false
quiet=false while getopts 'hniqlRp:s:vV' option; do
while getopts 'hniqRp:s:vV' option; do
case "$option" in case "$option" in
h) usage; exit 0;; h) usage; exit 0;;
n) dryrun=true;; n) dryrun=true;;
i) invert=true;; i) invert=true;;
l) listonly=true;;
p) prefix=$OPTARG;; p) prefix=$OPTARG;;
s) suffix=$OPTARG;; s) suffix=$OPTARG;;
q) quiet=true;; q) exec 1>/dev/null;;
R) recursive=true;; R) recursive=true;;
v) ((verbosity++));; v) ((verbosity++));;
V) echo "$VERSION"; exit 0;; V) echo "$VERSION"; exit 0;;
@ -236,38 +233,49 @@ while read -r line; do
continue continue
fi fi
# print the snapshot here if `-l`
if $listonly; then
echo "$snapshot"
fi
# we care about this dataset # we care about this dataset
((totalused += used)) ((totalused += used))
((numsnapshots++)) ((numsnapshots++))
lines+=("$line") lines+=("$line")
done < <(zfs list -Hpo creation,name,used -t snapshot -r "${pools[@]}") done < <(zfs list -Hpo creation,name,used -t snapshot -r "${pools[@]}")
# finish if running with `-l`
if $listonly; then
debug "running in '-l' mode - exiting here"
exit 0
fi
humantotal=$(human-size "$totalused") humantotal=$(human-size "$totalused")
log "found $numsnapshots snapshots ($humantotal) on pools: ${humanpools:-<all>}" echo "found $numsnapshots snapshots ($humantotal) on pools: ${humanpools:-<all>}"
# process snapshots found # process snapshots found
i=1 i=0
for line in "${lines[@]}"; do for line in "${lines[@]}"; do
read -r creation snapshot used _ <<< "$line" read -r creation snapshot used _ <<< "$line"
((i++))
delta=$((now - creation)) delta=$((now - creation))
humantime=$(human-time "$delta") humantime=$(human-time "$delta")
humanused=$(human-size "$used") humanused=$(human-size "$used")
if $dryrun; then if $dryrun; then
log -n '[dry-run] ' echo -n '[dry-run] '
fi fi
log "[$i/$numsnapshots] removing $snapshot: $humantime old ($humanused)" echo "[$i/$numsnapshots] removing $snapshot: $humantime old ($humanused)"
if ! $dryrun; then if ! $dryrun; then
zfs destroy "${destroyargs[@]}" "$snapshot" || code=1 echo zfs destroy "${destroyargs[@]}" "$snapshot" || code=1
fi fi
((i++))
done done
log "processed $numsnapshots snapshots ($humantotal)" echo "processed $numsnapshots snapshots ($humantotal)"
exit "$code" exit "$code"