Download scripts/openclaw-backup-cron.sh from SZFSPC/apple: direct link, hf CLI and curl.
- Browser
- Download file 8.68 kB
-
https://huggingface.co/spaces/SZFSPC/apple/resolve/main/scripts/openclaw-backup-cron.sh
- Command line
-
hf download hf://spaces/SZFSPC/apple/scripts/openclaw-backup-cron.sh
-
curl -L -o openclaw-backup-cron.sh https://huggingface.co/spaces/SZFSPC/apple/resolve/main/scripts/openclaw-backup-cron.sh
8.68 kB
| # | |
| # OpenClaw Backup Cron Script with Health Check Integration | |
| # 集成健康检查的备份脚本 | |
| # | |
| set -euo pipefail | |
| BACKUP_ENV_FILE_PATH="${OPENCLAW_BACKUP_ENV_FILE_PATH:-/root/.env.d/openclaw-backup.env}" | |
| BACKUP_LOG_DIR="${OPENCLAW_BACKUP_LOG_DIR:-/var/log/openclaw}" | |
| BACKUP_LOG_FILE="${BACKUP_LOG_DIR}/backup.log" | |
| HEALTH_CHECK_ENABLED="${OPENCLAW_BACKUP_HEALTH_CHECK_ENABLED:-false}" | |
| HEALTH_CHECK_BEFORE="${OPENCLAW_BACKUP_HEALTH_CHECK_BEFORE:-false}" | |
| HEALTH_CHECK_AFTER="${OPENCLAW_BACKUP_HEALTH_CHECK_AFTER:-false}" | |
| # 统一备份时间间隔(与 Dockerfile 和 watchdog.sh 保持一致) | |
| MAX_BACKUP_AGE_MINUTES="${MAX_BACKUP_AGE_MINUTES:-30}" | |
| FORCE_BACKUP_INTERVAL="${FORCE_BACKUP_INTERVAL:-3600}" | |
| # 共享锁文件(与 watchdog 共享,避免 cron 和 watchdog 同时触发备份) | |
| SHARED_BACKUP_LOCK_FILE="/var/run/openclaw-backup-shared.lock" | |
| # Timestamp function | |
| timestamp() { | |
| date '+%Y-%m-%dT%H:%M:%S' | |
| } | |
| # Logging function - 只写入文件,避免Supervisor双重记录 | |
| log() { | |
| echo "[$(timestamp)] $*" >> "$BACKUP_LOG_FILE" | |
| } | |
| is_true() { | |
| local value | |
| value="$(printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]')" | |
| [[ "$value" == "1" || "$value" == "true" || "$value" == "yes" || "$value" == "on" ]] | |
| } | |
| # Load environment | |
| load_env() { | |
| if [[ -f "$BACKUP_ENV_FILE_PATH" ]]; then | |
| # shellcheck disable=SC1091 | |
| if ! source "$BACKUP_ENV_FILE_PATH"; then | |
| log "ERROR: Failed to source $BACKUP_ENV_FILE_PATH" | |
| return 1 | |
| fi | |
| log "INFO: Loaded environment from $BACKUP_ENV_FILE_PATH" | |
| else | |
| log "WARN: Environment file not found: $BACKUP_ENV_FILE_PATH" | |
| fi | |
| } | |
| # Run health check | |
| run_health_check() { | |
| if ! is_true "${HEALTH_CHECK_ENABLED:-false}"; then | |
| log "INFO: Health check is disabled" | |
| return 0 | |
| fi | |
| log "INFO: Running pre-backup health check..." | |
| if /usr/local/bin/openclaw-backup-health.sh --check >> "$BACKUP_LOG_FILE" 2>&1; then | |
| log "INFO: Health check passed" | |
| return 0 | |
| else | |
| log "WARN: Health check failed, attempting repair..." | |
| # Try to repair | |
| if /usr/local/bin/openclaw-backup-health.sh --repair >> "$BACKUP_LOG_FILE" 2>&1; then | |
| log "INFO: Repair successful, retrying health check..." | |
| # Retry health check | |
| if /usr/local/bin/openclaw-backup-health.sh --check >> "$BACKUP_LOG_FILE" 2>&1; then | |
| log "INFO: Health check passed after repair" | |
| return 0 | |
| else | |
| log "ERROR: Health check still failing after repair" | |
| return 1 | |
| fi | |
| else | |
| log "ERROR: Repair failed" | |
| return 1 | |
| fi | |
| fi | |
| } | |
| # Run backup with retry | |
| run_backup_with_retry() { | |
| local max_retries="${OPENCLAW_BACKUP_MAX_RETRIES:-3}" | |
| local retry_count=0 | |
| local backup_success=false | |
| while [[ $retry_count -lt $max_retries ]]; do | |
| ((retry_count++)) | |
| log "INFO: Backup attempt $retry_count/$max_retries" | |
| local exit_code=0 | |
| python3 /opt/openclaw-hf/openclaw_hf/backup.py backup >> "$BACKUP_LOG_FILE" 2>&1 || exit_code=$? | |
| if [[ $exit_code -eq 0 ]]; then | |
| log "INFO: Backup completed successfully on attempt $retry_count" | |
| backup_success=true | |
| break | |
| else | |
| log "ERROR: Backup failed on attempt $retry_count (exit code: $exit_code)" | |
| if [[ $retry_count -lt $max_retries ]]; then | |
| local retry_delay=$((retry_count * 10)) | |
| log "INFO: Waiting ${retry_delay}s before retry..." | |
| sleep $retry_delay | |
| # Try repair before next attempt | |
| log "INFO: Attempting repair before retry..." | |
| /usr/local/bin/openclaw-backup-health.sh --repair >> "$BACKUP_LOG_FILE" 2>&1 || true | |
| fi | |
| fi | |
| done | |
| if [[ "$backup_success" == "true" ]]; then | |
| return 0 | |
| else | |
| log "ERROR: Backup failed after $max_retries attempts" | |
| return 1 | |
| fi | |
| } | |
| # Post-backup health check | |
| post_backup_check() { | |
| if ! is_true "${HEALTH_CHECK_AFTER:-false}"; then | |
| return 0 | |
| fi | |
| log "INFO: Running post-backup health check..." | |
| # Check if backup log file exists | |
| if [[ ! -f "$BACKUP_LOG_FILE" ]]; then | |
| log "ERROR: Post-backup check: Backup log file not found, backup may have failed silently" | |
| return 1 | |
| fi | |
| local last_backup | |
| last_backup=$(grep -iE "full backup complete|incremental backup complete|backup uploaded" "$BACKUP_LOG_FILE" | tail -1) | |
| if [[ -n "$last_backup" ]]; then | |
| log "INFO: Post-backup check: Backup log shows completion" | |
| return 0 | |
| else | |
| log "WARN: Post-backup check: No completion found in logs" | |
| return 1 | |
| fi | |
| } | |
| # Main backup function | |
| main() { | |
| # Ensure log directory exists | |
| mkdir -p "$BACKUP_LOG_DIR" | |
| # ---- Prevent concurrent backups ---- | |
| # Check if backup.py or another cron instance is already running (exclude self and pgrep itself) | |
| local my_pid=$$ | |
| # Note: pgrep matches its own command line when searching for "openclaw-backup-watchdog", so we exclude lines containing "pgrep -af" | |
| # ---- Prevent concurrent backups ---- | |
| # Check if backup.py is already running (the actual backup process) | |
| # We only check for backup.py here, not the cron/watchdog scripts themselves, | |
| # to avoid false positives from pgrep matching its own command line | |
| local backup_pids | |
| backup_pids=$(pgrep -f "backup\.py" 2>/dev/null | grep -v "^${my_pid}" || true) | |
| if [[ -n "$backup_pids" ]]; then | |
| log "WARN: backup.py is already running (PIDs: $backup_pids), skipping this cron trigger" | |
| exit 0 | |
| fi | |
| # ---- 获取共享备份锁(与 watchdog 共用,30 秒超时放弃)---- | |
| local lock_fd=200 | |
| local lock_acquired=false | |
| mkdir -p "$(dirname "$SHARED_BACKUP_LOCK_FILE")" 2>/dev/null || true | |
| if eval "exec ${lock_fd}>>$SHARED_BACKUP_LOCK_FILE" 2>/dev/null; then | |
| if flock -n $lock_fd 2>/dev/null; then | |
| lock_acquired=true | |
| else | |
| # 快速获取失败,尝试 30 秒等待 | |
| if flock -w 30 $lock_fd 2>/dev/null; then | |
| lock_acquired=true | |
| else | |
| log "WARN: Could not acquire shared backup lock within 30s, another process is backing up, skipping" | |
| exit 0 | |
| fi | |
| fi | |
| fi | |
| # 获取锁后再次检查(避免等待期间有新进程启动) | |
| if pgrep -af "backup\.py" 2>/dev/null | grep -v "^${my_pid}" | grep -q "backup\.py"; then | |
| log "WARN: backup.py started while waiting for lock, skipping this cron trigger" | |
| exit 0 | |
| fi | |
| log "========================================" | |
| log "OpenClaw Backup Starting (cron-triggered)" | |
| log "========================================" | |
| # Load environment | |
| load_env | |
| # 检查恢复成功标记:只有确认恢复成功后,才允许备份 | |
| # 持久化到 /root/.openclaw/(不是 /tmp/,因为 tmpfs 重启后会丢失) | |
| if [[ ! -f "/root/dataset-restore-completed" ]]; then | |
| log "WARN: Restore not yet completed, backup blocked" | |
| log "WARN: Missing flag: /root/dataset-restore-completed" | |
| log "WARN: Backup is only allowed after a successful restore" | |
| exit 0 | |
| fi | |
| # Check if backup is enabled | |
| if ! is_true "${OPENCLAW_BACKUP_ENABLED:-false}"; then | |
| log "INFO: Backup is disabled (OPENCLAW_BACKUP_ENABLED=false), skipping" | |
| exit 0 | |
| fi | |
| # Pre-backup health check | |
| if is_true "${HEALTH_CHECK_BEFORE:-false}"; then | |
| if ! run_health_check; then | |
| log "ERROR: Pre-backup health check failed, aborting backup" | |
| exit 1 | |
| fi | |
| fi | |
| # Run backup | |
| if run_backup_with_retry; then | |
| log "INFO: Backup process completed successfully" | |
| # Post-backup check | |
| post_backup_check | |
| log "========================================" | |
| log "OpenClaw Backup Finished Successfully" | |
| log "========================================" | |
| exit 0 | |
| else | |
| log "ERROR: Backup process failed" | |
| # Run emergency repair on complete failure | |
| log "INFO: Running emergency repair..." | |
| /usr/local/bin/openclaw-backup-health.sh --emergency >> "$BACKUP_LOG_FILE" 2>&1 || true | |
| log "========================================" | |
| log "OpenClaw Backup Failed" | |
| log "========================================" | |
| exit 1 | |
| fi | |
| } | |
| # Run main | |
| main "$@" | |