Download scripts/openclaw-restore.sh from SZFSPC/apple: direct link, hf CLI and curl.
- Browser
- Download file 7.45 kB
-
https://huggingface.co/spaces/SZFSPC/apple/resolve/main/scripts/openclaw-restore.sh
- Command line
-
hf download hf://spaces/SZFSPC/apple/scripts/openclaw-restore.sh
-
curl -L -o openclaw-restore.sh https://huggingface.co/spaces/SZFSPC/apple/resolve/main/scripts/openclaw-restore.sh
7.45 kB
| set -euo pipefail | |
| # ---- Force line-buffered stdout for docker logs ---- | |
| # In Docker, stdout pipe is fully buffered (4KB+). Without this, output | |
| # may be lost if we exit before the buffer fills. | |
| if [[ -z "${_RESTORE_UNBUFFERED:-}" ]]; then | |
| export _RESTORE_UNBUFFERED=1 | |
| if command -v stdbuf >/dev/null 2>&1; then | |
| exec stdbuf -oL -eL "$0" "$@" | |
| fi | |
| fi | |
| # Preserve all existing environment variables (from HF Space / os.environ) | |
| # before sourcing env files, so HF Space always takes full priority. | |
| declare -A _PRESERVED_ENV | |
| while IFS='=' read -r _key _value; do | |
| _PRESERVED_ENV["$_key"]="$_value" | |
| done < <(env) | |
| unset _key _value | |
| # 恢复日志配置 | |
| RESTORE_LOG_DIR="${OPENCLAW_RESTORE_LOG_DIR:-/var/log/openclaw}" | |
| RESTORE_LOG_FILE="${RESTORE_LOG_DIR}/restore.log" | |
| # 确保日志目录存在 | |
| mkdir -p "$RESTORE_LOG_DIR" | |
| # 日志函数 | |
| restore_log() { | |
| local level="$1" | |
| shift | |
| local message="$*" | |
| local timestamp | |
| timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| echo "[$timestamp] [$level] $message" | timeout 1 tee -a "$RESTORE_LOG_FILE" 2>/dev/null || echo "[$timestamp] [$level] $message" | |
| } | |
| restore_log_info() { | |
| restore_log "INFO" "$@" | |
| } | |
| restore_log_warn() { | |
| restore_log "WARN" "$@" | |
| } | |
| restore_log_error() { | |
| restore_log "ERROR" "$@" >&2 | |
| } | |
| restore_log_info "=== Starting Backup Restore ===" | |
| restore_log_info "Restore started at: $(date -u +"%Y-%m-%d %H:%M:%S UTC")" | |
| BACKUP_ENV_FILE_PATH="${OPENCLAW_BACKUP_ENV_FILE_PATH:-/root/.env.d/openclaw-backup.env}" | |
| if [[ -f "$BACKUP_ENV_FILE_PATH" ]]; then | |
| # shellcheck disable=SC1091 | |
| source "$BACKUP_ENV_FILE_PATH" | |
| restore_log_info "Loaded backup environment from: $BACKUP_ENV_FILE_PATH" | |
| else | |
| restore_log_warn "Backup environment file not found: $BACKUP_ENV_FILE_PATH" | |
| fi | |
| # Restore all previously existing variables (HF Space takes priority over backup env files) | |
| for _var in "${!_PRESERVED_ENV[@]}"; do | |
| export "$_var"="${_PRESERVED_ENV[$_var]}" | |
| done | |
| unset _PRESERVED_ENV _var | |
| # 记录恢复配置信息 | |
| restore_log_info "Restore Configuration:" | |
| # 多数据集支持:优先使用 OPENCLAW_RESTORE_DATASET_REPO,否则使用 OPENCLAW_BACKUP_DATASET_REPO | |
| RESTORE_DATASET_REPO="${OPENCLAW_RESTORE_DATASET_REPO:-${OPENCLAW_BACKUP_DATASET_REPO:-}}" | |
| if [[ -z "${RESTORE_DATASET_REPO:-}" ]]; then | |
| restore_log_warn "Restore skipped: no dataset configured" | |
| restore_log_warn " OPENCLAW_RESTORE_DATASET_REPO: ${OPENCLAW_RESTORE_DATASET_REPO:-<not set>}" | |
| restore_log_warn " OPENCLAW_BACKUP_DATASET_REPO: ${OPENCLAW_BACKUP_DATASET_REPO:-<not set>}" | |
| # Even though restore is skipped, we must create the completed flag so | |
| # the startup pipeline (hf-entrypoint.sh) does not hang forever. | |
| flag_file="/root/dataset-restore-completed" | |
| failed_flag="/root/dataset-restore-failed" | |
| rm -f "$failed_flag" 2>/dev/null || true | |
| mkdir -p "$(dirname "$flag_file")" | |
| echo "skipped-no-dataset-$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$flag_file" | |
| restore_log_info "Created restore-completed flag (skipped mode): $flag_file" | |
| exit 0 | |
| fi | |
| restore_log_info " Restore Source Dataset: ${RESTORE_DATASET_REPO}" | |
| restore_log_info " Backup Dataset: ${OPENCLAW_BACKUP_DATASET_REPO:-<not set>}" | |
| restore_log_info " State Dir: ${OPENCLAW_STATE_DIR:-/root/.openclaw}" | |
| restore_log_info " Backup Source Dir: ${OPENCLAW_BACKUP_SOURCE_DIR:-<not set>}" | |
| restore_log_info " Path Prefix: ${OPENCLAW_BACKUP_PATH_PREFIX:-backups}" | |
| # 检查数据集配置 | |
| if [[ -z "${OPENCLAW_BACKUP_DATASET_REPO:-}" ]]; then | |
| restore_log_warn "Restore skipped: OPENCLAW_BACKUP_DATASET_REPO is not set" | |
| restore_log_warn "Backup target dataset is required for restore" | |
| # Same as above: create completed flag to avoid pipeline deadlock | |
| flag_file="/root/dataset-restore-completed" | |
| failed_flag="/root/dataset-restore-failed" | |
| rm -f "$failed_flag" 2>/dev/null || true | |
| mkdir -p "$(dirname "$flag_file")" | |
| echo "skipped-no-backup-dataset-$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$flag_file" | |
| restore_log_info "Created restore-completed flag (skipped mode): $flag_file" | |
| exit 0 | |
| fi | |
| # 注意:容器重启/重建时总是执行恢复,这是设计决定的 | |
| restore_log_info "" | |
| restore_log_info "Starting restore process from dataset: ${RESTORE_DATASET_REPO}" | |
| restore_log_info "----------------------------------------" | |
| # Run restore with real-time logging to restore.log | |
| # Using tee to write output to log file immediately as it's produced | |
| restore_exit_code=0 | |
| python3 /opt/openclaw-hf/openclaw_hf/backup.py restore 2>&1 | tee -a "$RESTORE_LOG_FILE" || restore_exit_code=$? | |
| if [[ $restore_exit_code -eq 0 ]]; then | |
| restore_log_info "----------------------------------------" | |
| restore_log_info "=== Backup Restore Completed Successfully ===" | |
| restore_log_info "Restore source: ${RESTORE_DATASET_REPO}" | |
| restore_log_info "Restore finished at: $(date -u +"%Y-%m-%d %H:%M:%S UTC")" | |
| # 创建恢复成功标记,标记恢复完成后才允许备份 | |
| # 持久化到 /root/.openclaw/(不是 /tmp/,因为 tmpfs 重启后会丢失) | |
| flag_file="/root/dataset-restore-completed" | |
| failed_flag="/root/dataset-restore-failed" | |
| rm -f "$failed_flag" 2>/dev/null || true | |
| mkdir -p "$(dirname "$flag_file")" | |
| date -u +"%Y-%m-%dT%H:%M:%SZ" > "$flag_file" | |
| restore_log_info "Created restore completed flag: $flag_file" | |
| exit 0 | |
| else | |
| restore_log_error "----------------------------------------" | |
| restore_log_error "=== Backup Restore Failed ===" | |
| restore_log_error "Restore source: ${RESTORE_DATASET_REPO}" | |
| restore_log_error "Restore failed at: $(date -u +"%Y-%m-%d %H:%M:%S UTC")" | |
| restore_log_error "Exit code from backup.py restore: ${restore_exit_code}" | |
| # 创建失败标记,通知 hf-entrypoint.sh 需要重启 Space | |
| # 持久化到 /root/(不是 /tmp/,因为 tmpfs 重启后会丢失) | |
| RESTORE_FAILED_FILE="/root/dataset-restore-failed" | |
| COMPLETED_FLAG_FILE="/root/dataset-restore-completed" | |
| rm -f "$COMPLETED_FLAG_FILE" 2>/dev/null || true | |
| mkdir -p "$(dirname "$RESTORE_FAILED_FILE")" | |
| date -u +"%Y-%m-%dT%H:%M:%SZ" > "$RESTORE_FAILED_FILE" | |
| restore_log_info "Created restore failed flag: $RESTORE_FAILED_FILE" | |
| # ============================================================ | |
| # FLUSHING OUTPUT — DO NOT EXIT PREMATURELY | |
| # Giving docker runtime 30+ seconds to drain stdout pipe. | |
| # Progressive RESTORE-FLUSH_01..30 are emitted 1s apart — | |
| # the highest-numbered marker you see in docker logs tells | |
| # you how much output was captured before the container died. | |
| # ============================================================ | |
| sync 2>/dev/null || true | |
| echo "========================================================================" | |
| echo " ABOUT TO EXIT (code 1) — flushing output to docker logs..." | |
| echo " Started: $(date -u +"%Y-%m-%dT%H:%M:%SZ")" | |
| echo " Diagnostics above: FULL dump of restore.log + gateway stderr" | |
| echo "========================================================================" | |
| sleep 1 | |
| for _flush_n in $(seq 1 30); do | |
| printf -- "-- RESTORE-FLUSH_%02d %s --\n" "$_flush_n" "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" | |
| sleep 1 | |
| done | |
| echo "========================================================================" | |
| echo " ALL DIAGNOSTICS FLUSHED TO STDOUT — exiting now" | |
| echo " If you see this line and RESTORE-FLUSH_30, output was FULLY captured" | |
| echo "========================================================================" | |
| sync 2>/dev/null || true | |
| sleep 1 | |
| exit 1 | |
| fi | |