tianzijian commited on
Commit
92cdf4f
·
verified ·
1 Parent(s): 8a9dd29

Upload data_sync.py

Browse files
Files changed (1) hide show
  1. src/data_sync.py +60 -7
src/data_sync.py CHANGED
@@ -51,6 +51,8 @@ class DatasetManager:
51
  'logs': self.hermes_home / 'logs',
52
  'cron': self.hermes_home / 'cron',
53
  'webui_token': Path('/data/.hermes-web-ui') / '.token',
 
 
54
  }
55
 
56
  def validate(self) -> bool:
@@ -83,6 +85,8 @@ class DatasetManager:
83
  (self.temp_dir / 'logs').mkdir()
84
  (self.temp_dir / 'cron').mkdir()
85
  (self.temp_dir / 'webui').mkdir()
 
 
86
 
87
  # 复制文件
88
  try:
@@ -126,11 +130,37 @@ class DatasetManager:
126
  if self.path_mapping['cron'].exists():
127
  shutil.copytree(self.path_mapping['cron'], self.temp_dir / 'cron', dirs_exist_ok=True)
128
 
 
 
 
 
 
 
 
 
129
  # WebUI 认证 Token
130
  if self.path_mapping['webui_token'].exists():
131
  (self.temp_dir / 'webui').mkdir(exist_ok=True)
132
  shutil.copy2(self.path_mapping['webui_token'], self.temp_dir / 'webui' / '.token')
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  # 添加元数据
135
  metadata = {
136
  'timestamp': datetime.now().isoformat(),
@@ -209,9 +239,28 @@ class DatasetManager:
209
  # 确保目标目录存在
210
  self.hermes_home.mkdir(parents=True, exist_ok=True)
211
 
212
- # 恢复各个部分(跳过 config.yaml,由 entrypoint.sh 重新生成)
 
 
 
 
 
213
  skip_restore = os.environ.get('SKIP_CONFIG_RESTORE', 'true').lower() in ('true', '1', 'yes')
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  restore_mapping = {
216
  'config/.env': self.path_mapping['env'],
217
  'config/auth.json': self.path_mapping['auth'],
@@ -223,17 +272,16 @@ class DatasetManager:
223
  'logs': self.path_mapping['logs'],
224
  'cron': self.path_mapping['cron'],
225
  'webui/.token': self.path_mapping['webui_token'],
 
 
226
  }
227
 
228
- # config.yaml 恢复策略:
229
- # - SKIP_CONFIG_RESTORE=true(默认):不直接覆盖 config.yaml(由 entrypoint.sh 重新生成),
230
- # 但恢复到 config.yaml.restored 供 entrypoint.sh 合并用户配置(platforms 等区块)
231
- # - SKIP_CONFIG_RESTORE=false:直接覆盖 config.yaml
232
  if not skip_restore:
233
  restore_mapping['config/config.yaml'] = self.path_mapping['config']
234
  else:
235
- logger.info("Skipping config.yaml direct restore (will be regenerated by entrypoint.sh)")
236
- # 恢复到 .restored 文件,供 entrypoint.sh 合并用户修改的配置区块
237
  restored_path = self.hermes_home / 'config.yaml.restored'
238
  src = download_dir / 'config' / 'config.yaml'
239
  if src.exists():
@@ -241,6 +289,11 @@ class DatasetManager:
241
  logger.info("Restored config.yaml to config.yaml.restored for merge")
242
 
243
  for src_rel, dst in restore_mapping.items():
 
 
 
 
 
244
  src = download_dir / src_rel
245
  if src.exists():
246
  try:
 
51
  'logs': self.hermes_home / 'logs',
52
  'cron': self.hermes_home / 'cron',
53
  'webui_token': Path('/data/.hermes-web-ui') / '.token',
54
+ 'image_cache': self.hermes_home / 'image_cache',
55
+ 'baoyu_skills': Path('/home/appuser/.baoyu-skills'),
56
  }
57
 
58
  def validate(self) -> bool:
 
85
  (self.temp_dir / 'logs').mkdir()
86
  (self.temp_dir / 'cron').mkdir()
87
  (self.temp_dir / 'webui').mkdir()
88
+ (self.temp_dir / 'image_cache').mkdir()
89
+ (self.temp_dir / 'baoyu_skills').mkdir()
90
 
91
  # 复制文件
92
  try:
 
130
  if self.path_mapping['cron'].exists():
131
  shutil.copytree(self.path_mapping['cron'], self.temp_dir / 'cron', dirs_exist_ok=True)
132
 
133
+ # Image Cache
134
+ if self.path_mapping['image_cache'].exists():
135
+ shutil.copytree(self.path_mapping['image_cache'], self.temp_dir / 'image_cache', dirs_exist_ok=True)
136
+
137
+ # baoyu-skills 用户配置 (EXTEND.md 等)
138
+ if self.path_mapping['baoyu_skills'].exists():
139
+ shutil.copytree(self.path_mapping['baoyu_skills'], self.temp_dir / 'baoyu_skills', dirs_exist_ok=True)
140
+
141
  # WebUI 认证 Token
142
  if self.path_mapping['webui_token'].exists():
143
  (self.temp_dir / 'webui').mkdir(exist_ok=True)
144
  shutil.copy2(self.path_mapping['webui_token'], self.temp_dir / 'webui' / '.token')
145
 
146
+ # 修复临时目录权限(源文件可能被设为只读,如 baoyu-imagine scripts 的 555)
147
+ # upload_folder 需要能正常读取所有文件
148
+ logger.info("Fixing permissions in temp backup dir...")
149
+ import stat
150
+ for root, dirs, files in os.walk(self.temp_dir):
151
+ for d in dirs:
152
+ dir_path = os.path.join(root, d)
153
+ try:
154
+ os.chmod(dir_path, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
155
+ except Exception:
156
+ pass
157
+ for f in files:
158
+ file_path = os.path.join(root, f)
159
+ try:
160
+ os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
161
+ except Exception:
162
+ pass
163
+
164
  # 添加元数据
165
  metadata = {
166
  'timestamp': datetime.now().isoformat(),
 
239
  # 确保目标目录存在
240
  self.hermes_home.mkdir(parents=True, exist_ok=True)
241
 
242
+ # 恢复策略控制:
243
+ # SKIP_CONFIG_RESTORE=true(默认):跳过由 entrypoint.sh 动态生成的配置,防止旧备份覆盖新配置
244
+ # 跳过项:config.yaml、baoyu_skills/
245
+ # 恢复项:skills/(Skills Hub 安装的技能,entrypoint.sh 不会重新安装 cover/illustrator 等)
246
+ # 保留项:memories、sessions、state.db、.env、auth.json、logs、cron、webui/.token、SOUL.md(用户数据)
247
+ # SKIP_CONFIG_RESTORE=false:恢复所有备份(配置稳定后使用)
248
  skip_restore = os.environ.get('SKIP_CONFIG_RESTORE', 'true').lower() in ('true', '1', 'yes')
249
 
250
+ # 定义跳过恢复的路径(entrypoint.sh 会重新生成这些配置)
251
+ # NOTE: skills 目录不跳过!它包含 Skills Hub 安装的技能(cover-image, article-illustrator 等),
252
+ # entrypoint.sh 只重新安装 baoyu-imagine,不会安装其他技能。
253
+ # 恢复后 entrypoint.sh 会覆盖 baoyu-imagine 确保其最新(第652-654行 cp -r)。
254
+ if skip_restore:
255
+ skip_paths = {
256
+ 'config/config.yaml', # 模型/供应商配置由 entrypoint.sh 根据环境变量生成
257
+ 'baoyu_skills', # baoyu-skills EXTEND.md 由 entrypoint.sh 重新生成
258
+ }
259
+ logger.info(f"SKIP_CONFIG_RESTORE=true, skipping: {', '.join(skip_paths)}")
260
+ else:
261
+ skip_paths = set()
262
+ logger.info("SKIP_CONFIG_RESTORE=false, restoring all backed-up configurations")
263
+
264
  restore_mapping = {
265
  'config/.env': self.path_mapping['env'],
266
  'config/auth.json': self.path_mapping['auth'],
 
272
  'logs': self.path_mapping['logs'],
273
  'cron': self.path_mapping['cron'],
274
  'webui/.token': self.path_mapping['webui_token'],
275
+ 'image_cache': self.path_mapping['image_cache'],
276
+ 'baoyu_skills': self.path_mapping['baoyu_skills'],
277
  }
278
 
279
+ # config.yaml 特殊处理:
280
+ # 即使不在 skip_paths 中,如果 skip_restore=true,也不直接覆盖,而是保存到 .restored 供合并
 
 
281
  if not skip_restore:
282
  restore_mapping['config/config.yaml'] = self.path_mapping['config']
283
  else:
284
+ # 恢复到 .restored 文件,供 entrypoint.sh 合并用户修改的配置区块(如 channels、display 等)
 
285
  restored_path = self.hermes_home / 'config.yaml.restored'
286
  src = download_dir / 'config' / 'config.yaml'
287
  if src.exists():
 
289
  logger.info("Restored config.yaml to config.yaml.restored for merge")
290
 
291
  for src_rel, dst in restore_mapping.items():
292
+ # 检查是否在跳过列表中
293
+ if src_rel in skip_paths:
294
+ logger.info(f"Skipping restore of {src_rel} (will be regenerated by entrypoint.sh)")
295
+ continue
296
+
297
  src = download_dir / src_rel
298
  if src.exists():
299
  try: