Files
system-backu/backend/app/schemas/job.py
T
publ 2568c0380b feat: MySQL SSL 支持 + 任务编辑回显 + InfluxDB 备份
MySQL:
- 新增 use_ssl 选项;MariaDB 11.x 默认强制 SSL 且默认 verify cert,
  故启用 SSL 时同时加 --skip-ssl-verify-server-cert 以兼容自签名证书。

任务编辑回显:
- 后端 JobOut 新增 source_config_safe 字段,解密后敏感字段(password/token/
  secret_key/access_key)脱敏为 "***"。
- 更新端点接收 source_config;若密码字段为 "***" 则保留原值不变,
  支持"不改密码只改其它字段"的常见场景。
- 前端 JobForm 完整回显所有数据源字段,type='edit' 时去掉 disabled。

InfluxDB 备份(新增类型):
- 后端 core/backup/influxdb.py:
  - v2.x:调用 influx CLI(influxdb2-client-2.7.5 二进制直接安装)执行 influx backup
    生成目录后 tar.gz 流式打包上传。
  - v1.x:通过 HTTP API 调用 /query?db=...&q=SELECT * FROM /.*/ 拉取全量数据
    导出为 CSV + META.txt。
- Dockerfile:直接下载 influx CLI 二进制(绕开 apt 签名问题)。
- 前端 JobForm:根据 version 动态显示 v1/v2 表单字段。
- schemas/job.py:新增 InfluxDBSourceConfigV1 / V2。
- 类型字面量扩展:'mysql' | 'directory' | 'influxdb'。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-22 19:14:45 +08:00

121 lines
3.8 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.
from datetime import datetime
from typing import Literal, Optional
from pydantic import BaseModel, ConfigDict, Field, field_validator
from app.schemas.storage import StorageOut
class MySQLSourceConfig(BaseModel):
host: str = Field(..., min_length=1)
port: int = Field(3306, ge=1, le=65535)
user: str = Field(..., min_length=1)
password: str = Field("", description="可空(trust auth")
database: str = Field(..., min_length=1, description="要备份的库名")
use_ssl: bool = Field(False, description="True 启用 SSL/TLS;如服务器是自签名证书建议关闭")
extra_args: str = Field("", description="额外 mysqldump 参数,如 '--skip-lock-tables'")
class DirectorySourceConfig(BaseModel):
path: str = Field(..., min_length=1, description="要备份的目录绝对路径")
exclude_patterns: list[str] = Field(
default_factory=lambda: [".DS_Store", "__pycache__", "node_modules", ".git"]
)
class InfluxDBSourceConfigV1(BaseModel):
version: Literal["1"]
host: str = Field(..., min_length=1)
port: int = Field(8086, ge=1, le=65535)
database: str = Field(..., min_length=1)
username: str = Field("", description="留空表示无认证")
password: str = Field("")
class InfluxDBSourceConfigV2(BaseModel):
version: Literal["2"]
host: str = Field(..., min_length=1)
port: int = Field(8086, ge=1, le=65535)
org: str = Field(..., min_length=1)
bucket: str = Field(..., min_length=1)
token: str = Field(..., min_length=1)
class JobBase(BaseModel):
name: str = Field(..., min_length=1, max_length=64)
type: Literal["mysql", "directory", "influxdb"]
source_config: dict
storage_id: int
cron_expression: Optional[str] = Field(None, description="标准 5 段 cron 表达式,空表示手动")
enabled: bool = True
retention_count: int = Field(7, ge=1, le=365)
retention_days: Optional[int] = Field(None, ge=1, le=3650)
description: Optional[str] = Field(None, max_length=255)
@field_validator("cron_expression")
@classmethod
def _check_cron(cls, v: Optional[str]) -> Optional[str]:
if v is None or v.strip() == "":
return None
v = v.strip()
parts = v.split()
if len(parts) != 5:
raise ValueError("cron 必须是 5 段:分 时 日 月 周")
return v
class JobCreate(JobBase):
pass
class JobUpdate(BaseModel):
name: Optional[str] = Field(None, min_length=1, max_length=64)
source_config: Optional[dict] = None
storage_id: Optional[int] = None
cron_expression: Optional[str] = None
enabled: Optional[bool] = None
retention_count: Optional[int] = Field(None, ge=1, le=365)
retention_days: Optional[int] = Field(None, ge=1, le=3650)
description: Optional[str] = None
class JobOut(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
type: str
cron_expression: Optional[str]
enabled: bool
retention_count: int
retention_days: Optional[int]
description: Optional[str]
storage: StorageOut
created_at: datetime
updated_at: datetime
# 解密后的数据源配置;敏感字段已脱敏为 "***"
source_config_safe: dict = Field(
default_factory=dict,
description="解密后的 source_config,敏感字段(password/token/secret_key)已脱敏",
)
class RunSummary(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
status: str
trigger: str
started_at: Optional[datetime]
finished_at: Optional[datetime]
duration_seconds: Optional[int]
artifact_size: Optional[int]
error_message: Optional[str]
class JobWithLastRun(JobOut):
last_run: Optional[RunSummary] = None
class JobRunResponse(BaseModel):
run_id: int
status: str