HEX
Server: nginx/1.28.3
System: Linux lightweb-s1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64
User: dawonefr-98 (1071)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: /home/hasdeuac-119/has.deu.ac.kr/public/recover.sh
#!/usr/bin/env bash

set -euo pipefail

log() { printf "\n[%s] %s\n" "$(date +'%F %T')" "$*"; }

WP_ROOT="$(pwd)"

TS="$(date +%F_%H%M%S)"

# 상대경로 백업 폴더(요구사항)

BACKUP_DIR_REL="./_wp_restore_backups"

BACKUP_DIR="$BACKUP_DIR_REL"

# 코어 다운로드 임시폴더

TMP_DIR="$(mktemp -d)"

cleanup() { rm -rf "$TMP_DIR"; }

trap cleanup EXIT

# wp-cli 옵션 (플러그인/테마 로딩 차단)

WP_CLI_SAFE=(--skip-plugins --skip-themes)

log "1) Pre-check: wp-config.php 존재 확인 (현재: $WP_ROOT)"

[[ -f "$WP_ROOT/wp-config.php" ]] || { echo "ERROR: wp-config.php not found. WP 루트에서 실행하세요."; exit 1; }

log "2) Backup directory 생성: $BACKUP_DIR (상대경로)"

if ! mkdir -p "$BACKUP_DIR" 2>/dev/null; then

  BACKUP_DIR="$HOME/_wp_restore_backups"

  log "WARN: 상대경로 쓰기 권한 없음 → HOME으로 폴백: $BACKUP_DIR"

  mkdir -p "$BACKUP_DIR"

fi

log "3) wp-config.php만 백업"

cp -a "$WP_ROOT/wp-config.php" "$BACKUP_DIR/wp-config.php.backup_$TS"

log "4) plugins 디렉터리 통째로 백업 폴더로 이동"

PLUGINS_DIR="$WP_ROOT/wp-content/plugins"

MOVED_PLUGINS_DIR="$BACKUP_DIR/plugins_moved_$TS"

if [[ -d "$PLUGINS_DIR" ]]; then

  mkdir -p "$(dirname "$MOVED_PLUGINS_DIR")"

  mv "$PLUGINS_DIR" "$MOVED_PLUGINS_DIR"

  mkdir -p "$PLUGINS_DIR"   # 빈 plugins 폴더 재생성 (WP가 필요로 함)

else

  log "WARN: wp-content/plugins 가 없음. (이미 삭제되었거나 경로가 다를 수 있음)"

  mkdir -p "$PLUGINS_DIR"

fi

log "5) 이동된 플러그인 목록 확보"

PLUGIN_LIST_FILE="$BACKUP_DIR/plugins_list_$TS.txt"

if [[ -d "$MOVED_PLUGINS_DIR" ]]; then

  # 폴더명 = 플러그인 slug로 간주

  find "$MOVED_PLUGINS_DIR" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > "$PLUGIN_LIST_FILE" || true

  log "Saved plugin list: $PLUGIN_LIST_FILE"

else

  : > "$PLUGIN_LIST_FILE"

  log "WARN: moved plugins dir not found; list file created empty."

fi

log "6) 최신 WordPress 코어 다운로드"

curl -L -o "$TMP_DIR/wordpress.zip" https://wordpress.org/latest.zip

unzip -q "$TMP_DIR/wordpress.zip" -d "$TMP_DIR"

log "7) 코어만 완전 교체 (wp-content, wp-config.php 유지)"

rsync -a --delete \

  --exclude "wp-content" \

  --exclude "wp-config.php" \

  "$TMP_DIR/wordpress/" "$WP_ROOT/"

log "8) Sanity check: 필수 코어 파일 확인"

[[ -f "$WP_ROOT/wp-includes/Requests/Autoload.php" ]] || {

  echo "ERROR: Core restore failed (wp-includes/Requests/Autoload.php missing)"

  exit 1

}

log "9) wp-cli 사용 가능하면 DB 업데이트/플러그인 재설치 시도"

if command -v wp >/dev/null 2>&1; then

  wp core version "${WP_CLI_SAFE[@]}" || true

  wp core update-db "${WP_CLI_SAFE[@]}" || true

  # wp.org 플러그인 자동 재설치 시도

  # - 이동된 폴더명이 wp.org slug인 경우 성공

  # - WPML/유료/커스텀/호스팅 플러그인은 실패(그게 정상)

  INSTALL_OK="$BACKUP_DIR/plugins_install_ok_$TS.txt"

  INSTALL_FAIL="$BACKUP_DIR/plugins_install_fail_$TS.txt"

  : > "$INSTALL_OK"; : > "$INSTALL_FAIL"

  log "10) 플러그인 재설치 시도 (wp.org에 있는 것만 성공)"

  while IFS= read -r slug; do

    [[ -n "$slug" ]] || continue

    if wp plugin install "$slug" --force "${WP_CLI_SAFE[@]}" >/dev/null 2>&1; then

      echo "$slug" >> "$INSTALL_OK"

    else

      echo "$slug" >> "$INSTALL_FAIL"

    fi

  done < "$PLUGIN_LIST_FILE"

  log "Install OK list : $INSTALL_OK"

  log "Install FAIL list: $INSTALL_FAIL (유료/커스텀/호스팅 플러그인일 가능성 큼)"

  # 안전: 플러그인은 기본 비활성 상태로 둠 (원하면 이후 1개씩 활성화)

  wp plugin list "${WP_CLI_SAFE[@]}" || true

else

  log "WARN: wp-cli not found. 플러그인 자동 재설치는 건너뜀."

fi

log "11) 최종 퍼미션 정리 (권장값 755/644, wp-config 600)"

find "$WP_ROOT" -type d -exec chmod 755 {} \; 2>/dev/null || true

find "$WP_ROOT" -type f -exec chmod 644 {} \; 2>/dev/null || true

chmod 600 "$WP_ROOT/wp-config.php" 2>/dev/null || true

log "DONE ✅"

log "- Config backup: $BACKUP_DIR/wp-config.php.backup_$TS"

log "- Plugins moved: $MOVED_PLUGINS_DIR"

log "- Plugins list : $PLUGIN_LIST_FILE"

log "- Next step    : FAIL 목록은 원본 ZIP/라이선스 소스에서 수동 업로드 필요"