#!/bin/bash # MarkBaseFS Snapshot Management Tool # Date: 2026-09-21 # Purpose: 快照管理工具 set -e DATABASE="${1:-/tmp/markbase_test_suite/test.sqlite}" ROOT_PATH="/tmp/markbase_test_suite/volume" SNAPSHOT_DIR="$ROOT_PATH/.snapshots" COMMAND="${2:-help}" shift 2 2>/dev/null || true # 创建快照 create_snapshot() { local name="${1:-snapshot_$(date +%Y%m%d_%H%M%S)}" echo "Creating snapshot: $name" echo "----------------------------------------" # 创建快照目录 mkdir -p "$SNAPSHOT_DIR" # 创建快照 local snapshot_path="$SNAPSHOT_DIR/$name" if [ -e "$snapshot_path" ]; then echo "❌ Snapshot already exists: $name" return 1 fi # 创建快照目录 mkdir -p "$snapshot_path" # 复制文件(排除 .snapshots 目录) for item in "$ROOT_PATH"/*; do if [ -e "$item" ] && [ "$(basename "$item")" != ".snapshots" ]; then cp -R "$item" "$snapshot_path/" fi done # 创建元数据 local meta_path="$snapshot_path.meta" local size=$(du -sb "$snapshot_path" 2>/dev/null | cut -f1 || echo 0) cat > "$meta_path" << EOF { "name": "$name", "created": "$(date -Iseconds)", "source_path": "$ROOT_PATH", "size": $size } EOF echo " ✓ Snapshot created: $snapshot_path" echo " ✓ Size: $(du -sh "$snapshot_path" 2>/dev/null | cut -f1 || echo 'unknown')" echo "" echo "✅ Snapshot created successfully" } # 列出快照 list_snapshots() { echo "Listing snapshots" echo "----------------------------------------" if [ ! -d "$SNAPSHOT_DIR" ]; then echo " No snapshots found" echo "" echo "💡 Create a snapshot first:" echo " ./snapshot_tool.sh create [name]" return 0 fi local count=0 for snapshot in "$SNAPSHOT_DIR"/*; do # 跳过 .meta 文件,只列出目录 if [ -d "$snapshot" ]; then local name=$(basename "$snapshot") local meta_file="$snapshot.meta" if [ -f "$meta_file" ]; then local created=$(grep -o '"created": "[^"]*"' "$meta_file" | cut -d'"' -f4) local size=$(du -sh "$snapshot" 2>/dev/null | cut -f1) printf " %-30s %10s %s\n" "$name" "$size" "$created" else local size=$(du -sh "$snapshot" 2>/dev/null | cut -f1) printf " %-30s %10s (no metadata)\n" "$name" "$size" fi count=$((count + 1)) fi done echo "" if [ $count -eq 0 ]; then echo " No snapshots found" else echo "✅ Found $count snapshot(s)" fi } # 删除快照 delete_snapshot() { local name="$1" if [ -z "$name" ]; then echo "❌ Missing snapshot name" echo "Usage: ./snapshot_tool.sh delete " return 1 fi echo "Deleting snapshot: $name" echo "----------------------------------------" local snapshot_path="$SNAPSHOT_DIR/$name" local meta_path="$snapshot_path.meta" if [ ! -e "$snapshot_path" ]; then echo "❌ Snapshot not found: $name" return 1 fi # 删除快照 rm -rf "$snapshot_path" rm -f "$meta_path" echo " ✓ Snapshot deleted: $name" echo "" echo "✅ Delete successful" } # 恢复快照 restore_snapshot() { local name="$1" if [ -z "$name" ]; then echo "❌ Missing snapshot name" echo "Usage: ./snapshot_tool.sh restore " return 1 fi echo "Restoring snapshot: $name" echo "----------------------------------------" local snapshot_path="$SNAPSHOT_DIR/$name" if [ ! -d "$snapshot_path" ]; then echo "❌ Snapshot not found: $name" return 1 fi # 备份当前状态 local backup_name="pre_restore_$(date +%Y%m%d_%H%M%S)" echo "Creating backup: $backup_name" # 创建备份快照 mkdir -p "$SNAPSHOT_DIR/$backup_name" for item in "$ROOT_PATH"/*; do if [ -e "$item" ] && [ "$(basename "$item")" != ".snapshots" ]; then cp -R "$item" "$SNAPSHOT_DIR/$backup_name/" fi done # 创建备份元数据 cat > "$SNAPSHOT_DIR/$backup_name.meta" << EOF { "name": "$backup_name", "created": "$(date -Iseconds)", "source_path": "$ROOT_PATH", "type": "pre_restore_backup" } EOF # 清空当前目录(保留 .snapshots) echo "Clearing current directory..." for item in "$ROOT_PATH"/*; do if [ -e "$item" ] && [ "$(basename "$item")" != ".snapshots" ]; then rm -rf "$item" fi done # 恢复快照 echo "Restoring files..." for item in "$snapshot_path"/*; do if [ -e "$item" ]; then cp -R "$item" "$ROOT_PATH/" fi done echo " ✓ Snapshot restored: $name" echo " ✓ Backup created: $backup_name" echo "" echo "✅ Restore successful" } # 显示快照信息 show_info() { local name="$1" if [ -z "$name" ]; then echo "❌ Missing snapshot name" echo "Usage: ./snapshot_tool.sh info " return 1 fi echo "Snapshot Information: $name" echo "----------------------------------------" local snapshot_path="$SNAPSHOT_DIR/$name" local meta_path="$snapshot_path.meta" if [ ! -d "$snapshot_path" ]; then echo "❌ Snapshot not found: $name" return 1 fi # 显示元数据 if [ -f "$meta_path" ]; then echo "Metadata:" cat "$meta_path" | jq . 2>/dev/null || cat "$meta_path" echo "" fi # 统计信息 local file_count=$(find "$snapshot_path" -type f 2>/dev/null | wc -l) local dir_count=$(find "$snapshot_path" -type d 2>/dev/null | wc -l) local total_size=$(du -sh "$snapshot_path" 2>/dev/null | cut -f1) echo "Statistics:" echo " Files: $file_count" echo " Directories: $dir_count" echo " Total size: $total_size" echo "" echo "✅ Information displayed" } # 测试快照功能 test_snapshots() { echo "Testing Snapshot Functionality" echo "========================================" echo "" # 测试 1: 创建快照 echo "Test 1: Create snapshot" create_snapshot "test_snapshot_1" echo "" # 测试 2: 列出快照 echo "Test 2: List snapshots" list_snapshots echo "" # 测试 3: 显示信息 echo "Test 3: Show snapshot info" show_info "test_snapshot_1" echo "" # 测试 4: 删除快照 echo "Test 4: Delete snapshot" delete_snapshot "test_snapshot_1" echo "" # 最终验证 echo "Test 5: Final verification" list_snapshots echo "" echo "✅ All tests passed" } # 显示帮助 show_help() { echo "MarkBaseFS Snapshot Management Tool" echo "" echo "USAGE:" echo " ./snapshot_tool.sh [arguments]" echo "" echo "COMMANDS:" echo " create [name] Create a new snapshot" echo " list List all snapshots" echo " delete Delete a snapshot" echo " restore Restore from a snapshot" echo " info Show snapshot information" echo " test Run snapshot tests" echo " help Show this help" echo "" echo "EXAMPLES:" echo " # Create snapshot with auto-generated name" echo " ./snapshot_tool.sh /path/to/db.sqlite create" echo "" echo " # Create named snapshot" echo " ./snapshot_tool.sh /path/to/db.sqlite create my_snapshot" echo "" echo " # List snapshots" echo " ./snapshot_tool.sh /path/to/db.sqlite list" echo "" echo " # Restore from snapshot" echo " ./snapshot_tool.sh /path/to/db.sqlite restore my_snapshot" echo "" echo " # Delete snapshot" echo " ./snapshot_tool.sh /path/to/db.sqlite delete my_snapshot" } # 主程序 echo "========================================" echo "MarkBaseFS Snapshot Tool v1.0" echo "========================================" echo "" echo "Database: $DATABASE" echo "Root Path: $ROOT_PATH" echo "Snapshot Dir: $SNAPSHOT_DIR" echo "" case "$COMMAND" in create) create_snapshot "$1" ;; list|ls) list_snapshots ;; delete|rm) delete_snapshot "$1" ;; restore) restore_snapshot "$1" ;; info) show_info "$1" ;; test) test_snapshots ;; help|--help|-h) show_help ;; *) echo "❌ Unknown command: $COMMAND" echo "" show_help ;; esac