-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmacbac.sh
executable file
·371 lines (295 loc) · 7.82 KB
/
macbac.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env bash
PACKAGE_NAME="com.hazcod.macbac"
PLIST_PATH="$HOME/Library/LaunchAgents/${PACKAGE_NAME}.plist"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
BOLD='\033[1m'
#
#--------------------------------------------------------------------------------------------------------------------------------------
#
usage() {
# show command cli usage help
echo "Usage: $0 <status|list|snapshot|enable|disable|schedule|deschedule|prune|next> <...>"
exit 1
}
error() {
# show an error message and exit
>&2 echo -e "${RED}ERROR:${N} ${1}${NC}"
exit 1
}
realpath() {
OURPWD=$PWD
cd "$(dirname "$1")"
LINK=$(readlink "$(basename "$1")")
while [ "$LINK" ]; do
cd "$(dirname "$LINK")"
LINK=$(readlink "$(basename "$1")")
done
REALPATH="$PWD/$(basename "$1")"
cd "$OURPWD"
echo "$REALPATH"
}
deschedule() {
if [ ! -f "${PLIST_PATH}" ]; then
error "a schedule was not configured"
exit 1
fi
launchctl stop "${PACKAGE_NAME}" 2>/dev/null
if ! launchctl unload "$PLIST_PATH" || ! rm "${PLIST_PATH}"; then
error "could not disable the schedule"
exit 1
fi
echo -e "Removed previous snapshot schedule."
}
showNext() {
parts=$(grep StartCalendarInterval "$PLIST_PATH" -C2 | tail -n1)
mode=$(echo "$parts" | cut -d '>' -f 2 | cut -d '<' -f 1)
number=$(echo "$parts" | cut -d '>' -f 4 | cut -d '<' -f 1)
if [[ "$mode" == "" ]]; then
error "No schedule detected. Did you run 'schedule'?"
exit 1
fi
local now
local unit
if [[ "$mode" == "Minute" ]]; then
now=$(date +'%M')
unit="minutes"
fi
if [[ "$mode" == "Hour" ]]; then
now=$(date +'%H')
unit="hours"
fi
diff="$((number - now))"
if (( diff < 0 )); then
diff=$((diff+60))
fi
echo "Your next snapshot is scheduled in the next ${diff} ${unit}."
return
}
schedule() {
local mode="$1"
local keep="$2"
interval=""
if [ "$mode" == "hourly" ]; then
interval="<key>Minute</key><integer>$(($(date +%M) +1))</integer>"
if [ -z "$keep" ]; then
keep="24"
fi
elif [ "$mode" == "daily" ]; then
interval="<key>Hour</key><integer>$(($(date +%H) -1))</integer>"
if [ -z "$keep" ]; then
keep="7"
fi
else
error "invalid schedule mode: ${mode}"
exit 1
fi
if [ -f "$PLIST_PATH" ]; then
echo "Removing previous schedule..."
deschedule
fi
mkdir -p "$HOME/Library/LaunchAgents/"
echo "Installing daemon config to ${PLIST_PATH}"
cat >"$PLIST_PATH" <<EOL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${PACKAGE_NAME}</string>
<key>Nice</key>
<integer>20</integer>
<key>StandardOutPath</key>
<string>/tmp/macbac.log</string>
<key>StandardErrorPath</key>
<string>/tmp/macbac.err</string>
<key>ProgramArguments</key>
<array>
<string>$(realpath $0)</string>
<string>snapshot</string>
<string>${keep}</string>
</array>
<key>StartCalendarInterval</key>
<dict>
${interval}
</dict>
</dict>
</plist>
EOL
echo "Loading config to enable schedule..."
if ! launchctl load "$PLIST_PATH" || ! launchctl start "${PACKAGE_NAME}"; then
error "Could not enable the schedule"
exit 1
fi
echo -e "${GREEN}Scheduled ${mode} snapshots!${NC}"
}
prune() {
local volume="$1"
local amount="$2"
if [[ ${amount} -le 0 ]]; then
exit 0
fi
IFS=$'\n' read -r -d '' -a snapshots < <(tmutil listlocalsnapshotdates "$volume" | tail -n +2 | sort)
if [[ ${#snapshots[@]} -eq 0 ]]; then
echo "No snapshots to purge"
exit 0
fi
if [[ ${amount} -gt ${#snapshots[@]} ]]; then
amount=0
else
amount=$((${#snapshots[@]} - amount))
fi
echo "Pruning ${amount} of ${#snapshots[@]} snapshots for ${volume}"
counter="$amount"
for snapshot in "${snapshots[@]}"; do
if [[ $counter -le 0 ]]; then
break
fi
echo "Pruning snapshot ${snapshot} (${counter}/${amount})"
if ! tmutil deletelocalsnapshots "${snapshot}" >/dev/null; then
error "could not prune local snapshot ${snapshot}"
exit 1
fi
counter=$((counter -1))
done
}
snapshot() {
local volume="$1"
local pruneAmount="$2"
if [ -z "$volume" ]; then
echo "Assuming / is the volume we would like to snapshot."
volume="/"
fi
if ! tmutil localsnapshot "${volume}" | grep -v '^NOTE:'; then
error "Could not snapshot ${volume}"
exit 1
fi
echo -e "${GREEN}Snapshotted volume ${volume}${NC}"
if [ -n "$pruneAmount" ] && (( pruneAmount > 0 )); then
prune "$volume" "$pruneAmount"
fi
}
enable() {
if ! sudo tmutil enable; then
error "Could not enable backups"
exit 1
fi
echo -e "${GREEN}Enabled backups.${NC}"
}
disable() {
if ! sudo tmutil disable; then
error "Could not enable backups"
exit 1
fi
echo -e "${YELLOW}Disabled backups.${NC}"
}
getVolumes() {
ls -1d /Volumes/*
}
getSnapshots() {
local volume="$1"
tmutil listlocalsnapshotdates "$volume" | tail -n +2 | xargs
#listlocalsnapshots / | tail -n +2 | xargs | sed 's/com\.apple\.TimeMachine\.//g' | sed 's/\.local//'
}
listStatus() {
status="$(tmutil currentphase)"
if [ "$status" == "BackupNotRunning" ]; then
echo -e "${BOLD}Backup Status${NC}: Inactive"
return
fi
if [ "$status" == "BackupError" ]; then
echo -e "${BOLD}Backup Status${NC}: ${RED}Error during backup${NC}"
return
fi
echo -e "${BOLD}Backup Status${NC}: ${GREEN}Running${NC}"
}
listSnapshots() {
volumes=($(getVolumes))
for volume in "${volumes[@]}"; do
# show current volume
echo -e "${BOLD}${volume}${NC}"
local snapshots
# retrieve snapshots
if ! snapshots="$(getSnapshots "$volume")"; then
error "could not retrieve snapshots"
exit 1
fi
# list all snapshots
for snapshot in $snapshots; do
IFS='-' read -ra parts <<< "$snapshot"
dateStr="${parts[3]:0:2}:${parts[3]:2:2}:${parts[3]:4:2}"
echo "> ${parts[0]}/${parts[1]}/${parts[2]} $dateStr"
done
# add extra space when we have multiple rows
if (( ${#volumes[@]} > 1 )); then
echo ""
fi
done
}
#
#--------------------------------------------------------------------------------------------------------------------------------------
#
if [ $# -lt 1 ]; then
usage
fi
case "$1" in
"status")
if [[ "$#" -gt 1 ]]; then
usage
fi
listStatus
;;
"list")
if [[ "$#" -gt 1 ]]; then
usage
fi
listSnapshots
;;
"snapshot")
if [[ "$#" -gt 2 ]]; then
usage
fi
snapshot "/" "$2"
;;
"enable")
if [[ "$#" -gt 1 ]]; then
usage
fi
enable
;;
"disable")
if [[ "$#" -gt 1 ]]; then
usage
fi
disable
;;
"schedule")
if [[ "$#" -gt 3 ]]; then
usage
fi
schedule "$2" "$3"
;;
"deschedule")
if [[ "$#" -gt 1 ]]; then
usage
fi
deschedule
;;
"prune")
if [[ "$#" -gt 2 ]]; then
usage
fi
prune "/" "$2"
;;
"next")
if [[ "$#" -gt 1 ]]; then
usage
fi
showNext
;;
*)
usage
;;
esac