forked from lioensky/VCPChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
81 lines (67 loc) · 3.21 KB
/
backup.py
File metadata and controls
81 lines (67 loc) · 3.21 KB
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
import os
import zipfile
import datetime
import sys
def create_backup():
# 获取当前时间作为备份文件名的一部分
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
backup_filename = f"VCP_Backup_{timestamp}.zip"
appdata_dir = "AppData"
if not os.path.exists(appdata_dir):
print(f"错误: 未找到 {appdata_dir} 目录。请确保在程序根目录下运行此脚本。")
input("按回车键退出...")
return
print("--- VCP 一键备份工具 ---")
print("正在准备备份...")
# 备份选项
backup_attachments = input("是否备份聊天附件 (AppData/UserData/attachments)? (体积可能很大) [y/N]: ").lower() == 'y'
backup_tts_cache = input("是否备份 TTS 缓存 (AppData/tts_cache)? (体积可能很大) [y/N]: ").lower() == 'y'
# 定义备份规则
# 1. AppData 根目录文件 (不包含子文件夹)
# 2-9. 指定子文件夹的所有文件
subdirs_to_include = [
"Notemodules",
"avatarimage",
"generated_lists",
"systemPromptPresets",
"UserData",
"Agents",
"AgentGroups",
"Translatormodules"
]
if backup_tts_cache:
subdirs_to_include.append("tts_cache")
try:
with zipfile.ZipFile(backup_filename, 'w', zipfile.ZIP_DEFLATED) as backup_zip:
# 1. 备份 AppData 根目录下的文件 (不包含子文件夹)
print("正在备份 AppData 根目录文件...")
for item in os.listdir(appdata_dir):
item_path = os.path.join(appdata_dir, item)
if os.path.isfile(item_path):
backup_zip.write(item_path, arcname=os.path.join("AppData", item))
# 2-10. 备份指定的子文件夹
for subdir in subdirs_to_include:
subdir_path = os.path.join(appdata_dir, subdir)
if not os.path.exists(subdir_path):
print(f"跳过: 目录 {subdir_path} 不存在")
continue
print(f"正在备份 {subdir}...")
for root, dirs, files in os.walk(subdir_path):
# 处理附件排除逻辑
if not backup_attachments and "attachments" in root:
# 如果不备份附件,且当前路径包含 attachments,则跳过该目录下的文件
# 注意:这里简单处理,如果 UserData 下有 attachments 文件夹则跳过
if os.path.basename(root) == "attachments" or "attachments" in root.split(os.sep):
continue
for file in files:
file_path = os.path.join(root, file)
# 计算在 zip 中的相对路径
arcname = os.path.relpath(file_path, os.path.dirname(appdata_dir))
backup_zip.write(file_path, arcname=arcname)
print(f"\n备份完成!")
print(f"备份文件已保存为: {os.path.abspath(backup_filename)}")
except Exception as e:
print(f"\n备份过程中出错: {e}")
input("\n按回车键退出...")
if __name__ == "__main__":
create_backup()