Files
System-Sync-Restore/bin/ssr-sync
T
Oliver Surke 5ed6279b51 Snapshot and restore macOS preference domains
Adds a round-trip mechanism for macOS system preferences that are not
covered by Homebrew or Mackup (Dock layout, Finder options, global
keyboard shortcuts, input sources, Spaces config, menu-bar clock,
NSGlobalDomain).

sync: `ssr sync` exports each configured domain with `defaults export`
and writes one plist per domain to <backup_dir>/macos-prefs/.  Export is
soft-fail per domain so one bad domain doesn't abort the sync. Gated by
SSR_SNAPSHOT_MACOS_PREFS=true (default on).

restore: `ssr restore` replays the snapshot with `defaults import` after
the declarative config/macos-defaults.sh runs, so the snapshot is the
last writer and wins for accepted domains.  Before importing, the restore
detects domains that both the snapshot and macos-defaults.sh touch and
prompts interactively:

  Domain `com.apple.dock`:
    Snapshot will OVERRIDE values set by macos-defaults.sh.
    [A]ccept snapshot / [r]eject (keep declarative)? [A/r]:

Non-TTY (launchd, CI) falls back to SSR_PREFS_CONFLICT_DEFAULT (default
"accept"). SSR_ASSUME_YES=true skips all prompts. After import, Dock,
Finder, SystemUIServer and cfprefsd are restarted once.

New config keys: SSR_SNAPSHOT_MACOS_PREFS, SSR_MACOS_PREF_DOMAINS,
SSR_MACOS_PREF_DOMAINS_EXTRA, SSR_PREFS_CONFLICT_DEFAULT, SSR_ASSUME_YES.

ssr-status surfaces the snapshotted-domain count.
README documents the snapshot flow, default domain table, conflict UX,
and the five new config keys.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 20:19:49 +02:00

72 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# ssr-sync — back up Homebrew + Mackup state to the configured cloud dir.
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/apps.sh
source "$SSR_ROOT/lib/apps.sh"
# shellcheck source=../lib/macos-prefs.sh
source "$SSR_ROOT/lib/macos-prefs.sh"
ssr::require_macos
ssr::load_config
ssr::log "ssr sync starting"
ssr::syslog "sync starting"
ssr::cloud::ensure_available
backup_dir="$(ssr::cloud::backup_dir)"
mackup_dir="$(ssr::cloud::mackup_dir)"
ssr::ensure_dir "$backup_dir"
ssr::ensure_dir "$mackup_dir"
ssr::brew::ensure_installed
ssr::mackup::ensure_installed
ssr::mackup::configure "$mackup_dir"
ssr::brew::update
ssr::brew::upgrade
ssr::mackup::backup
ssr::brew::dump "$backup_dir"
ssr::brew::cleanup
# Surface .app bundles that aren't covered by brew cask or the App Store —
# they would otherwise be missed on `ssr restore` and need manual reinstall.
if [[ "${SSR_SCAN_UNMANAGED_APPS:-true}" == "true" ]]; then
ssr::log "Scanning for unmanaged .app bundles"
report_path="$(ssr::apps::write_report "$backup_dir")"
# Each row in the Markdown table starts with `| [`; count those.
count="$(grep -cE '^\| \[' "$report_path" 2>/dev/null || echo 0)"
if [[ "$count" -gt 0 ]]; then
ssr::warn "Found $count unmanaged app(s) — manual reinstall on restore:"
# Extract the linked app name from each table row for a quick console summary.
grep -E '^\| \[' "$report_path" \
| sed -E 's/^\| \[([^]]+)\].*/ - \1/'
ssr::log "Full report: $report_path"
else
ssr::ok "No unmanaged apps detected."
fi
fi
# Snapshot macOS preference domains so they can be restored on a new machine.
if [[ "${SSR_SNAPSHOT_MACOS_PREFS:-true}" == "true" ]]; then
ssr::log "Snapshotting macOS preferences"
ssr::macos::prefs_export "$backup_dir"
fi
# Record sync time so `ssr status` can report freshness.
state_dir="${SSR_STATE_DIR:-$HOME/.local/state/ssr}"
ssr::ensure_dir "$state_dir"
date '+%Y-%m-%dT%H:%M:%S%z' > "$state_dir/last-sync"
ssr::ok "ssr sync completed → $backup_dir"
ssr::syslog "sync completed"