2 commited on
Commit ·
11c931f
1
Parent(s): 87f26d3
添加同步
Browse filesFiles changed (6)
- .gitignoreAdded+3
- Dockerfile+9-2
- README.md+51
- rclone.conf.templateAdded+27
- start.sh+50-6
- webdav-sync.shAdded+219
123WEBDAV_SETUP.mdDEPLOYMENT_CHECKLIST.mdtest-webdav.sh20 unmodified lines212223242526272816 unmodified lines45464748495051525320 unmodified lineshtop \tmux \screen \&& rm -rf /var/lib/apt/lists/*# 1. 创建应用用户并设置权限RUN useradd -m -u 1000 appuser && \mkdir -p /var/www/html /var/log/nginx /var/lib/nginx/tmp && \16 unmodified linesmkdir -p /home/appuser/.local/share/code-server && \chown -R appuser:appuser /home/appuser# 6. 复制启动脚本并设置权限COPY start.sh /start.shRUN chmod +x /start.sh# 7. 设置工作目录和用户WORKDIR /home/appuser20 unmodified lines21222324252627282930313216 unmodified lines49505152535455565758596020 unmodified lineshtop \tmux \screen \rsync \&& rm -rf /var/lib/apt/lists/*# 2. 安装 rclone (WebDAV客户端)RUN curl https://rclone.org/install.sh | bash# 1. 创建应用用户并设置权限RUN useradd -m -u 1000 appuser && \mkdir -p /var/www/html /var/log/nginx /var/lib/nginx/tmp && \16 unmodified linesmkdir -p /home/appuser/.local/share/code-server && \chown -R appuser:appuser /home/appuser# 6. 复制脚本文件并设置权限COPY start.sh /start.shCOPY webdav-sync.sh /webdav-sync.shCOPY test-webdav.sh /test-webdav.shCOPY rclone.conf.template /rclone.conf.templateRUN chmod +x /start.sh /webdav-sync.sh /test-webdav.sh# 7. 设置工作目录和用户WORKDIR /home/appuser
6 unmodified lines789106 unmodified linespinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference6 unmodified lines789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616 unmodified linespinned: false---# VSCode Server with WebDAV Sync这是一个部署在Hugging Face Spaces上的VSCode Server环境,集成了WebDAV同步备份功能,防止重启后数据丢失。## 功能特性- 🖥️ **VSCode Server** - 基于Web的VSCode开发环境- 🌐 **Nginx** - 静态文件服务- 🔗 **Cloudflare Tunnel** - 安全的外网访问- 💾 **WebDAV同步** - 自动备份和恢复工作区数据## WebDAV同步功能### 自动备份- 每5分钟自动增量同步到WebDAV服务器- 智能排除临时文件和缓存- 支持多种WebDAV服务器(Nextcloud、ownCloud、坚果云等)### 自动恢复- 容器启动时自动从WebDAV恢复数据- 保持VSCode配置、扩展设置和工作区文件## 环境变量配置在Space Settings中配置以下环境变量:| 变量名 | 描述 | 必需 ||--------|------|------|| `WEBDAV_URL` | WebDAV服务器地址 | ✅ || `WEBDAV_USERNAME` | WebDAV用户名 | ✅ || `WEBDAV_PASSWORD` | WebDAV密码 | ✅ || `WEBDAV_SYNC_ENABLED` | 启用同步(默认true) | ❌ || `VSCODE_PASSWORD` | VSCode访问密码 | ❌ || `TUNNEL_TOKEN` | Cloudflare Tunnel令牌 | ❌ |## 快速开始1. **配置WebDAV**:在Space Settings中添加WebDAV相关环境变量2. **部署Space**:保存配置后自动重新部署3. **访问VSCode**:通过Space URL访问VSCode界面4. **开始开发**:您的工作将自动备份到WebDAV服务器详细设置指南请参考:[WEBDAV_SETUP.md](./WEBDAV_SETUP.md)## 技术架构- **基础镜像**:Ubuntu 22.04- **WebDAV客户端**:rclone- **同步策略**:增量同步,错误重试- **备份目录**:VSCode配置、用户设置、工作区文件Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
123456789101112131415161718192021222324252627# rclone配置模板# 此文件将在运行时被webdav-sync.sh脚本动态生成# 支持的WebDAV服务器包括:# - Nextcloud# - ownCloud # - Box# - Yandex Disk# - 其他标准WebDAV服务器
# 配置示例:# [webdav_backup]# type = webdav# url = https://your-webdav-server.com/remote.php/dav/files/username/# vendor = nextcloud # 或 owncloud, other# user = your_username# pass = your_obscured_password
# 环境变量说明:# WEBDAV_URL - WebDAV服务器URL# WEBDAV_USERNAME - 用户名# WEBDAV_PASSWORD - 密码(明文,脚本会自动加密)# WEBDAV_SYNC_ENABLED - 是否启用同步(true/false,默认true)
# 支持的WebDAV URL格式示例:# Nextcloud: https://cloud.example.com/remote.php/dav/files/username/# ownCloud: https://cloud.example.com/remote.php/webdav/# 通用WebDAV: https://webdav.example.com/42 unmodified lines434445464748495051525310 unmodified lines6465666768697019 unmodified lines9091929394959697989910010110210310410510610742 unmodified linesecho "Starting Nginx..."nginx -c /tmp/nginx.conf -g "daemon off;" &# 2. 修复 code-server 权限问题# 确保目录存在mkdir -p $HOME/.config/code-servermkdir -p $HOME/.local/share/code-servermkdir -p $HOME/.local/share/code-server/extensions# 设置 VS Code 密码PASSWORD=${VSCODE_PASSWORD:-"your_strong_password"}10 unmodified lines--verbose \--config $HOME/.config/code-server/config.yaml \--user-data-dir $HOME/.local/share/code-server \--extensions-dir $HOME/.local/share/code-server/extensions 2>&1 | tee /tmp/code-server.log &# 等待 code-server 启动echo "Waiting for code-server to start..."19 unmodified linesecho "Starting Cloudflare Tunnel..."cloudflared tunnel run --token $TUNNEL_TOKEN 2>&1 | tee /tmp/cloudflared.log &# 4. 添加保活机制(防止容器退出)echo "All services started. Waiting and pinging services periodically..."while true; do# 每30秒检查一次服务状态echo "Service status:"echo "- Nginx: $(pgrep nginx | wc -l) processes"echo "- code-server: $(pgrep -f "code-server" | wc -l) processes"echo "- cloudflared: $(pgrep cloudflared | wc -l) processes"# 访问本地服务保持活跃curl -s http://localhost:7860 > /dev/null || echo "Nginx ping failed"curl -s http://localhost:8080 > /dev/null || echo "code-server ping failed"sleep 30done42 unmodified lines43444546474849505152535455565758596061626364656667686910 unmodified lines808182838485868719 unmodified lines10710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015142 unmodified linesecho "Starting Nginx..."nginx -c /tmp/nginx.conf -g "daemon off;" &# 2. WebDAV同步 - 启动时恢复数据echo "=== WebDAV Sync Setup ==="if [[ "${WEBDAV_SYNC_ENABLED:-true}" == "true" ]]; thenecho "WebDAV sync is enabled, attempting to restore data..."if /webdav-sync.sh restore; thenecho "Data restored from WebDAV successfully"elseecho "Failed to restore from WebDAV, continuing with fresh setup"fielseecho "WebDAV sync is disabled"fiecho "=========================="# 3. 修复 code-server 权限问题# 确保目录存在mkdir -p $HOME/.config/code-servermkdir -p $HOME/.local/share/code-servermkdir -p $HOME/.local/share/code-server/extensionsmkdir -p $HOME/workspacemkdir -p $HOME/projects# 设置 VS Code 密码PASSWORD=${VSCODE_PASSWORD:-"your_strong_password"}10 unmodified lines--verbose \--config $HOME/.config/code-server/config.yaml \--user-data-dir $HOME/.local/share/code-server \--extensions-dir $HOME/.local/share/code-server/extensions \$HOME/workspace 2>&1 | tee /tmp/code-server.log &# 等待 code-server 启动echo "Waiting for code-server to start..."19 unmodified linesecho "Starting Cloudflare Tunnel..."cloudflared tunnel run --token $TUNNEL_TOKEN 2>&1 | tee /tmp/cloudflared.log &# 4. 启动定期备份任务if [[ "${WEBDAV_SYNC_ENABLED:-true}" == "true" ]]; thenecho "Starting periodic WebDAV backup..."(# 等待5分钟后开始第一次备份sleep 300while true; doecho "=== Periodic WebDAV Backup ==="if /webdav-sync.sh backup; thenecho "Periodic backup completed successfully"elseecho "Periodic backup failed"fiecho "=============================="# 每5分钟备份一次sleep 300done) &fi# 5. 添加保活机制(防止容器退出)echo "All services started. Waiting and pinging services periodically..."LOOP_COUNT=0while true; do# 每30秒检查一次服务状态echo "Service status (Loop: $LOOP_COUNT):"echo "- Nginx: $(pgrep nginx | wc -l) processes"echo "- code-server: $(pgrep -f "code-server" | wc -l) processes"echo "- cloudflared: $(pgrep cloudflared | wc -l) processes"# 访问本地服务保持活跃curl -s http://localhost:7860 > /dev/null || echo "Nginx ping failed"curl -s http://localhost:8080 > /dev/null || echo "code-server ping failed"# 每10分钟显示一次WebDAV同步状态if [[ $((LOOP_COUNT % 20)) -eq 0 ]] && [[ "${WEBDAV_SYNC_ENABLED:-true}" == "true" ]]; thenecho "WebDAV sync status: Enabled (backup every 5 minutes)"fiLOOP_COUNT=$((LOOP_COUNT + 1))sleep 30done
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219#!/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 = webdavurl = $WEBDAV_URLvendor = otheruser = $WEBDAV_USERNAMEpass = $(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}
# 备份到WebDAVbackup_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 "$@".gitignoreAdded+3
Dockerfile+9-2
README.md+51
rclone.conf.templateAdded+27
start.sh+50-6
webdav-sync.shAdded+219