Compare commits
9 Commits
3754b8f063
...
f1be6dc83d
Author | SHA1 | Date |
---|---|---|
Dave Eddy | f1be6dc83d | |
Dave Eddy | 547d01009e | |
Dave Eddy | 7867fc3d07 | |
Dave Eddy | 555988063b | |
Dave Eddy | 198ba41245 | |
Dave Eddy | e8e035c9a2 | |
Dave Eddy | 5c3f3d934f | |
Dave Eddy | e4ba772f2c | |
Dave Eddy | b5f0f8f097 |
15
CHANGES.md
15
CHANGES.md
|
@ -4,23 +4,30 @@ ZFS Prune Snapshots Changes
|
|||
Not Yet Released
|
||||
----------------
|
||||
|
||||
- Show snapshot size (used) and number of snapshots processed
|
||||
(nothing yet)
|
||||
|
||||
`v1.4.0`
|
||||
--------
|
||||
|
||||
- Show snapshot size (used) and number of snapshots processed (6e3891ddd1)
|
||||
- Add list only option (`-l`) to just list datasets that match (e4ba772f2c)
|
||||
|
||||
`v1.3.0`
|
||||
--------
|
||||
|
||||
- Add recursive deletion option (-R) (482420faba)
|
||||
- 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))
|
||||
- Support inverting of prefix/suffix match (`-i`)
|
||||
([#17](https://github.com/bahamas10/zfs-prune-snapshots/pull/17))
|
||||
|
||||
`v1.1.0`
|
||||
--------
|
||||
|
||||
- Support suffix matching (-s) (e7aa72160f8)
|
||||
- Support suffix matching (`-s`) (e7aa72160f8)
|
||||
|
||||
`v1.0.1`
|
||||
--------
|
||||
|
|
11
README.md
11
README.md
|
@ -53,7 +53,7 @@ be considered for deletion - possible specifiers are
|
|||
Usage
|
||||
-----
|
||||
|
||||
usage: zfs-prune-snapshots [-hniqvV] [-p <prefix] <time> [[dataset1] ...]
|
||||
usage: zfs-prune-snapshots [-hnliqRvV] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
|
||||
|
||||
remove snapshots from one or more zpools that match given criteria
|
||||
|
||||
|
@ -78,6 +78,10 @@ Usage
|
|||
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
|
||||
|
@ -93,10 +97,13 @@ Usage
|
|||
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
|
||||
-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
|
||||
|
||||
|
|
|
@ -18,6 +18,10 @@ print this message and exit
|
|||
\fB\fC\-n\fR
|
||||
dry\-run, don't actually delete snapshots
|
||||
.TP
|
||||
\fB\fC\-l\fR
|
||||
list only mode, just list matching snapshots names without deleting (like
|
||||
dry\-run mode with machine\-parseable output)
|
||||
.TP
|
||||
\fB\fC\-p <prefix>\fR
|
||||
snapshot prefix string to match
|
||||
.TP
|
||||
|
|
|
@ -27,6 +27,10 @@ OPTIONS
|
|||
`-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
|
||||
|
||||
|
|
|
@ -6,67 +6,111 @@
|
|||
# Date: November 20, 2015
|
||||
# License: MIT
|
||||
|
||||
VERSION='v1.3.0'
|
||||
VERSION='v1.4.0'
|
||||
|
||||
usage() {
|
||||
local prog=${0##*/}
|
||||
cat <<-EOF
|
||||
usage: $prog [-hniqRvV] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
|
||||
cat <<EOF
|
||||
usage: $prog [-hnliqRvV] [-p <prefix>] [-s <suffix>] <time> [[dataset1] ...]
|
||||
|
||||
remove snapshots from one or more zpools that match given criteria
|
||||
remove snapshots from one or more zpools that match given criteria
|
||||
|
||||
examples
|
||||
# $prog 1w
|
||||
remove snapshots older than a week across all zpools
|
||||
examples
|
||||
# $prog 1w
|
||||
remove snapshots older than a week across all zpools
|
||||
|
||||
# $prog -vn 1w
|
||||
same as above, but with increased verbosity and without
|
||||
actually deleting any snapshots (dry-run)
|
||||
# $prog -vn 1w
|
||||
same as above, but with increased verbosity and without
|
||||
actually deleting any snapshots (dry-run)
|
||||
|
||||
# $prog 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
|
||||
# $prog 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
|
||||
|
||||
# $prog -p 'autosnap_' 1M zones
|
||||
remove snapshots older than a month on the zones pool that start
|
||||
with the string "autosnap_"
|
||||
# $prog -p 'autosnap_' 1M zones
|
||||
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"
|
||||
# $prog -s '_frequent' 2M tank
|
||||
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_"
|
||||
# $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
|
||||
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
|
||||
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
|
||||
-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
|
||||
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
|
||||
EOF
|
||||
}
|
||||
|
||||
debug() {
|
||||
((verbosity >= 1)) && echo "$@"
|
||||
((verbosity >= 1)) && echo "$@" >&2
|
||||
return 0
|
||||
}
|
||||
|
||||
# get epoch time
|
||||
get-epoch() {
|
||||
local time=0
|
||||
|
||||
# try bash built-in, date(1), and finally perl
|
||||
time=$(printf '%(%s)T\n' -1 2>/dev/null)
|
||||
_validate-epoch "$time" 'printf' && return 0
|
||||
|
||||
time=$(date '+%s' 2>/dev/null)
|
||||
_validate-epoch "$time" 'date' && return 0
|
||||
|
||||
time=$(perl -le 'print time' 2>/dev/null)
|
||||
_validate-epoch "$time" 'perl' && return 0
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# validate a given epoch time for sanity
|
||||
_validate-epoch() {
|
||||
local time=$1
|
||||
local method=$2
|
||||
local num_re='^([0-9]+)$'
|
||||
|
||||
debug "checking time received from $method"
|
||||
|
||||
if [[ -z $time ]]; then
|
||||
debug "time invalid - empty"
|
||||
return 1
|
||||
elif ! [[ $time =~ $num_re ]]; then
|
||||
debug "time invalid - not a number :: '$time'"
|
||||
return 1
|
||||
elif ! (( time > 0 )); then
|
||||
debug "time invalid - not greater than 0 :: '$time'"
|
||||
return 1
|
||||
fi
|
||||
|
||||
debug "successfully got epoch time :: $time"
|
||||
echo "$time"
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -101,6 +145,7 @@ human-time() {
|
|||
echo '0 seconds'
|
||||
}
|
||||
|
||||
# convert bytes to a human-readable string
|
||||
human-size() {
|
||||
local bytes=$1
|
||||
|
||||
|
@ -122,26 +167,22 @@ human-size() {
|
|||
echo '0 B'
|
||||
}
|
||||
|
||||
if ! type -P zfs &>/dev/null; then
|
||||
echo "Error! zfs command not found. Are you on the right machine?"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
recursive=false
|
||||
dryrun=false
|
||||
listonly=false
|
||||
verbosity=0
|
||||
prefix=
|
||||
suffix=
|
||||
invert=false
|
||||
quiet=false
|
||||
while getopts 'hniqRp:s:vV' option; do
|
||||
while getopts 'hniqlRp:s:vV' option; do
|
||||
case "$option" in
|
||||
h) usage; exit 0;;
|
||||
n) dryrun=true;;
|
||||
i) invert=true;;
|
||||
l) listonly=true;;
|
||||
p) prefix=$OPTARG;;
|
||||
s) suffix=$OPTARG;;
|
||||
q) quiet=true;;
|
||||
q) exec 1>/dev/null;;
|
||||
R) recursive=true;;
|
||||
v) ((verbosity++));;
|
||||
V) echo "$VERSION"; exit 0;;
|
||||
|
@ -179,73 +220,122 @@ else
|
|||
fi
|
||||
|
||||
shift
|
||||
pools=("$@")
|
||||
|
||||
pools=("$@")
|
||||
destroyargs=()
|
||||
code=0
|
||||
totalused=0
|
||||
numsnapshots=0
|
||||
humanpools=${pools[*]}
|
||||
humanpools=${humanpools:-<all>}
|
||||
|
||||
if $recursive; then
|
||||
destroyargs+=('-R')
|
||||
fi
|
||||
now=$(date +%s)
|
||||
code=0
|
||||
totalused=0
|
||||
count=0
|
||||
while read -r creation snapshot used _; do
|
||||
|
||||
if ! now=$(get-epoch); then
|
||||
echo 'failed to get epoch time' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v zfs &>/dev/null; then
|
||||
echo "Error! zfs command not found. Are you on the right machine?" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# first pass of the pools (to calculate totals and filter unwanted datasets
|
||||
lines=()
|
||||
while read -r line; do
|
||||
read -r creation used snapshot _ <<< "$line"
|
||||
|
||||
# ensure optional prefix matches
|
||||
snapname=${snapshot#*@}
|
||||
if $invert; then
|
||||
if [[ -n $prefix && $prefix == "${snapname:0:${#prefix}}" ]]; then
|
||||
debug "skipping $snapshot: does match prefix $prefix"
|
||||
if [[ -n $prefix ]]; then
|
||||
match=false
|
||||
if [[ $prefix == "${snapname:0:${#prefix}}" ]]; then
|
||||
match=true
|
||||
fi
|
||||
|
||||
if $invert && $match; 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"
|
||||
|
||||
if ! $invert && ! $match; then
|
||||
debug "skipping $snapshot: doesn't match prefix '$prefix'"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
# ensure optional suffix matches
|
||||
if $invert; then
|
||||
if [[ -n $suffix && $suffix == "${snapname: -${#suffix}}" ]]; then
|
||||
debug "skipping $snapshot: does match suffix $suffix"
|
||||
if [[ -n $suffix ]]; then
|
||||
match=false
|
||||
if [[ $suffix == "${snapname: -${#suffix}}" ]]; then
|
||||
match=true
|
||||
fi
|
||||
|
||||
if $invert && $match; 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"
|
||||
|
||||
if ! $invert && ! $match; then
|
||||
debug "skipping $snapshot: doesn't match suffix '$suffix'"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
|
||||
# ensure snapshot is older than the cutoff time
|
||||
delta=$((now - creation))
|
||||
human=$(human-time "$delta")
|
||||
ht=$(human-time "$delta")
|
||||
if ((delta <= seconds)); then
|
||||
debug "skipping $snapshot: $human old"
|
||||
debug "skipping $snapshot: $ht old"
|
||||
continue
|
||||
fi
|
||||
|
||||
# remove the snapshot
|
||||
humanused=$(human-size "$used")
|
||||
|
||||
if ! $quiet || $dryrun; then
|
||||
echo -n "removing $snapshot: $human old ($humanused)"
|
||||
fi
|
||||
if $dryrun; then
|
||||
echo ' <dry-run: no action taken>'
|
||||
else
|
||||
if ! $quiet; then
|
||||
echo
|
||||
fi
|
||||
zfs destroy "${destroyargs[@]}" "$snapshot" || code=1
|
||||
# print the snapshot here if `-l`
|
||||
if $listonly; then
|
||||
echo "$snapshot"
|
||||
fi
|
||||
|
||||
((total += used))
|
||||
((count++))
|
||||
done < <(zfs list -Hpo creation,name,used -t snapshot -r "${pools[@]}")
|
||||
# we care about this dataset
|
||||
((totalused += used))
|
||||
((numsnapshots++))
|
||||
lines+=("$line")
|
||||
done < <(zfs list -Hpo creation,used,name -t snapshot -r "${pools[@]}")
|
||||
|
||||
if ! $quiet; then
|
||||
echo "processed $count snapshots ($(human-size "$total"))"
|
||||
# finish if running with `-l`
|
||||
if $listonly; then
|
||||
debug "running in '-l' mode - exiting here"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
humantotal=$(human-size "$totalused")
|
||||
|
||||
echo "found $numsnapshots snapshots ($humantotal) on pools: $humanpools"
|
||||
|
||||
# process snapshots found
|
||||
i=0
|
||||
for line in "${lines[@]}"; do
|
||||
read -r creation used snapshot _ <<< "$line"
|
||||
|
||||
((i++))
|
||||
|
||||
delta=$((now - creation))
|
||||
ht=$(human-time "$delta")
|
||||
hu=$(human-size "$used")
|
||||
|
||||
if $dryrun; then
|
||||
echo -n '[dry-run] '
|
||||
fi
|
||||
|
||||
echo "[$i/$numsnapshots] removing $snapshot: $ht old ($hu)"
|
||||
|
||||
if ! $dryrun; then
|
||||
zfs destroy "${destroyargs[@]}" "$snapshot" || code=1
|
||||
fi
|
||||
done
|
||||
|
||||
echo "removed $numsnapshots snapshots ($humantotal)"
|
||||
|
||||
exit "$code"
|
||||
|
|
Loading…
Reference in New Issue