Explorer
/proc/126/root/tmp/create_kdata_backup.py
← Zurück ↓ Download
import sqlite3,shutil,hashlib,json,os
from pathlib import Path
from datetime import datetime,timezone
stamp=datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ'); root=Path('/home/hermes')/f'Wissenspipeline-K-DATEN-Pilot-Backup-{stamp}'; root.mkdir()
files=['/opt/struktur/youtube-research/knowledge.db','/opt/struktur/obsidian-graphiti-import/obsidian_graphiti_import.db','/opt/struktur/knowledge-pipeline-aggregator-staging/knowledge_query.py','/opt/struktur/knowledge-pipeline-aggregator-staging/app.py','/opt/struktur/graphiti/youtube_graphiti_queue.py','/opt/struktur/graphiti/service/main.py','/opt/struktur/obsidian-graphiti-import/config.json']
src=sqlite3.connect('file:/opt/struktur/youtube-research/knowledge.db?mode=ro',uri=True); dst=sqlite3.connect(root/'knowledge.db'); src.backup(dst); dst.close(); src.close()
for s in files[1:]:
 p=Path(s); d=root/'files'/s.lstrip('/'); d.parent.mkdir(parents=True,exist_ok=True)
 try: shutil.copy2(p,d)
 except Exception as e: print('copy_error',s,e)
meta={'timestamp_utc':datetime.now(timezone.utc).isoformat(),'source_db':'/opt/struktur/youtube-research/knowledge.db','source_db_size':os.path.getsize(files[0]),'backup_db_size':(root/'knowledge.db').stat().st_size,'source_wal':Path(files[0]+'-wal').exists(),'source_shm':Path(files[0]+'-shm').exists(),'original_root_untouched':True}
(root/'manifest.json').write_text(json.dumps(meta,indent=2))
h=[]
for p in root.rglob('*'):
 if p.is_file(): h.append({'path':str(p),'size':p.stat().st_size,'sha256':hashlib.sha256(p.read_bytes()).hexdigest()})
(root/'SHA256SUMS.json').write_text(json.dumps(h,indent=2)); print(json.dumps({'path':str(root),'timestamp':meta['timestamp_utc'],'files':len(h),'db_sha256':next(x['sha256'] for x in h if x['path'].endswith('/knowledge.db'))},indent=2))