"use client"; import { useState, useEffect } from "react"; import { ArrowDownTrayIcon, ArrowUpTrayIcon, ClockIcon, ServerIcon, CheckCircleIcon, ExclamationTriangleIcon, BoltIcon, } from '@heroicons/react/24/outline'; interface Backup { filename: string; size: string; date: string; timestamp: string; } export default function BackupPage() { const [loading, setLoading] = useState(false); const [backups, setBackups] = useState([]); const [selectedBackup, setSelectedBackup] = useState(""); const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null); const [autoBackupEnabled, setAutoBackupEnabled] = useState(false); const [autoBackupInterval, setAutoBackupInterval] = useState(6); useEffect(() => { loadBackups(); loadAutoBackupSettings(); }, []); const loadAutoBackupSettings = () => { const enabled = localStorage.getItem('autoBackupEnabled') === 'true'; const interval = parseInt(localStorage.getItem('autoBackupInterval') || '6'); setAutoBackupEnabled(enabled); setAutoBackupInterval(interval); }; const toggleAutoBackup = () => { const newValue = !autoBackupEnabled; setAutoBackupEnabled(newValue); localStorage.setItem('autoBackupEnabled', newValue.toString()); if (newValue) { startAutoBackup(); setMessage({ type: 'success', text: `Backup automático ativado (a cada ${autoBackupInterval}h)` }); } else { stopAutoBackup(); setMessage({ type: 'success', text: 'Backup automático desativado' }); } }; const startAutoBackup = () => { const intervalMs = autoBackupInterval * 60 * 60 * 1000; // horas para ms const intervalId = setInterval(() => { createBackup(); }, intervalMs); localStorage.setItem('autoBackupIntervalId', intervalId.toString()); }; const stopAutoBackup = () => { const intervalId = localStorage.getItem('autoBackupIntervalId'); if (intervalId) { clearInterval(parseInt(intervalId)); localStorage.removeItem('autoBackupIntervalId'); } }; const changeInterval = (hours: number) => { setAutoBackupInterval(hours); localStorage.setItem('autoBackupInterval', hours.toString()); if (autoBackupEnabled) { stopAutoBackup(); startAutoBackup(); setMessage({ type: 'success', text: `Intervalo alterado para ${hours}h` }); } }; const loadBackups = async () => { try { const token = localStorage.getItem('token'); const response = await fetch('http://localhost:8085/api/superadmin/backups', { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const data = await response.json(); setBackups(data.backups || []); } } catch (error) { console.error('Erro ao carregar backups:', error); } }; const createBackup = async () => { setLoading(true); setMessage(null); try { const token = localStorage.getItem('token'); const response = await fetch('http://localhost:8085/api/superadmin/backup/create', { method: 'POST', headers: { 'Authorization': `Bearer ${token}` } }); const data = await response.json(); if (response.ok) { setMessage({ type: 'success', text: `Backup criado: ${data.filename} (${data.size})` }); await loadBackups(); } else { setMessage({ type: 'error', text: data.error || 'Erro ao criar backup' }); } } catch (error) { setMessage({ type: 'error', text: 'Erro ao criar backup' }); } finally { setLoading(false); } }; const restoreBackup = async () => { if (!selectedBackup) { setMessage({ type: 'error', text: 'Selecione um backup para restaurar' }); return; } if (!confirm(`⚠️ ATENÇÃO: Isso irá SOBRESCREVER todos os dados atuais!\n\nDeseja restaurar o backup:\n${selectedBackup}?`)) { return; } setLoading(true); setMessage(null); try { const token = localStorage.getItem('token'); const response = await fetch('http://localhost:8085/api/superadmin/backup/restore', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ filename: selectedBackup }) }); const data = await response.json(); if (response.ok) { setMessage({ type: 'success', text: 'Backup restaurado com sucesso! Recarregando...' }); setTimeout(() => { window.location.reload(); }, 2000); } else { setMessage({ type: 'error', text: data.error || 'Erro ao restaurar backup' }); } } catch (error) { setMessage({ type: 'error', text: 'Erro ao restaurar backup' }); } finally { setLoading(false); } }; const downloadBackup = async (filename: string) => { try { const token = localStorage.getItem('token'); const response = await fetch(`http://localhost:8085/api/superadmin/backup/download/${filename}`, { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); } else { setMessage({ type: 'error', text: 'Erro ao baixar backup' }); } } catch (error) { setMessage({ type: 'error', text: 'Erro ao baixar backup' }); } }; return (

Backup & Restore

Gerencie backups do banco de dados PostgreSQL

{message && (
{message.type === 'success' ? ( ) : ( )} {message.text}
)}

Criar Backup

Exportar dados atuais

Backup Automático

{autoBackupEnabled ? `Ativo (${autoBackupInterval}h)` : 'Desativado'}

Restaurar Backup

⚠️ Sobrescreve dados atuais

Backups Disponíveis

{backups.length} arquivo(s)
{backups.length === 0 ? (

Nenhum backup encontrado

) : (
{backups.map((backup) => (
setSelectedBackup(backup.filename)} >

{backup.filename}

{backup.date} • {backup.size}

))}
)}
); }