-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfs-destroy-snapshots.sh
72 lines (61 loc) · 1.96 KB
/
zfs-destroy-snapshots.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#! /bin/bash
# Define variables.
export PARENT_PID=$$
DATASET=$1
ZPOOL=$(printf "$DATASET" | awk -F'/' '{print $1}')
# Pretty styling
RED="\u001b[31;1m"
WHITE="\u001b[37m"
GREY="\u001b[30;1m"
UNDERLINE="\033[4m"
CLEAR="\033[0m"
# Temporarily set zpool property `listsnapshots=on`
LISTSNAPSHOTS_PREV=$(zpool get -o value -Hp listsnapshots amalgm)
zpool set listsnapshots=on "$ZPOOL"
echo $LISTSNAPSHOTS
# On error, warn user then exit parent process.
function paramsError {
printf "$RED"
printf 'ERROR: $1 is unset or empty\n'
printf "$WHITE"
printf 'Example: `zfs-destroy-snapshots.sh tank/Downloads`\n'
printf "$CLEAR"
kill -s TERM $PARENT_PID
}
# On call, cleanly exit the script.
function cleanExit {
# Return zpool property `listsnapshots` to original setting
zpool set listsnapshots="$LISTSNAPSHOTS_PREV" "$ZPOOL"
printf "$CLEAR"
kill -s TERM $PARENT_PID
exit # Just in case
}
# Ensure $1 and $2 are set; otherwise exit.
test -n "$1" || paramsError
test "$1" != " " || paramsError
# Ensure both the pool and dataset exist.
zfs list -H -o name -t snapshot "$DATASET" >> /dev/null || cleanExit
# Warn the user before they do something stupid.
#####clear
printf " ##################################### $RED WARNING $WHITE ################################### \n"
printf "$UNDERLINE The following snapshots for dataset $RED$DATASET$WHITE in zpool $RED$ZPOOL$WHITE will be destroyed: $CLEAR \n"
zfs list -o name,used -t snapshot "$DATASET"
echo -n " -- TOTAL "
zfs list -H -o usedbysnapshots "$DATASET"
# Prompt the user; are you SURE you want to risk doing something stupid?
printf "$RED"
read -p "Destroy all of the above snapshots? (y) " -n 1 -r
printf "$RED"
printf '\n'
# If input is 'y' or 'Y', proceed with destroying listed snapshots.
if [[ $REPLY =~ ^[Yy]$ ]]
then
printf "$GREY"
clear
zfs list -H -o name -t snapshot "$DATASET" | xargs -t -P4 --replace=@ zfs destroy '@'
cleanExit
# Otherwise, abandon ship!
else
printf "Okay, nevermind then. Bye!\n"
cleanExit
fi