Files
vps-panel/src/app/api/db/backup/route.ts
T

34 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server'
import { readConfig } from '@/lib/config'
import { createDbDumpBuffer } from '@/lib/backup'
import pool from '@/lib/appDb'
// GET /api/db/backup?dbId=...
export async function GET(req: Request) {
const { searchParams } = new URL(req.url)
const dbId = searchParams.get('dbId')
if (!dbId) return NextResponse.json({ error: 'Missing dbId' }, { status: 400 })
const config = await readConfig()
const db = config.databases.find(d => d.id === dbId)
if (!db) return NextResponse.json({ error: 'Database not found' }, { status: 404 })
try {
const buffer = await createDbDumpBuffer(db)
const dateStr = new Date().toISOString().replace(/[:.]/g, '-')
const filename = `${db.name}_${dateStr}.sql`
await pool.query(`INSERT INTO backup_logs (db_id, status, error, size, target) VALUES ($1, $2, $3, $4, $5)`, [dbId, 'success', null, buffer.length, 'manuel indirme'])
return new NextResponse(buffer.toString('utf-8'), {
headers: {
'Content-Type': 'application/sql',
'Content-Disposition': `attachment; filename="${filename}"`
}
})
} catch (e: any) {
await pool.query(`INSERT INTO backup_logs (db_id, status, error, size, target) VALUES ($1, $2, $3, $4, $5)`, [dbId, 'error', e.message, 0, 'manuel indirme'])
return NextResponse.json({ error: e.message }, { status: 500 })
}
}