-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmslim.sh
74 lines (56 loc) · 1.64 KB
/
tmslim.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
73
74
#!/bin/bash
# Give your TimeMachine Backup a Diet by culling older backups
# Coded by Jack-Daniyel Strong, J-D Strong Consulting, Inc. & Strong Solutions
# Written 2015.07.28, Last Modified 2015.07.28 by Jack-Daniyel Strong
# Define variables for path to executables
TMUTIL="/usr/bin/tmutil"
# Define variables
TARGETSIZE=$1 #in GB
### TOUCH NOTHING BELOW THIS LINE ###
TARGETSIZE=$((TARGETSIZE*1000))
echo "We will remove backups greater than $1 GB, or $TARGETSIZE MB."
declare -a BACKUPS
if [ $(whoami) != 'root' ]; then
echo "Must be root to run $0"
exit 1;
fi
if [ -z $1 ]; then
echo "Usage: $0 <target size in Gigabytes>"
exit 1
fi
# Build List to Loop Through
IFS=$'\n' # make newlines the only separator
let count=0
for backup in $( $TMUTIL "listbackups" ); do
BACKUPS[$count]="$backup"
((count++))
# echo "$count: $backup"
done
echo " Loop Count: $count /n"
let snapshot=$count
let snapshotsum=0
for ((i=${#BACKUPS[*]-1}; i>=0; i--));
do
busize=`$TMUTIL uniquesize ${BACKUPS[i]} | awk '{print $1}'`
busizemb=`echo $busize |
awk '{
ex = index("MB-G", substr($1, length($1)))
val = substr($1, 0, length($1)-1)
prod = val * 10^((ex * 1) - 1)
sum += prod
}
END {print sum}'`
let busizemb=${busizemb/\.*} #remove floating point
let newsnapshotsum=$snapshotsum+$busizemb
if (( $newsnapshotsum >= $TARGETSIZE )); then
#cull this backup
echo Removing ${BACKUPS[i]} $snapshotsum
$TMUTIL delete ${BACKUPS[i]}
else
let "snapshotsum+=$busizemb"
echo $i: ${BACKUPS[i]} $snapshotsum
fi
done
# Detect SparseBundle and Compress
exit 0
# done ! :)