Spaces:
Paused

File size: 5,671 Bytes
11c931f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash

# WebDAV同步脚本 - 用于备份和恢复VSCode工作区数据
# 作者: Augment Agent
# 用途: 防止Hugging Face Spaces重启后数据丢失

set -e

# 配置变量
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOME_DIR="/home/appuser"
RCLONE_CONFIG_DIR="$HOME_DIR/.config/rclone"
RCLONE_CONFIG_FILE="$RCLONE_CONFIG_DIR/rclone.conf"
WEBDAV_REMOTE_NAME="webdav_backup"

# 需要同步的目录
SYNC_DIRS=(
    "$HOME_DIR/.config/code-server"
    "$HOME_DIR/.local/share/code-server/User"
    "$HOME_DIR/workspace"
    "$HOME_DIR/projects"
)

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

error() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1" >&2
}

# 检查环境变量
check_env_vars() {
    if [[ -z "$WEBDAV_URL" ]]; then
        error "WEBDAV_URL environment variable is not set"
        return 1
    fi
    
    if [[ -z "$WEBDAV_USERNAME" ]]; then
        error "WEBDAV_USERNAME environment variable is not set"
        return 1
    fi
    
    if [[ -z "$WEBDAV_PASSWORD" ]]; then
        error "WEBDAV_PASSWORD environment variable is not set"
        return 1
    fi
    
    log "Environment variables validated"
    return 0
}

# 设置rclone配置
setup_rclone_config() {
    log "Setting up rclone configuration..."
    
    # 创建rclone配置目录
    mkdir -p "$RCLONE_CONFIG_DIR"
    
    # 生成rclone配置文件
    cat > "$RCLONE_CONFIG_FILE" <<EOF
[$WEBDAV_REMOTE_NAME]
type = webdav
url = $WEBDAV_URL
vendor = other
user = $WEBDAV_USERNAME
pass = $(echo -n "$WEBDAV_PASSWORD" | rclone obscure -)
EOF
    
    chmod 600 "$RCLONE_CONFIG_FILE"
    log "rclone configuration created"
}

# 测试WebDAV连接
test_webdav_connection() {
    log "Testing WebDAV connection..."
    
    if rclone lsd "$WEBDAV_REMOTE_NAME:" >/dev/null 2>&1; then
        log "WebDAV connection successful"
        return 0
    else
        error "Failed to connect to WebDAV server"
        return 1
    fi
}

# 创建远程目录结构
create_remote_dirs() {
    log "Creating remote directory structure..."
    
    for dir in "${SYNC_DIRS[@]}"; do
        local relative_path="${dir#$HOME_DIR/}"
        local remote_path="$WEBDAV_REMOTE_NAME:vscode-backup/$relative_path"
        
        if ! rclone mkdir "$remote_path" 2>/dev/null; then
            log "Directory $remote_path already exists or created"
        fi
    done
}

# 备份到WebDAV
backup_to_webdav() {
    log "Starting backup to WebDAV..."
    
    for dir in "${SYNC_DIRS[@]}"; do
        if [[ -d "$dir" ]]; then
            local relative_path="${dir#$HOME_DIR/}"
            local remote_path="$WEBDAV_REMOTE_NAME:vscode-backup/$relative_path"
            
            log "Backing up $dir to $remote_path"
            
            # 使用rclone sync进行增量同步
            if rclone sync "$dir" "$remote_path" \
                --exclude "*.log" \
                --exclude "*.tmp" \
                --exclude "**/node_modules/**" \
                # --exclude "**/.git/**" \
                --exclude "**/logs/**" \
                --exclude "**/CachedExtensions/**" \
                --exclude "**/CachedExtensionVSIXs/**" \
                --progress \
                --transfers 3 \
                --checkers 8 \
                --retries 3 \
                --low-level-retries 10; then
                log "Successfully backed up $dir"
            else
                error "Failed to backup $dir"
            fi
        else
            log "Directory $dir does not exist, skipping"
        fi
    done
    
    log "Backup completed"
}

# 从WebDAV恢复
restore_from_webdav() {
    log "Starting restore from WebDAV..."
    
    for dir in "${SYNC_DIRS[@]}"; do
        local relative_path="${dir#$HOME_DIR/}"
        local remote_path="$WEBDAV_REMOTE_NAME:vscode-backup/$relative_path"
        
        # 检查远程目录是否存在
        if rclone lsd "$WEBDAV_REMOTE_NAME:vscode-backup" | grep -q "$(basename "$relative_path")"; then
            log "Restoring $remote_path to $dir"
            
            # 创建本地目录
            mkdir -p "$dir"
            
            # 使用rclone sync进行恢复
            if rclone sync "$remote_path" "$dir" \
                --progress \
                --transfers 3 \
                --checkers 8 \
                --retries 3 \
                --low-level-retries 10; then
                log "Successfully restored $dir"
            else
                error "Failed to restore $dir"
            fi
        else
            log "Remote directory $remote_path does not exist, skipping"
        fi
    done
    
    log "Restore completed"
}

# 主函数
main() {
    local action="${1:-backup}"
    
    # 检查是否启用WebDAV同步
    if [[ "${WEBDAV_SYNC_ENABLED:-true}" != "true" ]]; then
        log "WebDAV sync is disabled"
        exit 0
    fi
    
    log "Starting WebDAV sync script - Action: $action"
    
    # 检查环境变量
    if ! check_env_vars; then
        exit 1
    fi
    
    # 设置rclone配置
    setup_rclone_config
    
    # 测试连接
    if ! test_webdav_connection; then
        exit 1
    fi
    
    case "$action" in
        "backup")
            create_remote_dirs
            backup_to_webdav
            ;;
        "restore")
            restore_from_webdav
            ;;
        "test")
            log "WebDAV connection test completed successfully"
            ;;
        *)
            error "Unknown action: $action. Use 'backup', 'restore', or 'test'"
            exit 1
            ;;
    esac
    
    log "WebDAV sync script completed successfully"
}

# 执行主函数
main "$@"