-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmacos-state
executable file
·583 lines (484 loc) · 17.1 KB
/
macos-state
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env bash
# we can assume [HOMEBREW_PREFIX] have already been provided
function macos_state() (
source "$DOROTHY/sources/bash.bash"
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Applies your theme preference to macOS, VSCode, and Wallpaper.
USAGE:
macos-state <backup|restore> [...options]
OPTIONS:
--local=<local>
What is your local volume?
--backups=<backups>
Where backups are stored / will be stored.
EXAMPLE:
# To restore configuration from a remote machine:
macos-state restore --root='username@hostname:/Volumes/System'
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}
# process
local item action='' local_root='' backup_root=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--local='*) local_root="${item#*=}" ;;
'--backups='*) backup_root="${item#*=}" ;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*)
if [[ -z $action ]]; then
action="$item"
else
help "An unrecognised argument was provided: $item"
fi
;;
esac
done
# assert compatibility
if ! is-mac; then
help 'This command is only purposeful on macOS, which you are not running.'
fi
# ensure
action="$(
choose --linger --question='What do you want to do?' \
--label --default="$action" -- \
backup 'Make a backup?' \
restore 'Restore a backup?'
)"
# What is the local volume?
local_root="$(
choose --linger --required \
--question='What is your local volume?' \
--default="$local_root" -- /Volumes/*
)"
# =====================================
# Action
# Use time machine?
local backup_type='fs' # tm, fs, remote
local time_backup_volume='' time_backup_machine=''
if [[ $action == 'restore' && -z $backup_root ]]; then
__print_line
if
confirm --linger --bool --ppid=$$ -- 'Do you want to restore from Time Machine?' \
"Ensure either:" \
"- Time Machine is configured in system preferences" \
"- The Time Machine Backup Volume is already mounted"
then
backup_root="$(tmutil latestbackup || :)"
if [[ ! -d $backup_root ]]; then
__print_lines '' 'Unable to find the Time Machine backup automatically, attempting manual resolution...'
time_backup_volume="$(
choose --linger --required \
--question='Which volume contains the time machine backups?' \
-- /Volumes/*
)"
time_backup_machine="$(
choose-path --required \
--question='Which backup entry to use?' \
-- "$time_backup_volume/Backups.backupdb/"*
)"
backup_root="$time_backup_machine/Latest"
fi
if [[ -d $backup_root ]]; then
__print_lines "Time Machine Backup: $backup_root"
backup_type='tm'
else
__print_lines 'Time Machine backup could not be determined' >/dev/stderr
backup_root=''
fi
fi
fi
# Use cloud location if it exists
local cloud_root=''
if [[ $action == 'restore' && -z $backup_root ]]; then
cloud_root="$HOME/Library/Mobile Documents/com~apple~CloudDocs/Apps/macos-state"
if [[ -d $cloud_root ]]; then
backup_root="$(fs-join -- "$cloud_root" "$local_root")"
fi
fi
# Confirm the backup location
__print_line
local question=''
cloud_root="$HOME/Library/Mobile Documents/com~apple~CloudDocs/Apps/macos-state"
if [[ $action == 'backup' ]]; then
question="Where will the backups be stored?"
else
question="Where are the backups stored?"
fi
backup_root="$(
ask --required --confirm \
--question="$question" \
--default="${backup_root:-"$cloud_root"}"
)"
# Detection
if [[ "Time Machine Backups" == *"$backup_root"* || "Backups.backupdb" == *"$backup_root"* ]]; then
# tm
backup_type='tm'
elif [[ $backup_root == *"@"* ]]; then
# remote
if [[ $backup_root != *":"* ]]; then
backup_root="$backup_root:"
fi
backup_type='remote'
else
# fs
backup_type='fs'
fi
# Ensure volume
local potential_root=''
if [[ $backup_root != *"Volumes"* ]]; then
# if it is backup, use our current location
if [[ $action == 'backup' ]]; then
backup_root="$(fs-join -- "$backup_root" "$local_root")"
else
# if it is restore, then ask the user, if supported
if [[ $backup_type == 'remote' ]]; then
# fail on remote
potential_root="$(fs-join -- "$backup_root" "$local_root")"
echo-style \
--error='Remote backup locations must manually specify the volume.' --newline \
--notice='Try again with something like:' --newline \
--code="$(echo-escape-command -- 'macos-state' "$potential_root")" >/dev/stderr
return 1
else
# ask
backup_root="$(
choose --linger --required \
--question='Which backup volume to use?' \
-- "$(fs-join -- "$backup_root" 'Volumes')/"*
)"
fi
fi
fi
# dump variables
echo-style --dim \
"Action: $action" $'\n' \
"Local Volume: $local_root" $'\n' \
"Cloud Root: $cloud_root" $'\n' \
"Backup Root: $backup_root" $'\n' \
"Backup Type: $backup_type"
# Helpers
function fs_volume_join {
if [[ $2 == '/'* ]]; then
fs-join -- "$1" "$2"
else
fs-join -- "$1" "$HOME/$2"
fi
}
function do_remove {
local path="$1"
if is-present -- "$path"; then
if confirm --linger --positive --ppid=$$ -- '^ will be replaced, continue?'; then
fs-rm --quiet --no-confirm --sudo -- "$path"
fi
fi
}
function do_mkdir {
local path="$1"
__mkdirp "$(dirname -- "$path")"
}
function do_replace {
local path="$path"
do_remove "$path"
do_mkdir "$path"
}
function do_backup {
local from to path="$1"
from="$(fs_volume_join "$local_root" "$path")"
to="$(fs_volume_join "$backup_root" "$path")"
cat <<-EOF
BACKUP:
$from
=>
$to
EOF
if is-present -- "$from"; then
if [[ $backup_type == 'remote' ]]; then
cpr -- "$from" "$to"
else
do_replace "$to"
cpr -- "$from" "$to"
fi
else
echo-style --notice='^ MISSING'
warnings+=("MISSING: $from")
fs-rm --quiet --no-confirm --sudo -- "$path"
fi
}
function do_restore {
local from to path="$1"
from="$(fs_volume_join "$backup_root" "$path")"
to="$(fs_volume_join "$local_root" "$path")"
cat <<-EOF
$(echo-style --bold='RESTORE:')
$from
=>
$to
EOF
if [[ $backup_type == 'tm' ]]; then
do_replace "$to"
tmutil restore -v "$from" "$to"
elif [[ $backup_type == 'remote' ]]; then
do_replace "$to"
cpr -- "$from" "$to"
elif is-present -- "$from"; then
do_replace "$to"
cpr -- "$from" "$to"
else
echo-style --notice='^ MISSING'
warnings+=("MISSING: $from")
fs-rm --quiet --no-confirm --sudo -- "$path"
fi
}
function process {
local title="$1" paths=("${@:1}") path=''
echo-style --h1 "$title"
if confirm --linger --bool --ppid=$$ -- "Do you want to $action $title"; then
for path in "${paths[@]}"; do
if [[ $action == 'backup' ]]; then
do_backup "$path"
elif [[ $action == 'restore' ]]; then
do_restore "$path"
fi
done
else
echo-style --dim="Skipped..."
fi
echo-style --g1 "$title"
}
# ===============
# Current
process "Choosy" \
"Library/Application Support/Choosy/" \
"Library/Preferences/com.choosyosx.Choosy.plist"
process "Elmedia" \
"Library/Preferences/com.eltima.elmedia-setapp.plist"
process "Google Chrome" \
"Library/Application Support/Google/Chrome/"
process "OBS" \
"Library/Application Support/obs-studio/" \
"Library/Preferences/com.obsproject.obs-studio.plist"
process "Plex" \
"Library/Application Support/Plex Media Server/" \
"Library/Preferences/com.plexapp.plexmediaserver.plist"
process "Twitch Studio" \
"Library/Application Support/Twitch Studio/"
# ===============
# Apple
process "Apple Mail" "Library/Containers/com.apple.mail/Data/Library/Preferences/"
process "Apple Safari History" \
"Library/Safari/History.db" \
"Library/Safari/History.db-lock" \
"Library/Safari/History.db-shm" \
"Library/Safari/History.db-wal" \
"Library/Safari/HistoryIndex.sk"
process "Apple Safari Preferences" "Library/Preferences/com.apple.Safari.plist"
process "Apple Saved Searches" "Library/Saved Searches/"
process "Apple Stickies" "Library/StickiesDatabase/"
process "Apple Terminal" "Library/Preferences/com.apple.Terminal.plist"
# ===============
# Documents
process "Downloads" "Downloads/"
process "Music" "Music/"
process "Projects" "Projects/"
process "Virtual Machines" "Virtual Machines/"
process "$DOROTHY" "$DOROTHY/"
process "GPG" ".gnupg/"
process "SSH" ".ssh/"
process "NPM" ".npmrc"
process "Bash History" ".bash_history"
process "Travis" ".travis/config.yml"
# ===============
# Apps
# todo: acorn, adguard, authy, agenda, backblaze, bear, bee, bitcoin.com, gitter, jaikoz, keybase, kindle, ntfs for mac, numi, pastebot, paw, pinstriped, protonvpn, scrumpy, signal, sketch, sunama, things, thoughttrain, transmit, ubersicht, webtorrent, workflowy
process "Acorn" \
"Library/Application Support/Acorn/" \
"Library/Preferences/com.flyingmeat.Acorn6.plist"
process "Adguard" \
"Library/Application Support/com.adguard.Adguard/" \
"Library/Preferences/com.adguard.Adguard.plist"
process "Agenda" "Library/Containers/com.momenta.agenda.macos/"
process "Amphetamine" "Library/Containers/com.if.Amphetamine/"
process "AppZapper" "Library/Preferences/com.appzapper.AppZapper.plist"
process "Audio Hijack" \
"Library/Application Support/Audio Hijack/" \
"Library/Preferences/com.rogueamoeba.audiohijack3.plist"
process "Authy" "Library/Application Support/Authy Desktop/"
process "Base" \
"Library/Application Support/Base/" \
"Library/Preferences/uk.co.menial.Base.plist"
process "Bartender" "Library/Preferences/com.surteesstudios.Bartender.plist"
process "Bear" "Library/Containers/net.shinyfrog.bear/"
process "Bitcoin.com" "Library/Application Support/bitcoin.com/"
process "Blockstack" \
"Library/Application Support/Blockstack/" \
"Library/Preferences/org.blockstack.mac.plist"
process "Boxy" "Library/Containers/com.francescodilorenzo.Mailbro/"
process "Brave" "Library/Application Support/brave/"
process "Calibre" \
"Library/Preferences/calibre/" \
"Library/Preferences/net.kovidgoyal.calibre.plist"
process "Context" "Library/Preferences/com.contextsformac.Contexts.plist"
process "Cyberduck" \
"Library/Containers/ch.sudo.cyberduck/" \
"Library/Preferences/ch.sudo.cyberduck.plist"
process "DevDocs" "Library/Application Support/DevDocs/"
process "DiskMap" "Library/Preferences/com.fiplab.diskmap.plist"
process "DNSCrypt" "$HOMEBREW_PREFIX/etc/dnscrypt-proxy.toml"
process "Donut" \
"Library/Application Support/Donut/" \
"Library/Preferences/com.harshjv.donut.helper.plist" \
"Library/Preferences/com.harshjv.donut.plist"
process "Exodus" "Library/Application Support/Exodus/"
process "Firefox" \
"Library/Application Support/Firefox/" \
"Library/Preferences/org.mozilla.firefox.plist"
process "Freedom" "Library/Preferences/com.80pct.FreedomPlatform.plist"
process "Gitter" \
"Library/Application Support/Gitter/" \
"Library/Preferences/com.troupe.gitter.mac.Gitter.plist"
process "Hazeover" \
"Library/Application Support/com.pointum.hazeover/" \
"Library/Preferences/com.pointum.hazeover.plist"
process "Jaikoz" "Library/Preferences/Jaikoz/"
process "Keybase" \
"Library/Application Support/Keybase/" \
"Library/Preferences/keybase.Electron.plist" \
"Library/Preferences/keybase.ElectronHelper.plist"
process "Kindle" "Library/Containers/com.amazon.Kindle/"
process "Kodi" "Library/Application Support/Kodi/"
process "Little Snitch" \
"Library/Application Support/Objective Development/" \
"Library/Application Support/Little Snitch/" \
"Library/Preferences/at.obdev.LittleSnitchAgent.plist" \
"Library/Preferences/at.obdev.LittleSnitchConfiguration.plist" \
"Library/Preferences/at.obdev.LittleSnitchNetworkMonitor.plist" \
"Library/Preferences/at.obdev.LittleSnitchSoftwareUpdate.plist"
process "Loopback" \
"Library/Application Support/Loopback/" \
"Library/Preferences/com.rogueamoeba.Loopback.plist" \
"Library/Preferences/com.rogueamoeba.loopbackd.plist"
process "Micro Snitch" "Library/Preferences/at.obdev.MicroSnitch.plist"
process "Numi" "Library/Containers/com.dmitrynikolaev.numi.NumiExtension/"
process "Opera" "Library/Application Support/com.operasoftware.Opera/"
process "Parity" "Library/Application Support/io.parity.ethereum/"
process "Pastebot" \
"Library/Preferences/com.tapbots.Pastebot2Mac.plist" \
"Library/Containers/com.tapbots.PAstebot2Mac/"
process "Paragon NTFS" \
"Library/Application Support/com.paragon-software.ntfs.fsapp/" \
"Library/Application Support/com.paragon-software.ntfs.notification-agent/" \
"Library/Preferences/com.paragon-software.ntfs.fsapp.plist" \
"Library/Preferences/com.paragon-software.ntfs.FSMenuApp.plist"
process "Pinstriped" "Library/Application Support/com.pinstriped.pinstriped-mac/"
process "ProtonVPN" \
"Library/Containers/ch.protonvpn.mac/" \
"Library/Containers/ch.protonvpn.ProtonVPNStarter"
process "Screenflow" "Library/Application Support/Telestream/"
process "Signal" \
"Library/Application Support/Signal/" \
"Library/Preferences/org.whispersystems.signal-desktop.helper.plist" \
"Library/Preferences/org.whispersystems.signal-desktop.plist"
process "Sketch" \
"Library/Application Support/com.bohemiancoding.sketch3" \
"Library/Preferences/com.bohemiancoding.sketch3.plist"
process "Skype" \
"Library/Application Support/Skype/" \
"Library/Application Support/Skype Helper/" \
"Library/Preferences/com.skype.skype.plist"
process "Slack" "Library/Containers/com.tinyspeck.slackmacgap/"
process "SoundSource" \
"Library/Application Support/SoundSource/" \
"Library/Preferences/com.rogueamoeba.soundsource.plist"
process "Spotify" \
"Library/Application Support/Spotify/" \
"Library/Preferences/com.spotify.client.plist"
process "Spotifree" "Library/Preferences/de.eneas.Spotifree.plist"
process "Teamviewer" "Library/Preferences/com.teamviewer.teamviewer.preferences.plist"
process "Things" "Library/Containers/com.culturedcode.ThingsMac/"
process "ThoughtTrain" "Library/Preferences/me.simonarcher.ThoughtTrain.plist"
process "Timing" \
"Library/Application Support/info.eurocomp.Timing-setapp/" \
"Library/Application Support/info.eurocomp.Timing-setapp.TimingHelper/" \
"Library/Application Support/info.eurocomp.Timing2/" \
"Library/Application Support/info.eurocomp.TimingHelper/" \
"Library/Preferences/info.eurocomp.Timing-setapp.plist" \
"Library/Preferences/info.eurocomp.Timing-setapp.TimingHelper.fallback.plist" \
"Library/Preferences/info.eurocomp.Timing-setapp.TimingHelper.plist" \
"Library/Preferences/info.eurocomp.Timing2.fallback.plist" \
"Library/Preferences/info.eurocomp.Timing2.plist" \
"Library/Preferences/info.eurocomp.TimingHelper.fallback.plist" \
"Library/Preferences/info.eurocomp.TimingHelper.plist"
process "Tower" \
"Library/Application Support/com.fournova.Tower2/" \
"Library/Preferences/com.fournova.Tower2.plist" \
"Library/Application Support/com.fournova.Tower3/" \
"Library/Preferences/com.fournova.Tower3.plist"
process "Transmit" \
"Library/Preferences/com.panic.Transmit.plist" \
"Library/Application Support/com.panic.Transmit/" \
"Library/Application Support/Transmit/"
process "Transmission" \
"Library/Preferences/org.m0k.transmission.plist" \
"Library/Application Support/Transmission/" \
".config/transmission/"
process "Tunnelblick" \
"Library/Application Support/Tunnelblick/" \
"Library/Preferences/net.tunnelblick.tunnelblick.plist"
process "Typora" \
"Library/Application Support/abnerworks.Typora/" \
"Library/Preferences/abnerworks.Typora.plist"
process "The Unarchiver" "Library/Containers/cx.c3.theunarchiver/"
process "Ubersicht" \
"Library/Application Support/tracesOf.Uebersicht/" \
"Library/Application Support/Übersicht/"
process "Usage" "Library/Application Support/com.mediaatelier.Usage/"
process "VLC" \
"Library/Application Support/org.videolan.vlc/" \
"Library/Preferences/org.videolan.vlc" \
"Library/Preferences/org.videolan.vlc.plist"
process "VMware" \
"Library/Application Support/VMware Fusion/" \
"Library/Preferences/VMware Fusion/" \
"Library/Preferences/com.vmware.fusion.plist" \
"Library/Preferences/com.vmware.fusionStartMenu.plist"
process "WebTorrent" \
"Library/Application Support/WebTorrent/" \
"Library/Preferences/io.webtorrent.webtorrent-helper.plist" \
"Library/Preferences/io.webtorrent.webtorrent.plist"
process "Wire" \
"Library/Containers/com.wearezeta.zclient.mac/" \
"Library/Preferences/com.wearezeta.zclient.mac.plist"
process "Workflowy" \
"Library/Application Support/WorkFlowy/" \
"Library/Preferences/com.workflowy.desktop.helper.plist" \
"Library/Preferences/com.workflowy.desktop.plist"
process "XLD" "Library/Preferences/jp.tmkk.XLD.plist"
process "ZeroNet" "Library/Application Support/ZeroNet/"
# ===============
# Post
# Clean
setup-mac clean
# Finish
if [[ ${#warnings[@]} -eq 0 ]]; then
echo-style --success="Completed successfully"
else
{
echo-style --error="Completed with warnings:"
__print_lines "${warnings[@]}"
} >/dev/stderr
fi
)
# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
macos_state "$@"
fi