openclaw-home-pc/vector_memory/auto_backup.py
2026-03-24 04:00:48 +08:00

95 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
飞书云盘自动上传脚本
功能:将本地备份自动上传到飞书云盘指定文件夹
"""
import os
import sys
import json
import subprocess
from datetime import datetime
from pathlib import Path
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 飞书配置
FOLDER_TOKEN = "C9jPf8MVwlvWgldZ7VHcHrLTnCf" # openclaw 文件夹
VECTOR_FOLDER_NAME = "向量记忆系统"
# 导入备份模块
from vector_memory import VectorMemorySystem
from memory_backup import MemoryBackup
def upload_file_to_feishu(file_path: str, folder_token: str) -> dict:
"""上传文件到飞书云盘"""
import requests
# 这个需要飞书开放平台的应用有云空间权限
# 使用用户身份调用 API需要用户授权
print(f"📤 准备上传: {file_path}")
# 方案:返回手动上传指引
return {
"success": False,
"message": "需要飞书应用权限",
"file_path": str(file_path)
}
def do_backup_and_upload():
"""执行备份并尝试上传"""
print("=" * 60)
print("🧠 向量记忆系统 - 定时备份")
print("=" * 60)
print(f"⏰ 执行时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# 1. 获取 API Key
api_key = os.getenv("SILICONFLOW_API_KEY")
if not api_key:
print("❌ 未设置 SILICONFLOW_API_KEY")
return False
# 2. 执行本地备份
try:
vm = VectorMemorySystem(api_key=api_key)
backup = MemoryBackup(backup_dir="./backups")
result = backup.backup_all(vm)
print(f"\n✅ 本地备份完成!")
print(f" - JSON: {result['json']}")
print(f" - Markdown: {result['markdown']}")
except Exception as e:
print(f"❌ 备份失败: {e}")
return False
# 3. 尝试上传到飞书(需要权限)
print(f"\n📤 飞书云盘上传...")
# 这里可以后续添加自动上传逻辑
print(f"\n" + "=" * 60)
print("✅ 定时备份完成!")
print("=" * 60)
return True
def main():
"""主函数"""
# 直接执行备份
success = do_backup_and_upload()
if success:
print("\n🎉 备份成功完成!")
else:
print("\n❌ 备份失败,请检查日志")
sys.exit(1)
if __name__ == "__main__":
main()