feat: update vps-panel backup and notification features

This commit is contained in:
mstfyldz
2026-06-05 23:53:57 +03:00
parent 29d03604f9
commit c1f04735d6
22 changed files with 1029 additions and 372 deletions
+62
View File
@@ -0,0 +1,62 @@
import { NextRequest, NextResponse } from 'next/server'
import { requireAuth } from '@/lib/auth'
import { readConfig } from '@/lib/config'
import { createDbDumpBuffer, uploadToDropbox, uploadToGoogleDrive, backupFilename, testGoogleDriveCredentials } from '@/lib/backup'
import pool from '@/lib/appDb'
// POST /api/backups/test
// { db_id, cloud_type, credentials, gdrive_folder_id? }
// → Gerçek yedek alır, buluta yükler, loglar
export async function POST(req: NextRequest) {
const authErr = await requireAuth(req)
if (authErr) return authErr
const { db_id, cloud_type, credentials, gdrive_folder_id } = await req.json()
if (!credentials?.trim()) {
return NextResponse.json({ ok: false, error: 'Credentials boş olamaz.' })
}
// DB bilgisini al
const config = await readConfig()
const db = config.databases.find(d => d.id === db_id)
if (!db) return NextResponse.json({ ok: false, error: 'Veritabanı bulunamadı.' })
const filename = backupFilename()
try {
// 1. Dump al
const buffer = await createDbDumpBuffer(db)
// 2. Buluta yükle
if (cloud_type === 'dropbox') {
await uploadToDropbox(credentials.trim(), db.name, filename, buffer)
} else if (cloud_type === 'gdrive') {
await uploadToGoogleDrive(credentials, db.name, filename, buffer, gdrive_folder_id || undefined)
} else {
return NextResponse.json({ ok: false, error: 'Desteklenmeyen cloud türü.' })
}
// 3. Logla
await pool.query(
`INSERT INTO backup_logs (db_id, status, message, file_size) VALUES ($1, $2, $3, $4)`,
[db_id, 'success', `Manuel test yedek — ${cloud_type}`, buffer.length]
)
const sizeStr = buffer.length > 1024 * 1024
? `${(buffer.length / (1024 * 1024)).toFixed(2)} MB`
: `${(buffer.length / 1024).toFixed(1)} KB`
return NextResponse.json({
ok: true,
detail: `✓ Yedek alındı ve yüklendi — ${db.name}/${filename} (${sizeStr})`,
})
} catch (e: any) {
await pool.query(
`INSERT INTO backup_logs (db_id, status, message, file_size) VALUES ($1, $2, $3, $4)`,
[db_id, 'error', e.message, 0]
).catch(() => {})
return NextResponse.json({ ok: false, error: e.message })
}
}