Download webdav-sync.sh from dss11/ddda: direct link, hf CLI and curl.
- Browser
- Download file 5.67 kB
-
https://huggingface.co/spaces/dss11/ddda/resolve/main/webdav-sync.sh
- Command line
-
hf download hf://spaces/dss11/ddda/webdav-sync.sh
-
curl -L -o webdav-sync.sh https://huggingface.co/spaces/dss11/ddda/resolve/main/webdav-sync.sh
5.67 kB
| # 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 "$@" | |