Files
System-Sync-Restore/bin/ssr-restore
T
Oliver Surke 698cbe145a feat: async iCloud prefetch + keep-local for Mackup folder
lib/icloud.sh (new):
  ssr::icloud::prefetch <dir>    — spawn background open -g requests for
    all .icloud stubs; returns immediately so other work can run in parallel.
  ssr::icloud::wait <dir> [sec]  — poll until stubs are gone or timeout
    (default 180s). Logs progress every 5s.
  ssr::icloud::keep_local <dir>  — after sync, removes
    com.apple.icloud.evictable xattr from every real file + updates
    access-time (LRU delay), preventing iCloud from re-creating stubs
    before the next run. Best-effort (no public pin API on macOS CLI).

bin/ssr-sync:
  - prefetch Mackup folder immediately after cloud is available (before
    brew update/upgrade) so downloads proceed in the background.
  - ssr::icloud::wait called just before mackup backup.
  - ssr::icloud::keep_local called after sync completes.
  - SSR_ICLOUD_WAIT_TIMEOUT (default 180) and SSR_ICLOUD_KEEP_LOCAL
    (default true) config toggles added.

bin/ssr-restore:
  - prefetch Mackup folder right after cloud is validated, before the
    brew bundle install step, maximising download time available.

config/ssr.conf.example:
  Added SSR_ICLOUD_WAIT_TIMEOUT and SSR_ICLOUD_KEEP_LOCAL keys.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 09:46:12 +02:00

118 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# ssr-restore — restore brew packages, mackup data, macOS defaults.
#
# Flags:
# --resume Skip steps already marked 'ok' in the previous run's state.
# -h|--help Show usage.
set -euo pipefail
SSR_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=../lib/common.sh
source "$SSR_ROOT/lib/common.sh"
# shellcheck source=../lib/cloud.sh
source "$SSR_ROOT/lib/cloud.sh"
# shellcheck source=../lib/brew.sh
source "$SSR_ROOT/lib/brew.sh"
# shellcheck source=../lib/mackup.sh
source "$SSR_ROOT/lib/mackup.sh"
# shellcheck source=../lib/macos.sh
source "$SSR_ROOT/lib/macos.sh"
# shellcheck source=../lib/macos-prefs.sh
source "$SSR_ROOT/lib/macos-prefs.sh"
# shellcheck source=../lib/restore-state.sh
source "$SSR_ROOT/lib/restore-state.sh"
# shellcheck source=../lib/icloud.sh
source "$SSR_ROOT/lib/icloud.sh"
ssr::require_macos
ssr::load_config
usage() {
cat <<EOF
Usage: ssr restore [--resume] [BREWFILE]
Restores Homebrew packages, Mackup dotfiles and macOS settings.
--resume Skip steps already completed in a previous (aborted) run.
State is read from: \$SSR_STATE_DIR/restore-state
If BREWFILE is omitted, the latest cloud backup is used:
<cloud>/BACKUP/<host> - <uuid>/Brewfile
EOF
}
# --- Argument parsing --------------------------------------------------------
SSR_RESTORE_RESUME=false
BREWFILE_ARG=""
while [[ $# -gt 0 ]]; do
case "$1" in
--resume) SSR_RESTORE_RESUME=true; shift ;;
-h|--help) usage; exit 0 ;;
-*) ssr::die "Unknown flag: $1" ;;
*) BREWFILE_ARG="$1"; shift ;;
esac
done
export SSR_RESTORE_RESUME
# --- Cloud + Brewfile --------------------------------------------------------
ssr::log "ssr restore starting${SSR_RESTORE_RESUME:+ (--resume mode)}"
_cloud_dir="$(ssr::cloud::resolve_dir 2>/dev/null)" || true
if [[ -z "$_cloud_dir" || ! -d "$_cloud_dir" ]]; then
ssr::warn "Cloud directory not yet accessible: ${_cloud_dir:-<unknown>}"
ssr::warn "Sign in to ${SSR_CLOUD_PROVIDER:-iCloud} and ensure the drive is mounted, then press Enter."
ssr::confirm "Cloud drive ready — proceed with restore?" 120 \
|| ssr::die "Aborted by user"
fi
ssr::cloud::ensure_available
backup_dir="$(ssr::cloud::backup_dir)"
mackup_dir="$(ssr::cloud::mackup_dir)"
brewfile="${BREWFILE_ARG:-$backup_dir/Brewfile}"
[[ -f "$brewfile" ]] || ssr::die "Brewfile not found: $brewfile"
ssr::log "Using Brewfile: $brewfile"
# Initialise state (archives previous state file if present).
ssr::rs::init "$brewfile"
# Trigger async iCloud prefetch for the Mackup folder as early as possible
# so files have time to download while brew bundle install runs.
ssr::icloud::prefetch "$mackup_dir"
# --- Step helpers ------------------------------------------------------------
# Groups mackup install + configure + restore into a single resumable step.
# ssr::mackup::restore already calls ssr::icloud::wait internally.
_ssr_mackup_restore_step() {
ssr::mackup::ensure_installed
ssr::mackup::configure "$mackup_dir"
ssr::mackup::restore
}
# --- Steps -------------------------------------------------------------------
ssr::rs::run brew_install "Homebrew install" ssr::brew::ensure_installed
ssr::rs::run brew_restore "Brew bundle install" ssr::brew::restore "$brewfile"
ssr::rs::run mackup_restore "Mackup restore" _ssr_mackup_restore_step
ssr::rs::run macos_defaults "macOS defaults" ssr::macos::apply_defaults
if [[ "${SSR_SNAPSHOT_MACOS_PREFS:-true}" == "true" ]]; then
ssr::rs::run prefs_import "Prefs snapshot import" ssr::macos::prefs_import "$backup_dir"
else
ssr::rs::set "step_prefs_import" "skipped"
fi
# Optional: open installed cask apps for first-run config (not tracked as a
# resumable step — it's side-effect-only and always safe to re-run).
ssr::macos::open_installed_casks
# Mark overall completion time.
ssr::rs::set "finished" "$(ssr::ts)"
# --- Final report ------------------------------------------------------------
ssr::rs::report
ssr::ok "ssr restore completed"