KeiSeiKit-1.0/install/lib-backup.sh
Denis Parfionovich 53ada048b2
Some checks failed
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / preflight (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / vps-smoke (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:frustration-matrix,kei-frustration-loop,kei-skill-importer,kei-projects-index,kei-projects-watcher,kei-gdrive-import,kei-leak-matrix,kei-skills,kei-gateway,kei-cron-scheduler,kei-export-trajectories,kei-backend-daytona,kei-d… (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-compute-baremetal,kei-compute-vultr,kei-compute-linode,kei-compute-digitalocean,kei-svc-systemd,kei-llm-bridge-mlx name:hosted-sleep-compute]) (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-diff,kei-scheduler,kei-watch,kei-prune,kei-discover,kei-brain-view,kei-hibernate,kei-ledger-sign,kei-fork name:wave13-15]) (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-git-gitea,kei-git-forgejo,kei-git-gitlab,kei-git-bitbucket,kei-memory-sled,kei-memory-redis,kei-memory-postgres,kei-memory-sqlite,kei-auth-google,kei-auth-apple,kei-auth-magiclink,kei-auth-webauthn,kei-notify-slack,kei-n… (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-ledger,kei-migrate,kei-changelog,kei-memory,kei-store,kei-conflict-scan,kei-refactor-engine,kei-graph-check,kei-shared,kei-dna-index,kei-pet name:core]) (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-machine-probe,kei-llm-ollama,kei-llm-llamacpp,kei-llm-mlx,kei-llm-router,kei-model name:llm-stack]) (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:kei-router,kei-sage,kei-task,kei-chat-store,kei-crossdomain,kei-search-core,kei-content-store,kei-social-store,kei-curator,kei-auth,kei-artifact name:mcp-lbm]) (push) Has been cancelled
CI (Forgejo Actions — self-hosted runner on Mac, host mode) / rust-primitives (map[crates:keisei,kei-forge,kei-runtime,kei-runtime-core,kei-atom-discovery,kei-agent-runtime,kei-capability,kei-provision,kei-entity-store,kei-pipe,kei-cache,kei-spawn,kei-replay name:atom-substrate]) (push) Has been cancelled
fix(install): backup_file mv→cp -a, install --activate-hooks больше не падает
gx10 fail при первой установке (фото юзера 2026-05-18):
  jq: error: Could not open file /home/keisei/.claude/settings.json
  [error] jq-merge produced invalid output; ... unchanged
  [error] install failed at line 126; rolled back

Root cause:
  backup_file() (install/lib-backup.sh:56) делал `mv target backup`,
  затем activate_hooks() → _jq_merge_hooks() пыталась читать оригинал
  target'а через jq — но оригинал уже унесён mv'ом, открыть нечего.
  jq fail → rollback всей установки.

Fix: cp -a вместо mv (как уже сделано в backup_path() для директорий).
Атомарный безопасный бэкап; оригинал остаётся для jq, потом
_jq_merge_hooks атомарно переписывает его mv tmp → target.
2026-05-18 14:33:22 +08:00

66 lines
2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# shellcheck shell=bash
# lib-backup.sh — rollback trap + backup_dir / backup_file helpers.
#
# Every successful backup_dir / per-file backup appends a "ORIGINAL|BACKUP"
# pair to BACKUP_PAIRS. On ERR the trap walks the list in reverse and
# atomically swaps BACKUP back onto ORIGINAL. A boolean guard makes
# rollback idempotent.
#
# Requires: say / warn / err from lib-log.sh.
# Sourced by install.sh; no top-level execution except global var init and
# `trap rollback ERR` inside setup_backup_trap.
BACKUP_PAIRS=()
ROLLED_BACK=0
rollback() {
[ "$ROLLED_BACK" = "1" ] && return 0
ROLLED_BACK=1
if [ "${#BACKUP_PAIRS[@]}" -eq 0 ]; then
err "install failed at line ${BASH_LINENO[0]:-?}; no backups to restore"
return 0
fi
warn "install failed — rolling back ${#BACKUP_PAIRS[@]} backup(s)"
local i pair orig bak
for (( i=${#BACKUP_PAIRS[@]}-1; i>=0; i-- )); do
pair="${BACKUP_PAIRS[$i]}"
orig="${pair%%|*}"
bak="${pair#*|}"
if [ -e "$bak" ]; then
if [ -d "$orig" ] || [ -f "$orig" ]; then
rm -rf "$orig"
fi
mv "$bak" "$orig"
say " restored $orig from $bak"
fi
done
err "install failed at line ${BASH_LINENO[0]:-?}; rolled back"
}
setup_backup_trap() {
trap rollback ERR
}
backup_dir() {
local target="$1"
[ -d "$target" ] || return 0
if [ -z "$(find "$target" -type f -print -quit 2>/dev/null)" ]; then
return 0
fi
local backup="${target}.bak-$(date +%s)"
cp -a "$target" "$backup"
BACKUP_PAIRS+=("$target|$backup")
say "backed up existing $target to $backup"
}
backup_file() {
local target="$1"
[ -f "$target" ] || return 0
local backup="${target}.bak-$(date +%s)"
# cp -a, НЕ mv — _jq_merge_hooks ниже читает оригинал target'а после
# backup_file. С mv оригинал исчезает → jq не может open file →
# «invalid output» → rollback (gx10 fail 2026-05-18).
cp -a "$target" "$backup"
BACKUP_PAIRS+=("$target|$backup")
say "backed up existing $target to $backup"
}