Add PostgreSQL production backend to eliminate SQLite lock contention.
Support DATABASE_URL with connection pooling, pg_dump backups, SQLite migration script, and deploy_postgres.sh with docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+91
-24
@@ -3,7 +3,7 @@
|
||||
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
|
||||
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
|
||||
|
||||
"""SQLite 数据库自动备份:打包 futures.db 与 uploads,可在其他服务器恢复。"""
|
||||
"""数据库备份:SQLite futures.db 或 PostgreSQL pg_dump,含 uploads 与一键恢复脚本。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
@@ -12,6 +12,7 @@ import os
|
||||
import re
|
||||
import shutil
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -21,7 +22,7 @@ from pathlib import Path
|
||||
from typing import Callable, Optional
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from db_conn import DB_PATH
|
||||
from db_conn import DB_PATH, db_backend
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -44,9 +45,10 @@ RESTORE_MD = """# qihuo 备份恢复说明
|
||||
|
||||
| 文件/目录 | 说明 |
|
||||
|-----------|------|
|
||||
| `futures.db` | SQLite 主库(账号、交易记录、设置等) |
|
||||
| `futures.db` | SQLite 主库(仅 SQLite 模式备份) |
|
||||
| `postgres_dump.sql` | PostgreSQL 逻辑备份(仅 PostgreSQL 模式) |
|
||||
| `uploads/` | 复盘截图与 K 线图(若备份时存在) |
|
||||
| `manifest.json` | 备份元数据 |
|
||||
| `manifest.json` | 备份元数据(含 `backend` 字段) |
|
||||
| `restore.sh` | 一键恢复脚本 |
|
||||
|
||||
## 快速恢复(推荐)
|
||||
@@ -62,30 +64,45 @@ chmod +x restore.sh
|
||||
./restore.sh
|
||||
```
|
||||
|
||||
默认恢复到 **`/root/qihuo`**。指定目录:
|
||||
默认恢复到 **`/root/qihuo`**(SQLite)或导入到 `.env` 中的 PostgreSQL(见 manifest)。
|
||||
|
||||
指定应用目录:
|
||||
|
||||
```bash
|
||||
RESTORE_DIR=/opt/qihuo ./restore.sh
|
||||
```
|
||||
|
||||
3. 在新服务器部署 qihuo 代码与 Python 环境(见 `docs/DEPLOY.md`)
|
||||
4. 若恢复到 `/opt/qihuo`,将生成的 `futures.db`、`uploads/` 放入该目录
|
||||
5. 配置 `.env`(CTP 账号、SECRET_KEY 等),**不要**直接复制旧 `.env` 到公网环境
|
||||
6. 重启服务:`pm2 restart qihuo`
|
||||
3. 在新服务器部署 qihuo 代码与 Python 环境(见 `docs/POSTGRES.md` / `docs/DEPLOY.md`)
|
||||
4. 配置 `.env`(`DATABASE_URL` 或 SQLite、`SECRET_KEY`、CTP 账号等)
|
||||
5. 重启服务:`pm2 restart qihuo`
|
||||
|
||||
## 手工恢复
|
||||
## PostgreSQL 恢复
|
||||
|
||||
若 `manifest.json` 中 `"backend": "postgres"`:
|
||||
|
||||
1. 确保目标机已安装 PostgreSQL,且 `.env` 中 `DATABASE_URL` 指向空库或待覆盖库
|
||||
2. 执行 `./restore.sh`(会调用 `psql` 导入 `postgres_dump.sql`)
|
||||
|
||||
手工导入:
|
||||
|
||||
```bash
|
||||
mkdir -p /root/qihuo/uploads
|
||||
cp futures.db /root/qihuo/futures.db
|
||||
cp -a uploads/. /root/qihuo/uploads/ # 若有 uploads 目录
|
||||
export DATABASE_URL=postgresql://qihuo:密码@127.0.0.1:5432/qihuo
|
||||
psql "$DATABASE_URL" -f postgres_dump.sql
|
||||
```
|
||||
|
||||
## SQLite 手工恢复
|
||||
|
||||
```bash
|
||||
mkdir -p /opt/qihuo/uploads
|
||||
cp futures.db /opt/qihuo/futures.db
|
||||
cp -a uploads/. /opt/qihuo/uploads/
|
||||
```
|
||||
|
||||
## 注意
|
||||
|
||||
- 恢复前请停止 qihuo 进程,避免覆盖正在使用的数据库
|
||||
- 恢复后首次启动会自动执行数据库迁移,一般无需手工改表
|
||||
- `.env` 含敏感信息,请单独安全传输,不要放入公开网盘
|
||||
- 恢复前请停止 qihuo 进程
|
||||
- `.env` 含敏感信息,请单独安全传输
|
||||
- 详见 `docs/POSTGRES.md` 与 `docs/BACKUP.md`
|
||||
"""
|
||||
|
||||
|
||||
@@ -142,12 +159,54 @@ def _backup_sqlite(src_path: str, dst_path: str) -> None:
|
||||
src.close()
|
||||
|
||||
|
||||
def _write_restore_script(dest: Path, folder_name: str) -> None:
|
||||
def _backup_postgres(dst_path: str) -> None:
|
||||
url = (os.getenv("DATABASE_URL") or "").strip()
|
||||
if not url:
|
||||
raise RuntimeError("PostgreSQL 备份需要 DATABASE_URL")
|
||||
env = os.environ.copy()
|
||||
proc = subprocess.run(
|
||||
["pg_dump", "--no-owner", "--no-acl", "-f", dst_path, url],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
check=False,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
raise RuntimeError(f"pg_dump 失败: {proc.stderr.strip() or proc.stdout.strip()}")
|
||||
|
||||
|
||||
def _write_restore_script(dest: Path, *, backend: str) -> None:
|
||||
pg_block = ""
|
||||
if backend == "postgres":
|
||||
pg_block = """
|
||||
if [ -f "$SCRIPT_DIR/postgres_dump.sql" ]; then
|
||||
if [ -z "${DATABASE_URL:-}" ]; then
|
||||
if [ -f "$RESTORE_DIR/.env" ]; then
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
source "$RESTORE_DIR/.env"
|
||||
set +a
|
||||
fi
|
||||
fi
|
||||
if [ -z "${DATABASE_URL:-}" ]; then
|
||||
echo "错误: PostgreSQL 恢复需要 DATABASE_URL(环境变量或 $RESTORE_DIR/.env)"
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v psql >/dev/null; then
|
||||
echo "错误: 未找到 psql,请先安装 PostgreSQL 客户端"
|
||||
exit 1
|
||||
fi
|
||||
echo "导入 PostgreSQL: postgres_dump.sql"
|
||||
psql "$DATABASE_URL" -f "$SCRIPT_DIR/postgres_dump.sql"
|
||||
echo "PostgreSQL 导入完成"
|
||||
fi
|
||||
"""
|
||||
script = f"""#!/bin/bash
|
||||
set -euo pipefail
|
||||
RESTORE_DIR="${{RESTORE_DIR:-{default_restore_dir()}}}"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
mkdir -p "$RESTORE_DIR/uploads"
|
||||
{pg_block}
|
||||
if [ -f "$SCRIPT_DIR/futures.db" ]; then
|
||||
cp -f "$SCRIPT_DIR/futures.db" "$RESTORE_DIR/futures.db"
|
||||
echo "已复制 futures.db -> $RESTORE_DIR/futures.db"
|
||||
@@ -158,16 +217,19 @@ if [ -d "$SCRIPT_DIR/uploads" ]; then
|
||||
fi
|
||||
echo ""
|
||||
echo "恢复完成。目标目录: $RESTORE_DIR"
|
||||
echo "下一步: 部署 qihuo 代码、配置 .env、pm2 restart qihuo"
|
||||
echo "详见 RESTORE.md 与 docs/BACKUP.md"
|
||||
echo "下一步: 确认 .env、pm2 restart qihuo"
|
||||
echo "详见 RESTORE.md 与 docs/POSTGRES.md"
|
||||
"""
|
||||
dest.write_text(script, encoding="utf-8")
|
||||
|
||||
|
||||
def create_backup(*, include_uploads: bool = True) -> tuple[str, str]:
|
||||
"""创建 tar.gz 备份,返回 (文件名, 说明)。"""
|
||||
if not os.path.isfile(DB_PATH):
|
||||
backend = db_backend()
|
||||
if backend == "sqlite" and not os.path.isfile(DB_PATH):
|
||||
raise FileNotFoundError(f"数据库不存在: {DB_PATH}")
|
||||
if backend == "postgres" and not (os.getenv("DATABASE_URL") or "").strip():
|
||||
raise RuntimeError("PostgreSQL 模式需要 DATABASE_URL")
|
||||
|
||||
with _backup_lock:
|
||||
stamp = datetime.now(TZ).strftime("%Y%m%d_%H%M%S")
|
||||
@@ -180,15 +242,19 @@ def create_backup(*, include_uploads: bool = True) -> tuple[str, str]:
|
||||
with tempfile.TemporaryDirectory(prefix="qihuo_bak_") as tmp:
|
||||
work = Path(tmp) / folder_name
|
||||
work.mkdir()
|
||||
_backup_sqlite(DB_PATH, str(work / "futures.db"))
|
||||
if backend == "postgres":
|
||||
_backup_postgres(str(work / "postgres_dump.sql"))
|
||||
else:
|
||||
_backup_sqlite(DB_PATH, str(work / "futures.db"))
|
||||
|
||||
if include_uploads and upload_src.is_dir():
|
||||
shutil.copytree(upload_src, work / "uploads", dirs_exist_ok=True)
|
||||
|
||||
manifest = {
|
||||
"app": "qihuo",
|
||||
"backend": backend,
|
||||
"created_at": datetime.now(TZ).isoformat(timespec="seconds"),
|
||||
"db_path": DB_PATH,
|
||||
"db_path": DB_PATH if backend == "sqlite" else (os.getenv("DATABASE_URL") or ""),
|
||||
"includes_uploads": include_uploads and upload_src.is_dir(),
|
||||
"default_restore_dir": default_restore_dir(),
|
||||
"files": sorted(p.name for p in work.iterdir()),
|
||||
@@ -198,13 +264,14 @@ def create_backup(*, include_uploads: bool = True) -> tuple[str, str]:
|
||||
encoding="utf-8",
|
||||
)
|
||||
(work / "RESTORE.md").write_text(RESTORE_MD, encoding="utf-8")
|
||||
_write_restore_script(work / "restore.sh", folder_name)
|
||||
_write_restore_script(work / "restore.sh", backend=backend)
|
||||
|
||||
with tarfile.open(out_path, "w:gz") as tar:
|
||||
tar.add(work, arcname=folder_name)
|
||||
|
||||
size_mb = out_path.stat().st_size / (1024 * 1024)
|
||||
return filename, f"备份已生成 {filename}({size_mb:.2f} MB)"
|
||||
label = "PostgreSQL" if backend == "postgres" else "SQLite"
|
||||
return filename, f"备份已生成 {filename}({label},{size_mb:.2f} MB)"
|
||||
|
||||
|
||||
def list_backups() -> list[dict]:
|
||||
|
||||
Reference in New Issue
Block a user