-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapspace
executable file
·90 lines (86 loc) · 2.37 KB
/
snapspace
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
function format_data ()
{
local amount="$1"
local suffixes=(" " "K" "M" "G" "T" "P" "E" "Z" "Y")
local testnum="$amount"
local divisor="1"
local suffix=""
for ((index = 0; index < ${#suffixes[@]}; ++index))
do
suffix="${suffixes[$index]}"
testnum=`echo "$amount/($divisor)" | bc`
if ((testnum < 1000))
then
break
fi
divisor="$divisor*1024"
done
local prespace=" "
if ((testnum >= 100))
then
prespace=""
else
if ((testnum >= 10))
then
prespace=" "
fi
fi
if [[ "$divisor" == "1" ]]
then
#don't use bc for non-fractional cases
local displaydata="$amount "
else
local displaydata=`echo "scale = 2; $amount/($divisor)" | bc -l`
#we switch at 1000, not 1024, so check for having no digit before decimal point
if [[ "$displaydata" == .* ]]
then
#because leading zero behavor is unspecified in bc
displaydata=0"$displaydata"
fi
fi
echo "$prespace$displaydata$suffix"
}
if (($# < 1))
then
echo "usage: $0 <filesystem>"
exit 1
fi
if [[ "$1" == *@* || "$1" == /* ]]
then
echo "Snapshots and paths are not supported"
echo "usage: $0 <filesystem>"
exit 1
fi
#check that the filesystem is valid
if ! zfs get referenced "$1" > /dev/null
then
#zfs will have printed an error message, so just exit
exit 1
fi
echo " OLDREFS UNIQUE UNIQUE% SNAPSHOT"
fullref=`zfs get -Hp referenced "$1" | awk '{print $3}'`
for snap in `zfs list -Hd 1 -t snapshot -o name "$1" | cut -f2- -d@`
do
snapref=`zfs get -Hp referenced "$1"@"$snap" | awk '{print $3}'`
snapwritten=`zfs get -Hp written@"$snap" "$1" | awk '{print $3}'`
olddata=$((snapref + snapwritten - fullref))
snapusedraw=`zfs get -Hp used "$1"@"$snap" | awk '{print $3}'`
chars=`echo "$snap" | wc -m | awk '{print $1}'`
if ((olddata > 0))
then
displaypercent=`echo "100*$snapusedraw/$olddata" | bc`
else
displaypercent=0
fi
if ((displaypercent < 100))
then
if ((displaypercent >= 10))
then
displaypercent=" $displaypercent"
else
displaypercent=" $displaypercent"
fi
fi
echo "$(format_data "$olddata") $(format_data "$snapusedraw") $displaypercent% $snap"
done