Qilan2 commited on
Commit
dcae0c8
·
verified ·
1 Parent(s): df08812

Update Card_Hub.py

Browse files
Files changed (1) hide show
  1. Card_Hub.py +55 -8
Card_Hub.py CHANGED
@@ -444,22 +444,33 @@ def prepare_database_sync():
444
 
445
  def sync_database():
446
  """同步数据库"""
447
- db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite" # 移到函数开头
448
 
449
  try:
450
  # 1. 准备同步
451
  prepare_database_sync()
452
 
453
- # 2. 执行 rsync
454
- rsync_cmd = f"rsync -avz --timeout=600 --chmod=666 -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/"
455
  subprocess.run(rsync_cmd, shell=True, check=True)
456
 
457
- # 3. 验证数据库完整性
 
 
 
 
458
  try:
 
 
 
 
 
 
459
  result = subprocess.run(f"sqlite3 {db_path} 'PRAGMA integrity_check;'",
460
  shell=True,
461
  capture_output=True,
462
  text=True)
 
463
  if "ok" not in result.stdout.lower():
464
  print("数据库完整性检查失败,尝试恢复备份...")
465
  # 恢复最新的备份
@@ -467,6 +478,9 @@ def sync_database():
467
  if backups:
468
  latest_backup = max(backups, key=os.path.getctime)
469
  subprocess.run(f"cp {latest_backup} {db_path}", shell=True)
 
 
 
470
  except Exception as e:
471
  print(f"验证数据库完整性时出错: {e}")
472
 
@@ -477,14 +491,36 @@ def sync_database():
477
  if backups:
478
  latest_backup = max(backups, key=os.path.getctime)
479
  subprocess.run(f"cp {latest_backup} {db_path}", shell=True)
 
 
 
480
  finally:
481
- # 4. 重启服务
482
  try:
483
  os.chdir('/Card_Hub/RoleCardSite')
484
  # 修复权限问题
485
  subprocess.run('chmod +x ./start.sh', shell=True)
486
- subprocess.run('chmod +x ./server/node_modules/.bin/nodemon', shell=True)
487
- subprocess.run('chmod +x ./client/node_modules/.bin/vite', shell=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
  subprocess.run('sh ./start.sh', shell=True)
489
  except Exception as e:
490
  print(f"重启服务时出错: {e}")
@@ -532,7 +568,12 @@ def Run_SillyTavern():
532
  # 确保数据库权限正确
533
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
534
  subprocess.run(f"chmod 666 {db_path}", shell=True)
535
- subprocess.run(f"chmod 755 {os.path.dirname(db_path)}", shell=True)
 
 
 
 
 
536
 
537
  # 修复启动脚本和依赖的权限
538
  subprocess.run('chmod +x ./start.sh', shell=True)
@@ -546,6 +587,7 @@ def Run_SillyTavern():
546
  esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
547
  if os.path.exists(esbuild_path):
548
  subprocess.run(f'chmod +x {esbuild_path}', shell=True)
 
549
 
550
  # 修复服务器权限
551
  server_path = './server'
@@ -589,14 +631,19 @@ def install_dependencies():
589
  esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
590
  if os.path.exists(esbuild_path):
591
  subprocess.run(f'chmod +x {esbuild_path}', shell=True)
 
592
 
593
  # 修复其他可执行文件权限
594
  subprocess.run(f'chmod +x {client_path}/node_modules/.bin/*', shell=True)
595
  subprocess.run(f'chmod +x {server_path}/node_modules/.bin/*', shell=True)
 
 
596
 
597
  # 修复项目文件权限
598
  subprocess.run(f'chmod -R 755 {client_path}', shell=True)
599
  subprocess.run(f'chmod -R 755 {server_path}', shell=True)
 
 
600
 
601
  print("依赖安装和权限修复完成")
602
  except Exception as e:
 
444
 
445
  def sync_database():
446
  """同步数据库"""
447
+ db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
448
 
449
  try:
450
  # 1. 准备同步
451
  prepare_database_sync()
452
 
453
+ # 2. 执行 rsync,添加更多选项确保文件权限正确
454
+ rsync_cmd = f"rsync -avz --timeout=600 --chmod=666 --chown=root:root -e 'ssh -p {ssh_port} -o StrictHostKeyChecking=no' root@{ssh_url}:{data_folder}/ {data_folder}/"
455
  subprocess.run(rsync_cmd, shell=True, check=True)
456
 
457
+ # 3. 确保数据库文件权限正确
458
+ subprocess.run(f"chmod 666 {db_path}", shell=True)
459
+ subprocess.run(f"chown root:root {db_path}", shell=True)
460
+
461
+ # 4. 验证数据库完整性
462
  try:
463
+ # 先尝试修复数据库
464
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA integrity_check;'", shell=True, check=True)
465
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA foreign_key_check;'", shell=True, check=True)
466
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA optimize;'", shell=True, check=True)
467
+
468
+ # 再次检查完整性
469
  result = subprocess.run(f"sqlite3 {db_path} 'PRAGMA integrity_check;'",
470
  shell=True,
471
  capture_output=True,
472
  text=True)
473
+
474
  if "ok" not in result.stdout.lower():
475
  print("数据库完整性检查失败,尝试恢复备份...")
476
  # 恢复最新的备份
 
478
  if backups:
479
  latest_backup = max(backups, key=os.path.getctime)
480
  subprocess.run(f"cp {latest_backup} {db_path}", shell=True)
481
+ # 修复权限
482
+ subprocess.run(f"chmod 666 {db_path}", shell=True)
483
+ subprocess.run(f"chown root:root {db_path}", shell=True)
484
  except Exception as e:
485
  print(f"验证数据库完整性时出错: {e}")
486
 
 
491
  if backups:
492
  latest_backup = max(backups, key=os.path.getctime)
493
  subprocess.run(f"cp {latest_backup} {db_path}", shell=True)
494
+ # 修复权限
495
+ subprocess.run(f"chmod 666 {db_path}", shell=True)
496
+ subprocess.run(f"chown root:root {db_path}", shell=True)
497
  finally:
498
+ # 5. 重启服务
499
  try:
500
  os.chdir('/Card_Hub/RoleCardSite')
501
  # 修复权限问题
502
  subprocess.run('chmod +x ./start.sh', shell=True)
503
+
504
+ # 修复客户端权限
505
+ client_path = './client'
506
+ subprocess.run(f'chmod -R 755 {client_path}/node_modules', shell=True)
507
+ subprocess.run(f'chmod +x {client_path}/node_modules/.bin/*', shell=True)
508
+
509
+ # 特别修复 esbuild 权限
510
+ esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
511
+ if os.path.exists(esbuild_path):
512
+ subprocess.run(f'chmod +x {esbuild_path}', shell=True)
513
+ subprocess.run(f'chown root:root {esbuild_path}', shell=True)
514
+
515
+ # 修复服务器权限
516
+ server_path = './server'
517
+ subprocess.run(f'chmod -R 755 {server_path}/node_modules', shell=True)
518
+ subprocess.run(f'chmod +x {server_path}/node_modules/.bin/*', shell=True)
519
+
520
+ # 修复项目文件权限
521
+ subprocess.run('chmod -R 755 .', shell=True)
522
+
523
+ # 启动服务
524
  subprocess.run('sh ./start.sh', shell=True)
525
  except Exception as e:
526
  print(f"重启服务时出错: {e}")
 
568
  # 确保数据库权限正确
569
  db_path = "/Card_Hub/RoleCardSite/server/src/data/database.sqlite"
570
  subprocess.run(f"chmod 666 {db_path}", shell=True)
571
+ subprocess.run(f"chown root:root {db_path}", shell=True)
572
+
573
+ # 修复数据库
574
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA integrity_check;'", shell=True)
575
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA foreign_key_check;'", shell=True)
576
+ subprocess.run(f"sqlite3 {db_path} 'PRAGMA optimize;'", shell=True)
577
 
578
  # 修复启动脚本和依赖的权限
579
  subprocess.run('chmod +x ./start.sh', shell=True)
 
587
  esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
588
  if os.path.exists(esbuild_path):
589
  subprocess.run(f'chmod +x {esbuild_path}', shell=True)
590
+ subprocess.run(f'chown root:root {esbuild_path}', shell=True)
591
 
592
  # 修复服务器权限
593
  server_path = './server'
 
631
  esbuild_path = f'{client_path}/node_modules/@esbuild/linux-x64/bin/esbuild'
632
  if os.path.exists(esbuild_path):
633
  subprocess.run(f'chmod +x {esbuild_path}', shell=True)
634
+ subprocess.run(f'chown root:root {esbuild_path}', shell=True)
635
 
636
  # 修复其他可执行文件权限
637
  subprocess.run(f'chmod +x {client_path}/node_modules/.bin/*', shell=True)
638
  subprocess.run(f'chmod +x {server_path}/node_modules/.bin/*', shell=True)
639
+ subprocess.run(f'chown -R root:root {client_path}/node_modules/.bin', shell=True)
640
+ subprocess.run(f'chown -R root:root {server_path}/node_modules/.bin', shell=True)
641
 
642
  # 修复项目文件权限
643
  subprocess.run(f'chmod -R 755 {client_path}', shell=True)
644
  subprocess.run(f'chmod -R 755 {server_path}', shell=True)
645
+ subprocess.run(f'chown -R root:root {client_path}', shell=True)
646
+ subprocess.run(f'chown -R root:root {server_path}', shell=True)
647
 
648
  print("依赖安装和权限修复完成")
649
  except Exception as e: