# -*- coding: utf-8 -*- """水利智学系统 精简备份:保留代码+主库+业务资料,排除模型/历史大备份/缓存/过程产物""" import os, shutil, json, sys, time SRC = r"F:\笔记本电脑盘\娱乐学习\水利智学系统\可运行版(解压即用,双击start.bat)" DST = r"D:\_备份\水利智学系统_精简备份_20261120" GB = 1073741824 MB = 1048576 def long(p): p = os.path.abspath(p) if p.startswith("\\\\?\\"): return p return "\\\\?\\" + p # 顶层白名单:只复制这些(其余历史修复目录/截图目录一律不带走) TOP_DIRS = ["backend", "frontend", "docs", "data", "tools", "watch", "change_logs", "deliverables", "screenshots", "materials", "voice_samples"] # 任意层级都跳过的目录名 SKIP_ANY = { ".git", "backups", "migration_backup", "browser-profile", "python", "runtime", "ollama", "ollama_models", "ai_models", "models", "tmp", "temp", "__pycache__", ".pytest_cache", ".hvigor", ".preview", "node_modules", "_pdf_cache", "pdf2_pages", "extracted_images", "office_tools", "artifacts", "artifacts_s1", "办公工具", "_quarantine_待删除_20260909", "_archive", "_temp_scripts", } # 文件名模式跳过 SKIP_SUFFIX = (".bak", ".old", ".pyc") SKIP_CONTAINS = (".bak_", ".bak-", "_backup_", "_corrupted", ".exe.bak") # 除主库外的其他 db 副本 KEEP_DB = {"shuili_exam.db", "code_index.db"} MAX_FILE = 60 * MB # 单文件上限,超过视为"大文件" copied = [] skipped_big = [] skipped_dir = [] failed = [] total = 0 def should_skip_file(name, size): low = name.lower() if name in KEEP_DB: return False if low.endswith(SKIP_SUFFIX): return True for c in SKIP_CONTAINS: if c in low: return True if low.endswith(".db") and name not in KEEP_DB: return True if size > MAX_FILE: skipped_big.append((size, name)) return True return False for top in TOP_DIRS: s = os.path.join(SRC, top) if not os.path.isdir(long(s)): continue for root, dirs, files in os.walk(long(s), onerror=lambda e: None): dirs[:] = [d for d in dirs if d not in SKIP_ANY] rel_root = os.path.relpath(root, long(SRC)) out_root = os.path.join(long(DST), rel_root) os.makedirs(out_root, exist_ok=True) for f in files: src_p = os.path.join(root, f) try: size = os.path.getsize(src_p) except Exception: failed.append(src_p) continue if should_skip_file(f, size): continue dst_p = os.path.join(out_root, f) try: shutil.copy2(src_p, dst_p) total += size copied.append((size, os.path.relpath(src_p, long(SRC)))) except Exception as e: failed.append("%s :: %s" % (src_p, e)) # 根目录散文件(脚本、文档、配置) root_files = [f for f in os.listdir(long(SRC)) if os.path.isfile(os.path.join(long(SRC), f))] for f in root_files: src_p = os.path.join(long(SRC), f) try: size = os.path.getsize(src_p) except Exception: continue if should_skip_file(f, size): continue if size > MAX_FILE: continue try: shutil.copy2(src_p, os.path.join(long(DST), f)) total += size copied.append((size, f)) except Exception as e: failed.append("%s :: %s" % (src_p, e)) # 直接列出被整体跳过的顶层历史目录 all_top = [d for d in os.listdir(long(SRC)) if os.path.isdir(os.path.join(long(SRC), d))] for d in all_top: if d not in TOP_DIRS: skipped_dir.append(d) def size_of(p): t = 0 for r, _, fs in os.walk(p, onerror=lambda e: None): for f in fs: try: t += os.path.getsize(os.path.join(r, f)) except Exception: pass return t data_out = { "source": SRC, "dest": DST, "finished": time.strftime("%Y-%m-%d %H:%M:%S"), "copied_files": len(copied), "copied_MB": round(total / MB, 1), "failed": failed[:50], "skipped_top_dirs": len(skipped_dir), "skipped_big_count": len(skipped_big), "skipped_big_MB": round(sum(s for s, _ in skipped_big) / MB, 1), "skipped_big_top": [[round(s / MB, 1), n] for s, n in sorted(skipped_big, reverse=True)[:30]], "top_dirs_kept": TOP_DIRS, } json.dump(data_out, open(r"D:\_备份\_backup_report.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1) print(json.dumps({k: v for k, v in data_out.items() if k != "skipped_big_top"}, ensure_ascii=False, indent=1)) print("DST size MB:", round(size_of(long(DST)) / MB, 1))