#!/usr/bin/env bash # muse-git-backup.sh — git-bridge backup mirror for muse/musehub -> GitHub # See MuseHub staging issue #127 for full context. # # Usage: # ./muse-git-backup.sh preflight # ./muse-git-backup.sh replay-muse # ./muse-git-backup.sh replay-musehub # ./muse-git-backup.sh github-push # ./muse-git-backup.sh verify # ./muse-git-backup.sh incremental-muse # ./muse-git-backup.sh incremental-musehub # # Each subcommand is independent and safe to re-run. This script never # touches ~/ecosystem/muse or ~/ecosystem/musehub — every operation targets # the copies under /Users/gabriel/dev/copies 2/ and brand-new directories/ # GitHub repos this script creates fresh. Run as ./muse-git-backup.sh , # never sourced into your interactive shell — that's what makes `exit` # inside this script safe (it only ends the script's own process). set -euo pipefail MUSE_COPY="/Users/gabriel/dev/copies 2/muse 03" MUSEHUB_COPY="/Users/gabriel/dev/copies 2/musehub 03" MUSE_GIT_BACKUP="/Users/gabriel/dev/copies 2/muse-git-backup" MUSEHUB_GIT_BACKUP="/Users/gabriel/dev/copies 2/musehub-git-backup" GH_MUSE_REPO="gabriel/muse-backup" GH_MUSEHUB_REPO="gabriel/musehub-backup" guard() { case "$1" in *ecosystem*) echo "❌ REFUSED: path contains 'ecosystem': $1"; exit 1 ;; esac } cmd_preflight() { test -d "$MUSE_COPY/.muse" && echo "muse copy: OK" || { echo "muse copy: MISSING .muse/ — STOP"; exit 1; } test -d "$MUSEHUB_COPY/.muse" && echo "musehub copy: OK" || { echo "musehub copy: MISSING .muse/ — STOP"; exit 1; } if [ -L "$MUSE_COPY" ]; then echo "STOP — muse copy is a symlink"; exit 1; fi if [ -L "$MUSEHUB_COPY" ]; then echo "STOP — musehub copy is a symlink"; exit 1; fi echo "muse copy: real directory, not a symlink" echo "musehub copy: real directory, not a symlink" echo "muse commit count:" muse -C "$MUSE_COPY" rev-list --count HEAD --json echo "musehub commit count:" muse -C "$MUSEHUB_COPY" rev-list --count HEAD --json } _replay_full_history() { local muse_copy="$1" git_backup="$2" label="$3" guard "$git_backup" mkdir -p "$git_backup" if [ ! -d "$git_backup/.git" ]; then git -C "$git_backup" init -q git -C "$git_backup" config user.email "gabriel@tellurstori.com" git -C "$git_backup" config user.name "gabriel" git -C "$git_backup" commit --allow-empty -qm "init ($label git-bridge backup mirror)" fi local commits total n commits=$(muse -C "$muse_copy" rev-list --reverse HEAD --json | python3 -c "import sys,json; print('\n'.join(json.load(sys.stdin)['commit_ids']))") if [ -n "${LIMIT:-}" ]; then commits=$(echo "$commits" | head -n "$LIMIT") echo "LIMIT=$LIMIT set — replaying only the first $LIMIT commits (dry run)" echo "⚠️ Before running the full replay, delete $git_backup — replay always" echo " starts from commit 1, so re-running without cleanup duplicates these." fi total=$(echo "$commits" | wc -l | tr -d ' ') n=0 while IFS= read -r cid; do n=$((n + 1)) echo "[$n/$total] exporting $cid" (cd "$muse_copy" && muse bridge git-export \ --muse-ref "$cid" \ --git-dir "$git_backup" \ --git-branch muse-mirror \ --no-push \ --allow-empty \ --json) done <<< "$commits" echo "git history length:" git -C "$git_backup" rev-list --count muse-mirror } cmd_replay_muse() { _replay_full_history "$MUSE_COPY" "$MUSE_GIT_BACKUP" "muse" } cmd_replay_musehub() { _replay_full_history "$MUSEHUB_COPY" "$MUSEHUB_GIT_BACKUP" "musehub" } cmd_github_push() { gh auth status || { echo "Run 'gh auth login' first"; exit 1; } gh repo view "$GH_MUSE_REPO" >/dev/null 2>&1 || gh repo create "$GH_MUSE_REPO" --private --description "Git-bridge backup mirror of the muse Muse repo" gh repo view "$GH_MUSEHUB_REPO" >/dev/null 2>&1 || gh repo create "$GH_MUSEHUB_REPO" --private --description "Git-bridge backup mirror of the musehub Muse repo" git -C "$MUSE_GIT_BACKUP" remote get-url origin >/dev/null 2>&1 || git -C "$MUSE_GIT_BACKUP" remote add origin "git@github.com:$GH_MUSE_REPO.git" git -C "$MUSEHUB_GIT_BACKUP" remote get-url origin >/dev/null 2>&1 || git -C "$MUSEHUB_GIT_BACKUP" remote add origin "git@github.com:$GH_MUSEHUB_REPO.git" git -C "$MUSE_GIT_BACKUP" push -u origin muse-mirror git -C "$MUSEHUB_GIT_BACKUP" push -u origin muse-mirror } cmd_verify() { rm -rf /tmp/verify-muse-backup git clone "git@github.com:$GH_MUSE_REPO.git" /tmp/verify-muse-backup git -C /tmp/verify-muse-backup checkout muse-mirror echo "--- muse diff (expect no output) ---" diff -rq --exclude=.git --exclude=.muse "$MUSE_COPY" /tmp/verify-muse-backup || true rm -rf /tmp/verify-musehub-backup git clone "git@github.com:$GH_MUSEHUB_REPO.git" /tmp/verify-musehub-backup git -C /tmp/verify-musehub-backup checkout muse-mirror echo "--- musehub diff (expect no output) ---" diff -rq --exclude=.git --exclude=.muse "$MUSEHUB_COPY" /tmp/verify-musehub-backup || true } _incremental() { local muse_copy="$1" git_backup="$2" local last_bridged new_commits last_bridged=$(python3 -c " import tomllib with open('$muse_copy/.muse/git-bridge.toml', 'rb') as f: state = tomllib.load(f) print(state['last_export']['muse_commit_id']) ") echo "Last bridged commit: $last_bridged" new_commits=$(muse -C "$muse_copy" rev-list "$last_bridged..HEAD" --reverse --json | python3 -c "import sys,json; print('\n'.join(json.load(sys.stdin)['commit_ids']))") if [ -z "$new_commits" ]; then echo "Nothing new since the last backup — done." return 0 fi while IFS= read -r cid; do echo "exporting $cid" (cd "$muse_copy" && muse bridge git-export \ --muse-ref "$cid" \ --git-dir "$git_backup" \ --git-branch muse-mirror \ --no-push \ --allow-empty \ --json) done <<< "$new_commits" git -C "$git_backup" push origin muse-mirror } cmd_incremental_muse() { _incremental "$MUSE_COPY" "$MUSE_GIT_BACKUP" } cmd_incremental_musehub() { _incremental "$MUSEHUB_COPY" "$MUSEHUB_GIT_BACKUP" } SUBCOMMAND="${1:-}" LIMIT="" if [ "${2:-}" = "--limit" ]; then LIMIT="${3:?--limit requires a number}" fi case "$SUBCOMMAND" in preflight) cmd_preflight ;; replay-muse) cmd_replay_muse ;; replay-musehub) cmd_replay_musehub ;; github-push) cmd_github_push ;; verify) cmd_verify ;; incremental-muse) cmd_incremental_muse ;; incremental-musehub) cmd_incremental_musehub ;; *) echo "Usage: $0 {preflight|replay-muse|replay-musehub|github-push|verify|incremental-muse|incremental-musehub} [--limit N]" exit 1 ;; esac