fix: improve pg_dump execution and add better error handling
This commit is contained in:
@@ -80,10 +80,26 @@ export async function POST(request: NextRequest) {
|
|||||||
console.log('[BACKUP] Iniciando backup do PostgreSQL...');
|
console.log('[BACKUP] Iniciando backup do PostgreSQL...');
|
||||||
const pgBackupPath = path.join(backupPath, 'database.sql');
|
const pgBackupPath = path.join(backupPath, 'database.sql');
|
||||||
|
|
||||||
const pgCommand = `PGPASSWORD="${POSTGRES_PASSWORD}" pg_dump -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB} > "${pgBackupPath}"`;
|
// Usar execSync com shell shell: true para permitir redirecionamento de arquivo
|
||||||
execSync(pgCommand, { stdio: 'pipe', env: { ...process.env, PGPASSWORD: POSTGRES_PASSWORD } });
|
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) {
|
} catch (error) {
|
||||||
console.error('[BACKUP] Erro ao fazer backup do PostgreSQL:', error);
|
console.error('[BACKUP] Erro ao fazer backup do PostgreSQL:', error);
|
||||||
backupInfo.status = 'error';
|
backupInfo.status = 'error';
|
||||||
@@ -330,18 +346,26 @@ 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) {
|
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)) {
|
if (!fs.existsSync(dest)) {
|
||||||
fs.mkdirSync(dest, { recursive: true });
|
fs.mkdirSync(dest, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = fs.readdirSync(src);
|
const files = fs.readdirSync(src);
|
||||||
|
console.log(`[BACKUP] Copiando ${files.length} arquivos de ${src}`);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const srcPath = path.join(src, file);
|
const srcPath = path.join(src, file);
|
||||||
const destPath = path.join(dest, file);
|
const destPath = path.join(dest, file);
|
||||||
|
|
||||||
|
try {
|
||||||
const stat = fs.statSync(srcPath);
|
const stat = fs.statSync(srcPath);
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
@@ -349,6 +373,9 @@ function copyDirSync(src: string, dest: string) {
|
|||||||
} else {
|
} else {
|
||||||
fs.copyFileSync(srcPath, destPath);
|
fs.copyFileSync(srcPath, destPath);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`[BACKUP] Erro ao copiar ${srcPath}:`, (error as Error).message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,10 +386,16 @@ function calculateDirSize(dirPath: string): number {
|
|||||||
let size = 0;
|
let size = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!fs.existsSync(dirPath)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const files = fs.readdirSync(dirPath);
|
const files = fs.readdirSync(dirPath);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = path.join(dirPath, file);
|
const filePath = path.join(dirPath, file);
|
||||||
|
|
||||||
|
try {
|
||||||
const stat = fs.statSync(filePath);
|
const stat = fs.statSync(filePath);
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
@@ -370,9 +403,12 @@ function calculateDirSize(dirPath: string): number {
|
|||||||
} else {
|
} else {
|
||||||
size += stat.size;
|
size += stat.size;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`[BACKUP] Erro ao calcular tamanho de ${filePath}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erro ao calcular tamanho:', error);
|
console.error('[BACKUP] Erro ao calcular tamanho do diretório:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
|
|||||||
Reference in New Issue
Block a user