-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathequine.bash
268 lines (221 loc) · 10.9 KB
/
equine.bash
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
#!/bin/bash
# ___ ___ ___ ___
# /\__\ /\ \ /\ \ /\__\
# /:/ / \:\ \ \:\ \ /::| |
# /:/__/ \:\ \ \:\ \ /:|:| |
# /::\ \ ___ /::\ \ /::\ \ /:/|:|__|__
# /:/\:\ /\__\ /:/\:\__\ /:/\:\__\ /:/ |::::\__\
# \/__\:\/:/ / /:/ \/__/ /:/ \/__/ \/__/~~/:/ /
# \::/ / /:/ / /:/ / /:/ /
# /:/ / \/__/ \/__/ /:/ /
# /:/ / /:/ /
# \/__/ \/__/
#
# Copyright (c) 2023, Robert Swinford <robert.swinford<...at...>gmail.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.
set -euf -o pipefail
#set -x
function print_version {
printf "\
equine $(httm --version | cut -f2 -d' ')
" 1>&2
exit 0
}
function print_usage {
local equine="\e[31mequine\e[0m"
local httm="\e[31mhttm\e[0m"
printf "\
$equine is a script to mount APFS snapshots for use with $httm.
$equine has two modes: 1) mount and unmount of local APFS snapshots,
and 2) mount and unmount of remote APFS snapshots, located on a Time
Machine network volume (NAS).
This script is *not* for use with Time Machines which utilize direct
attached storage (DAS).
USAGE:
equine [OPTIONS]
OPTIONS:
--mount-local:
Attempt to mount your local Time Machine snapshots.
--unmount-local:
Attempt to unmount your local Time Machine snapshots.
--mount-remote:
Attempt to mount your remote Time Machine snapshots and the Time
Machine image file, and connect the server.
--unmount-remote:
Attempt to unmount your remote Time Machine snapshots and the Time
Machine image file, and disconnect the server.
--help:
Display this dialog.
--version:
Display script version.
" 1>&2
exit 1
}
print_err_exit() {
print_err "$@"
exit 1
}
print_err() {
printf "%s\n" "Error: $*" 1>&2
}
function prep_exec {
[[ -n "$(
command -v plutil
exit 0
)" ]] || print_err_exit "'plutil' is required to execute 'equine'. Please check that 'plutil' is in your path."
[[ -n "$(
command -v tmutil
exit 0
)" ]] || print_err_exit "'tmutil' is required to execute 'equine'. Please check that 'tmutil' is in your path."
[[ -n "$(
command -v mount_apfs
exit 0
)" ]] || print_err_exit "'mount_apfs' is required to execute 'equine'. Please check that 'mount_apfs' is in your path."
[[ -n "$(
command -v mount_smbfs
exit 0
)" ]] || print_err_exit "'mount_smbfs' is required to execute 'equine'. Please check that 'mount_smbfs' is in your path."
[[ -n "$(
command -v mount
exit 0
)" ]] || print_err_exit "'mount' is required to execute 'equine'. Please check that 'mount' is in your path."
[[ -n "$(
command -v cut
exit 0
)" ]] || print_err_exit "'cut' is required to execute 'equine'. Please check that 'cut' is in your path."
[[ -n "$(
command -v grep
exit 0
)" ]] || print_err_exit "'grep' is required to execute 'equine'. Please check that 'grep' is in your path."
[[ -n "$(
command -v xargs
exit 0
)" ]] || print_err_exit "'xargs' is required to execute 'equine'. Please check that 'xargs' is in your path."
[[ -n "$(
command -v hdiutil
exit 0
)" ]] || print_err_exit "'hdiutil' is required to execute 'equine'. Please check that 'hdiutil' is in your path."
}
function mount_remote() {
local server="$( plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep "NetworkURL" | cut -d '"' -f4 )"
local mount_source="$( plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep "LastKnownVolumeName" | cut -d '"' -f4 )"
[[ -n "$server" ]] || print_err_exit "Could not determine server address, perhaps none is specified?"
[[ -n "$mount_source" ]] || print_err_exit "Could not determine mount source from server name"
local sys_defined_dir="$( find /Volumes/.timemachine -name "$mount_source" -print -quit )"
local dirname="/Volumes/$mount_source"
[[ -z "$sys_defined_dir" ]] || dirname="$sys_defined_dir"
if [[ "$( mount | grep -c "$dirname" )" -eq 0 ]]; then
printf "%s\n" "Connecting to remote Time Machine: $server ..."
[[ -d "$dirname" ]] || mkdir -p "$dirname"
mount_smbfs -o nobrowse "$server" "$dirname" 2>/dev/null || true
else
printf "%s\n" "Skip connecting to remote server, as Time Machine already mounted ..."
fi
# Wait for server to connect
timeout 30s bash -c "until [[ "$( mount | grep -c "$dirname" )" -gt 0 ]]; do sleep 1; done" || \
print_err_exit "Wait for server to be mounted timed out. Quitting."
local image_name="$(plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep LocalizedDiskImageVolumeName | cut -d '"' -f4)"
[[ -n "$image_name" ]] || print_err_exit "Could not determine Time Machine disk image name, perhaps none is specified?"
if [[ "$( mount | grep -c "$image_name" )" -eq 0 ]]; then
printf "%s\n" "Mounting sparse bundle (this may include an fsck): $image_name ..."
local bundle_name="$( find "$dirname" -type d -iname "*.sparsebundle" -print -quit )"
[[ -n "$bundle_name" ]] || print_err_exit "Could not find sparsebundle in location specified"
hdiutil attach -readonly -nobrowse "$bundle_name" || print_err_exit "Attaching disk image failed. Quitting."
else
printf "%s\n" "Skip mounting sparse bundle, as $image_name appears to already be mounted ..."
fi
printf "%s\n" "Discovering backup locations (this can take a few seconds)..."
local backups="$( tmutil listbackups / )"
local device="$( mount | grep "$image_name" | cut -d' ' -f1 | tail -1 )"
local uuid="$( echo "$backups" | cut -d "/" -f4 | head -1 )"
[[ -n "$device" ]] || print_err_exit "Could not determine Time Machine device from image give"
[[ -n "$uuid" ]] || print_err_exit "Could not determine uuid from list of backup locations"
[[ "$( mount | grep -c "$image_name" )" -gt 0 ]] || print_err_exit "Time machine disk image did not mount"
[[ -d "/Volumes/.timemachine/$uuid" ]] || mkdir -p "/Volumes/.timemachine/$uuid"
printf "%s\n" "Mounting snapshots..."
for snap in $( echo "$backups" | xargs basename ); do
[[ -d "/Volumes/.timemachine/$uuid/$snap" ]] || mkdir -p "/Volumes/.timemachine/$uuid/$snap"
printf "%s\n" "Mounting snapshot "com.apple.TimeMachine.$snap" from "$device" at "/Volumes/.timemachine/$uuid/$snap""
[[ -d "/Volumes/.timemachine/$uuid/$snap" ]] && mount_apfs -o ro,nobrowse,noowners,noatime,nodev,nosuid -s "com.apple.TimeMachine.$snap" "$device" "/Volumes/.timemachine/$uuid/$snap" 2>/dev/null || true
done
}
function unmount_remote() {
printf "%s\n" "Unmounting any mounted snapshots...."
mount | grep "com.apple.TimeMachine.*.backup@" | cut -d' ' -f1 | xargs -I{} umount "{}" 2>/dev/null || true
local image_name="$(plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep LocalizedDiskImageVolumeName | cut -d '"' -f4)"
[[ -n "$image_name" ]] || print_err "Could not determine Time Machine disk image name, perhaps none is specified?"
local sub_device="$( mount | grep "$image_name" | cut -d' ' -f1 | tail -1 )"
local device="$( echo $sub_device | cut -d's' -f1 )"
[[ -n "$sub_device" ]] || print_err "Could not determine subdevice from disk image given"
[[ -n "$device" ]] || print_err "Could not determine device from disk image given"
local server="$( plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep "NetworkURL" | cut -d '"' -f4 )"
local mount_source="$( plutil -p /Library/Preferences/com.apple.TimeMachine.plist | grep "LastKnownVolumeName" | cut -d '"' -f4 )"
local sys_defined_dir="$( find /Volumes/.timemachine -name "$mount_source" -print -quit )"
local dirname="/Volumes/$mount_source"
[[ -z "$sys_defined_dir" ]] || dirname="$sys_defined_dir"
[[ "$(tmutil status | grep -c "Running = 0")" -gt 0 ]] || \
print_err_exit "Backup running. 'equine' will not unmount sparsebundle or server while backup is running. Quitting."
printf "%s\n" "Attempting to unmount Time Machine sparse bundle: $image_name ..."
[[ -z "$sub_device" ]] || diskutil unmount "$sub_device" 2>/dev/null || true
[[ -z "$device" ]] || diskutil unmountDisk "$device" 2>/dev/null || true
[[ -n "$server" ]] || print_err "Could not determine server, perhaps none is specified?"
[[ -n "$mount_source" ]] || print_err "Could not determine mount source from server name given"
printf "%s\n" "Attempting to unmount/disconnect from Time Machine server: $server ..."
[[ -z "$mount_source" ]] || diskutil unmount force "$dirname" 2>/dev/null || true
[[ -z "$mount_source" ]] || diskutil unmountDisk force "$dirname" 2>/dev/null || true
}
function mount_local() {
printf "%s\n" "Discovering backup locations (this can take a few seconds)..."
local backups="$( tmutil listlocalsnapshots /System/Volumes/Data | grep -v ':' )"
local mounts="$( mount )"
local device="$( echo "$mounts" | grep "/System/Volumes/Data " | cut -d' ' -f1 )"
local hostname="$( hostname )"
[[ -n "$device" ]] || print_err_exit "Could not determine Time Machine device from image give"
printf "%s\n" "Mounting snapshots..."
for snap in $( echo "$backups" ); do
if [[ $( echo "$mounts" | grep -c "$snap" ) -gt 0 ]]; then
continue
fi
local snap_uuid="$( echo $snap | cut -d'.' -f4 )"
[[ -d "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/$hostname/$snap_uuid/Data" ]] || \
mkdir -p "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/$hostname/$snap_uuid/Data"
printf "%s\n" "Mounting snapshot "$snap" from "$device" at "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/$hostname/$snap_uuid/Data""
[[ -d "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/$hostname/$snap_uuid/Data" ]] && \
mount_apfs -o ro,nobrowse,noowners,noatime,nodev,nosuid -s "$snap" "$device" "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/$hostname/$snap_uuid/Data" 2>/dev/null || true
done
}
function unmount_local() {
[[ "$(tmutil status | grep -c "Running = 0")" -gt 0 ]] || \
print_err_exit "Backup running. 'equine' will not unmount local snapshots while backup is running. Quitting."
printf "%s\n" "Unmounting any mounted snapshots...."
mount | grep "com.apple.TimeMachine.*.local@" | cut -d' ' -f1 | xargs -I{} umount "{}" 2>/dev/null || true
}
function exec_equine() {
[[ $# -ge 1 ]] || print_usage
[[ "$1" != "-h" && "$1" != "--help" ]] || print_usage
[[ "$1" != "-V" && "$1" != "--version" ]] || print_version
[[ "$( uname )" == "Darwin" ]] || print_err_exit "This script requires you run it on MacOS"
[[ "$EUID" -eq 0 ]] || print_err_exit "This script requires you run it as root"
prep_exec
while [[ $# -ge 1 ]]; do
if [[ "$1" == "--mount-remote" ]]; then
mount_remote
break
elif [[ "$1" == "--unmount-remote" ]]; then
unmount_remote
break
elif [[ "$1" == "--mount-local" ]]; then
mount_local
break
elif [[ "$1" == "--unmount-local" ]]; then
unmount_local
break
else
print_err_exit "User must specify whether to mount or unmount the Time Machine volumes."
break
fi
done
}
exec_equine "$@"