File size: 2,529 Bytes
f6ab0dd
 
 
 
 
 
 
 
 
 
 
 
 
420c7cb
 
 
 
 
 
 
f6ab0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420c7cb
f6ab0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
set -e

echo "[Restore] Data restoration script started."

# --- 变量与暂存区定义 ---
LIVE_DATA_DIR="/app/server/storage"
LIVE_DB_FILE="/app/server/anythingllm.db"
STAGE_DIR="/tmp/restore_stage"
RESTORE_SOURCE="" # 将用于标记最终从哪里恢复

# --- 步骤 1: 强制清理线上旧数据和暂存区 ---
echo "[Restore] Forcing a clean slate..."
rm -rf "${LIVE_DATA_DIR}" "${LIVE_DB_FILE}" "${STAGE_DIR}" || {
    echo "[Warning] Failed to clean some directories, continuing anyway..."
}
mkdir -p "${STAGE_DIR}" || {
    echo "[Error] Failed to create staging directory"
    exit 1
}

# --- 步骤 2: 尝试从 WebDAV 恢复到暂存区 ---
if [ -n "${WEBDAV_URL}" ]; then
    echo "[Restore] Attempting to restore from WebDAV to staging area..."
    if rclone copy "webdav:anythingllm_backup/" "${STAGE_DIR}/" --max-depth 20; then
        # 检查关键文件是否存在,验证恢复是否有效
        if [ -f "${STAGE_DIR}/anythingllm.db" ]; then
            echo "[Restore] WebDAV restore to staging area successful."
            RESTORE_SOURCE="${STAGE_DIR}"
        else
            echo "[Restore] WebDAV backup seems incomplete. Ignoring."
        fi
    else
        echo "[Restore] WebDAV restore failed."
    fi
fi

# --- 步骤 3: 如果 WebDAV 未成功,尝试从 GitHub 恢复 ---
if [ -z "${RESTORE_SOURCE}" ] && [ -n "${GITHUB_REPO}" ]; then
    echo "[Restore] Attempting to restore from GitHub to staging area..."
    if git clone https://${GITHUB_TOKEN}@github.com/${GITHUB_REPO} "${STAGE_DIR}"; then
        if [ -f "${STAGE_DIR}/anythingllm.db" ]; then
            echo "[Restore] GitHub restore to staging area successful."
            RESTORE_SOURCE="${STAGE_DIR}"
        else
            echo "[Restore] GitHub backup seems incomplete. Ignoring."
        fi
    else
        echo "[Restore] GitHub restore failed."
    fi
fi

# --- 步骤 4: 从选定的恢复源部署到线上位置 ---
if [ -n "${RESTORE_SOURCE}" ]; then
    echo "[Restore] Deploying data from ${RESTORE_SOURCE} to live location..."
    # 确保线上目录存在
    mkdir -p "${LIVE_DATA_DIR}"
    # 使用 rsync 安全地移动文件
    rsync -a "${RESTORE_SOURCE}/storage/" "${LIVE_DATA_DIR}/"
    mv "${RESTORE_SOURCE}/anythingllm.db" "${LIVE_DB_FILE}"
    echo "[Restore] Deployment successful."
else
    echo "[Restore] No valid remote backups found to restore from. Starting fresh."
fi

# --- 步骤 5: 清理暂存区 ---
echo "[Restore] Cleaning up staging area..."
rm -rf "${STAGE_DIR}"