mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-05-05 13:40:44 +08:00
POST /backups 和 POST /backups/:id/restore 改为异步:立即返回 HTTP 202, 后台 goroutine 独立执行 pg_dump → gzip → S3 上传,前端每 2s 轮询状态。 后端: - 新增 StartBackup/StartRestore 方法,后台 goroutine 不依赖 HTTP 连接 - Graceful shutdown 等待活跃操作完成,启动时清理孤立 running 记录 - BackupRecord 新增 progress/restore_status 字段支持进度和恢复状态追踪 前端: - 创建备份/恢复后轮询 GET /backups/:id 直到完成或失败 - 标签页切换暂停/恢复轮询,组件卸载清理定时器 - 正确处理 409(备份进行中)和轮询超时 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
120 lines
3.1 KiB
TypeScript
120 lines
3.1 KiB
TypeScript
import { apiClient } from '../client'
|
|
|
|
export interface BackupS3Config {
|
|
endpoint: string
|
|
region: string
|
|
bucket: string
|
|
access_key_id: string
|
|
secret_access_key?: string
|
|
prefix: string
|
|
force_path_style: boolean
|
|
}
|
|
|
|
export interface BackupScheduleConfig {
|
|
enabled: boolean
|
|
cron_expr: string
|
|
retain_days: number
|
|
retain_count: number
|
|
}
|
|
|
|
export interface BackupRecord {
|
|
id: string
|
|
status: 'pending' | 'running' | 'completed' | 'failed'
|
|
backup_type: string
|
|
file_name: string
|
|
s3_key: string
|
|
size_bytes: number
|
|
triggered_by: string
|
|
error_message?: string
|
|
started_at: string
|
|
finished_at?: string
|
|
expires_at?: string
|
|
progress?: string
|
|
restore_status?: string
|
|
restore_error?: string
|
|
restored_at?: string
|
|
}
|
|
|
|
export interface CreateBackupRequest {
|
|
expire_days?: number
|
|
}
|
|
|
|
export interface TestS3Response {
|
|
ok: boolean
|
|
message: string
|
|
}
|
|
|
|
// S3 Config
|
|
export async function getS3Config(): Promise<BackupS3Config> {
|
|
const { data } = await apiClient.get<BackupS3Config>('/admin/backups/s3-config')
|
|
return data
|
|
}
|
|
|
|
export async function updateS3Config(config: BackupS3Config): Promise<BackupS3Config> {
|
|
const { data } = await apiClient.put<BackupS3Config>('/admin/backups/s3-config', config)
|
|
return data
|
|
}
|
|
|
|
export async function testS3Connection(config: BackupS3Config): Promise<TestS3Response> {
|
|
const { data } = await apiClient.post<TestS3Response>('/admin/backups/s3-config/test', config)
|
|
return data
|
|
}
|
|
|
|
// Schedule
|
|
export async function getSchedule(): Promise<BackupScheduleConfig> {
|
|
const { data } = await apiClient.get<BackupScheduleConfig>('/admin/backups/schedule')
|
|
return data
|
|
}
|
|
|
|
export async function updateSchedule(config: BackupScheduleConfig): Promise<BackupScheduleConfig> {
|
|
const { data } = await apiClient.put<BackupScheduleConfig>('/admin/backups/schedule', config)
|
|
return data
|
|
}
|
|
|
|
// Backup operations
|
|
export async function createBackup(req?: CreateBackupRequest): Promise<BackupRecord> {
|
|
const { data } = await apiClient.post<BackupRecord>('/admin/backups', req || {})
|
|
return data
|
|
}
|
|
|
|
export async function listBackups(): Promise<{ items: BackupRecord[] }> {
|
|
const { data } = await apiClient.get<{ items: BackupRecord[] }>('/admin/backups')
|
|
return data
|
|
}
|
|
|
|
export async function getBackup(id: string): Promise<BackupRecord> {
|
|
const { data } = await apiClient.get<BackupRecord>(`/admin/backups/${id}`)
|
|
return data
|
|
}
|
|
|
|
export async function deleteBackup(id: string): Promise<void> {
|
|
await apiClient.delete(`/admin/backups/${id}`)
|
|
}
|
|
|
|
export async function getDownloadURL(id: string): Promise<{ url: string }> {
|
|
const { data } = await apiClient.get<{ url: string }>(`/admin/backups/${id}/download-url`)
|
|
return data
|
|
}
|
|
|
|
// Restore
|
|
export async function restoreBackup(id: string, password: string): Promise<BackupRecord> {
|
|
const { data } = await apiClient.post<BackupRecord>(`/admin/backups/${id}/restore`, { password })
|
|
return data
|
|
}
|
|
|
|
export const backupAPI = {
|
|
getS3Config,
|
|
updateS3Config,
|
|
testS3Connection,
|
|
getSchedule,
|
|
updateSchedule,
|
|
createBackup,
|
|
listBackups,
|
|
getBackup,
|
|
deleteBackup,
|
|
getDownloadURL,
|
|
restoreBackup,
|
|
}
|
|
|
|
export default backupAPI
|