File size: 2,306 Bytes
888bb3e
0a2a142
66a7975
0dcac5e
0a2a142
 
 
 
 
 
66a7975
0a2a142
 
 
 
0dcac5e
0a2a142
0dcac5e
f736622
0dcac5e
f736622
a618aeb
0dcac5e
0a2a142
 
 
 
 
 
0dcac5e
0a2a142
 
 
0dcac5e
0a2a142
0dcac5e
 
0a2a142
 
0dcac5e
0a2a142
 
 
0dcac5e
0a2a142
 
0dcac5e
0a2a142
0dcac5e
0a2a142
 
 
 
0dcac5e
 
0a2a142
0dcac5e
 
0fd21aa
 
0a2a142
 
daadc3f
0a2a142
 
0dcac5e
0a2a142
 
0dcac5e
0a2a142
 
0fd21aa
0a2a142
 
0dcac5e
 
0a2a142
 
 
0dcac5e
0a2a142
0dcac5e
 
888bb3e
 
 
0a2a142
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
set -e  # 任何命令失败立即退出

# 检查WebDAV配置
check_webdav() {
    if [ -z "${WEBDAV_URL}" ] || [ -z "${WEBDAV_USERNAME}" ] || [ -z "${WEBDAV_PASSWORD}" ]; then
        echo "WebDAV配置缺失,跳过备份操作"
        exit 0
    fi
}

# 恢复备份函数
restore() {
    echo "=== 开始恢复备份 ==="
    check_webdav
    
    # Python恢复脚本
    python3 <<EOF
import os
import sys
from webdav3.client import Client

try:
    # 初始化客户端
    client = Client({
        'webdav_hostname': os.getenv('WEBDAV_URL'),
        'webdav_login': os.getenv('WEBDAV_USERNAME'),
        'webdav_password': os.getenv('WEBDAV_PASSWORD')
    })
    
    # 查找最新备份
    backups = [f for f in client.list() 
              if f.startswith('alist_backup_') and f.endswith('.tar.gz')]
    if not backups:
        print("未找到有效备份文件")
        sys.exit(0)
    
    latest = sorted(backups)[-1]
    print(f"正在恢复备份: {latest}")
    
    # 下载并解压
    tmp_file = f"/tmp/{latest}"
    client.download_sync(latest, tmp_file)
    
    # 清空目标目录
    for d in ['/app/data', '/app/config']:
        if os.path.exists(d):
            os.system(f"rm -rf {d}/*")
    
    # 解压到根目录
    os.system(f"tar -xzf {tmp_file} -C /")
    os.remove(tmp_file)
    print("备份恢复完成!")
    
except Exception as e:
    print(f"恢复失败: {str(e)}")
    sys.exit(1)
EOF
}

# 定时同步函数
sync() {
    while true; do
        echo "$(date) 同步数据到WebDAV..."
        BACKUP_FILE="alist_backup_$(date +%Y%m%d_%H%M%S).tar.gz"
        
        # 打包数据
        tar -czf "/tmp/${BACKUP_FILE}" -C / app/data app/config
        
        # 上传
        python3 -c "
from webdav3.client import Client
client = Client({
    'webdav_hostname': '${WEBDAV_URL}',
    'webdav_login': '${WEBDAV_USERNAME}',
    'webdav_password': '${WEBDAV_PASSWORD}'
})
client.upload_sync('${BACKUP_FILE}', '/tmp/${BACKUP_FILE}')
print('上传成功')"
        
        # 清理
        rm -f "/tmp/${BACKUP_FILE}"
        sleep ${SYNC_INTERVAL}
    done
}

# 根据参数执行
case "$1" in
    restore)
        restore
        ;;
    sync)
        sync
        ;;
    *)
        echo "Usage: $0 {restore|sync}"
        exit 1
esac