diff --git a/frontend/src/app/api/backup/route.ts b/frontend/src/app/api/backup/route.ts index 7c37011..d1945e7 100644 --- a/frontend/src/app/api/backup/route.ts +++ b/frontend/src/app/api/backup/route.ts @@ -80,10 +80,26 @@ export async function POST(request: NextRequest) { console.log('[BACKUP] Iniciando backup do PostgreSQL...'); const pgBackupPath = path.join(backupPath, 'database.sql'); - const pgCommand = `PGPASSWORD="${POSTGRES_PASSWORD}" pg_dump -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB} > "${pgBackupPath}"`; - execSync(pgCommand, { stdio: 'pipe', env: { ...process.env, PGPASSWORD: POSTGRES_PASSWORD } }); + // Usar execSync com shell shell: true para permitir redirecionamento de arquivo + const env = { ...process.env, PGPASSWORD: POSTGRES_PASSWORD }; + const pgCommand = `pg_dump -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB} -v`; - console.log('[BACKUP] PostgreSQL backup concluído'); + const output = execSync(pgCommand, { + env, + encoding: 'utf-8', + maxBuffer: 50 * 1024 * 1024 // 50MB buffer + }); + + // Escrever o output em arquivo + fs.writeFileSync(pgBackupPath, output); + + const dbSize = fs.statSync(pgBackupPath).size; + console.log(`[BACKUP] PostgreSQL backup concluído (${dbSize} bytes)`); + + if (dbSize < 1000) { + console.warn('[BACKUP] AVISO: Arquivo de backup muito pequeno, pode estar vazio!'); + backupInfo.message += 'Aviso: Backup do PostgreSQL pode estar incompleto. '; + } } catch (error) { console.error('[BACKUP] Erro ao fazer backup do PostgreSQL:', error); backupInfo.status = 'error'; @@ -330,24 +346,35 @@ export async function DELETE(request: NextRequest) { } /** - * Função auxiliar: copiar diretório recursivamente + * Função auxiliar: copiar diretório recursivamente com log */ function copyDirSync(src: string, dest: string) { + if (!fs.existsSync(src)) { + console.warn(`[BACKUP] Diretório fonte não existe: ${src}`); + return; + } + if (!fs.existsSync(dest)) { fs.mkdirSync(dest, { recursive: true }); } const files = fs.readdirSync(src); + console.log(`[BACKUP] Copiando ${files.length} arquivos de ${src}`); for (const file of files) { const srcPath = path.join(src, file); const destPath = path.join(dest, file); - const stat = fs.statSync(srcPath); + + try { + const stat = fs.statSync(srcPath); - if (stat.isDirectory()) { - copyDirSync(srcPath, destPath); - } else { - fs.copyFileSync(srcPath, destPath); + if (stat.isDirectory()) { + copyDirSync(srcPath, destPath); + } else { + fs.copyFileSync(srcPath, destPath); + } + } catch (error) { + console.warn(`[BACKUP] Erro ao copiar ${srcPath}:`, (error as Error).message); } } } @@ -359,20 +386,29 @@ function calculateDirSize(dirPath: string): number { let size = 0; try { + if (!fs.existsSync(dirPath)) { + return 0; + } + const files = fs.readdirSync(dirPath); for (const file of files) { const filePath = path.join(dirPath, file); - const stat = fs.statSync(filePath); + + try { + const stat = fs.statSync(filePath); - if (stat.isDirectory()) { - size += calculateDirSize(filePath); - } else { - size += stat.size; + if (stat.isDirectory()) { + size += calculateDirSize(filePath); + } else { + size += stat.size; + } + } catch (e) { + console.warn(`[BACKUP] Erro ao calcular tamanho de ${filePath}`); } } } catch (error) { - console.error('Erro ao calcular tamanho:', error); + console.error('[BACKUP] Erro ao calcular tamanho do diretório:', error); } return size;