Files
kefu/deploy/knowledge-v2-20260916/snapshot-backup.sh
T
2026-09-21 10:34:06 +08:00

32 lines
1.8 KiB
Bash

set -euo pipefail
release=/home/ps/knowledge-v2-20260916
cd "$release"
docker exec -i im-admin-api-1 python -u - <<'PY' | tee "$release/backup-verification.json"
from pathlib import Path
import sqlite3,shutil,json,os,time
folder=Path('/data/backups/knowledge-v2-20260916')
folder.mkdir(mode=0o700,exist_ok=True)
started=time.monotonic()
with sqlite3.connect('/data/backend.db',timeout=30) as source, sqlite3.connect(folder/'backend.snapshot.db') as backup:
source.execute('BEGIN')
source.execute('SELECT COUNT(*) FROM sqlite_master').fetchone()
source.backup(backup,pages=4096,sleep=0.05)
check=backup.execute('PRAGMA quick_check').fetchone()[0]
assert check=='ok',check
tables=['archive_message','archive_conversation','archive_person','archive_source_account','desktop_accounts','model_providers','users','knowledge_item','knowledge_job','knowledge_watch']
counts={t:backup.execute('SELECT COUNT(*) FROM '+t).fetchone()[0] for t in tables}
source.close()
backup.close()
(folder/'backend.snapshot.db').replace(folder/'backend.db')
os.chmod(folder/'backend.db',0o600)
keys=[]
for key in Path('/data').glob('*.key'):
shutil.copy2(key,folder/key.name)
os.chmod(folder/key.name,0o600)
keys.append(key.name)
assert keys,'No key file backed up'
print(json.dumps({'backup':str(folder),'bytes':(folder/'backend.db').stat().st_size,'quick_check':check,'counts':counts,'key_files':keys,'seconds':round(time.monotonic()-started,1)}),flush=True)
PY
docker build --network=none --build-arg BASE_IMAGE=zyt/wecom-admin:before-knowledge-v2-20260916 -f Dockerfile.overlay -t zyt/wecom-admin:knowledge-v2-20260916 . 2>&1 | tee build.log
docker run --rm --network=none --read-only --tmpfs /tmp:size=256m,mode=1777 zyt/wecom-admin:knowledge-v2-20260916 python -X utf8 -m unittest test_knowledge test_model_gateway -q 2>&1 | tee candidate-tests.log