Files
worklog/backup_db.py
T
azzz 5c4b8d84ed WorkLog 全量合并:密码登录体系、每日自动备份、发信日志、活动邮件通知一键参加
- 认证:注册设密码(双次确认,>=10 位)、密码/验证码双模式登录、邮箱验证码找回重置密码
- 备份:服务内置每天 23:00 自动备份到 data/backups/(默认保留 14 份),backup_db.py 支持手动/crontab
- 发信日志:logs/mail-YYYYMM.log 记录每次发信成败与失败原因(535 认证失败/断连细分),按月滚动保留 6 个月
- 通知:发起活动群发邮件(参与不发),邮件内一键参加(一次性令牌、7 天有效、私密活动不群发)
- 前端:报错细分(字段/类型/状态码全中文)、验证码按钮 60 秒防连点
- 部署:Dockerfile + entrypoint(seed 机制)、限流/唯一索引/令牌摘要等安全加固
- 工具:repair_db/clear_db 的 SQL 全部常量化,无动态拼接
- 测试:43 项全覆盖(注册/登录/活动/通知/备份/日志/安全)
2026-10-29 19:00:48 +08:00

62 lines
2.0 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
"""WorkLog 数据库手动备份脚本(Windows / Linux 通用)。
用法:
python backup_db.py 按配置备份一次(默认保留最近 14 份)
python backup_db.py --keep 30 本次保留最近 30 份
python backup_db.py --db PATH 指定数据库文件(默认 WORKLOG_DB / config.toml)
服务器上不想依赖服务内定时器时,可用系统 crontab(和本脚本是同一个备份逻辑):
0 23 * * * cd /opt/mcsmanager/daemon/syz-java && python3 backup_db.py
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
PROJECT_DIR = Path(__file__).resolve().parent
def resolve_db_path(explicit: str | None) -> Path:
"""与 server/main.py 一致:--db > WORKLOG_DB > config.toml > ./data/worklog.db"""
raw = explicit or os.getenv("WORKLOG_DB")
if not raw:
raw = "./data/worklog.db"
config_path = Path(os.getenv("WORKLOG_CONFIG", str(PROJECT_DIR / "config.toml")))
try:
import tomllib
with open(config_path, "rb") as fh:
cfg = tomllib.load(fh)
raw = cfg.get("database", {}).get("path", raw)
except Exception:
pass
path = Path(raw)
return path if path.is_absolute() else PROJECT_DIR / path
def main() -> int:
parser = argparse.ArgumentParser(description="WorkLog 数据库备份")
parser.add_argument("--db", help="数据库文件路径(默认 WORKLOG_DB / config.toml)")
parser.add_argument("--keep", type=int, default=14, help="保留最近几份备份(默认 14)")
args = parser.parse_args()
db_path = resolve_db_path(args.db)
if not db_path.exists():
print(f"[!] 数据库不存在:{db_path}")
return 1
from server.backup import _backup_once_sync
path = _backup_once_sync(db_path, db_path.parent / "backups", args.keep)
size_kb = path.stat().st_size / 1024
print(f"已备份:{path} ({size_kb:.0f} KB)")
return 0
if __name__ == "__main__":
sys.exit(main())