Correción en BackupCron para asegurar la creación del directorio de backup y manejo de errores
This commit is contained in:
parent
0be583a088
commit
5826a2c26c
@ -17,11 +17,17 @@ public function handle()
|
|||||||
{
|
{
|
||||||
$filename = 'backup-' . date('Y-m-d_H-i-s') . '.sql';
|
$filename = 'backup-' . date('Y-m-d_H-i-s') . '.sql';
|
||||||
$containerPath = '/tmp/' . $filename;
|
$containerPath = '/tmp/' . $filename;
|
||||||
$hostPath = storage_path('app/backup/' . $filename);
|
$finalPath = storage_path('app/backup/' . $filename);
|
||||||
|
|
||||||
// Crear backup dentro del contenedor MySQL
|
// Asegurar que existe el directorio de backup
|
||||||
|
$backupDir = storage_path('app/backup');
|
||||||
|
if (!file_exists($backupDir)) {
|
||||||
|
mkdir($backupDir, 0755, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crear backup ejecutando mysqldump dentro del contenedor MySQL
|
||||||
$dumpCommand = sprintf(
|
$dumpCommand = sprintf(
|
||||||
'docker exec repuve-backend-v1-mysql-1 sh -c "mysqldump --no-tablespaces -u%s -p%s %s > %s"',
|
'docker exec repuve-backend-v1-mysql-1 sh -c "mysqldump --no-tablespaces -u%s -p%s %s > %s" 2>&1',
|
||||||
env('DB_USERNAME'),
|
env('DB_USERNAME'),
|
||||||
env('DB_PASSWORD'),
|
env('DB_PASSWORD'),
|
||||||
env('DB_DATABASE'),
|
env('DB_DATABASE'),
|
||||||
@ -29,17 +35,61 @@ public function handle()
|
|||||||
);
|
);
|
||||||
exec($dumpCommand, $output, $returnCode);
|
exec($dumpCommand, $output, $returnCode);
|
||||||
|
|
||||||
// Copiar del contenedor al host
|
if ($returnCode !== 0) {
|
||||||
|
$this->error('Error al crear el backup en MySQL');
|
||||||
|
$this->error('Código: ' . $returnCode);
|
||||||
|
if (!empty($output)) {
|
||||||
|
$this->error('Salida: ' . implode("\n", $output));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copiar del contenedor MySQL a /tmp del host
|
||||||
|
$tempHostPath = '/tmp/' . $filename;
|
||||||
$copyCommand = sprintf(
|
$copyCommand = sprintf(
|
||||||
'docker cp repuve-backend-v1-mysql-1:%s %s',
|
'docker cp repuve-backend-v1-mysql-1:%s %s 2>&1',
|
||||||
$containerPath,
|
$containerPath,
|
||||||
$hostPath
|
$tempHostPath
|
||||||
);
|
);
|
||||||
exec($copyCommand);
|
exec($copyCommand, $copyOutput, $copyReturnCode);
|
||||||
|
|
||||||
// Limpiar archivo temporal del contenedor
|
if ($copyReturnCode !== 0) {
|
||||||
exec("docker exec repuve-backend-v1-mysql-1 rm " . $containerPath);
|
$this->error('Error al copiar el backup');
|
||||||
|
if (!empty($copyOutput)) {
|
||||||
|
$this->error('Salida: ' . implode("\n", $copyOutput));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
$this->info('Backup creado: ' . $filename);
|
// Mover de /tmp a storage y establecer permisos
|
||||||
|
if (file_exists($tempHostPath)) {
|
||||||
|
rename($tempHostPath, $finalPath);
|
||||||
|
chmod($finalPath, 0644);
|
||||||
|
|
||||||
|
// Limpiar archivo temporal del contenedor MySQL
|
||||||
|
exec('docker exec repuve-backend-v1-mysql-1 rm ' . $containerPath);
|
||||||
|
|
||||||
|
$size = filesize($finalPath);
|
||||||
|
$this->info('Backup creado exitosamente: ' . $filename);
|
||||||
|
$this->info('Tamaño: ' . $this->formatBytes($size));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
$this->error('Error: el archivo temporal no se creó');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formatear bytes a tamaño legible
|
||||||
|
*/
|
||||||
|
private function formatBytes($bytes, $precision = 2)
|
||||||
|
{
|
||||||
|
$units = ['B', 'KB', 'MB', 'GB'];
|
||||||
|
$bytes = max($bytes, 0);
|
||||||
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
||||||
|
$pow = min($pow, count($units) - 1);
|
||||||
|
$bytes /= pow(1024, $pow);
|
||||||
|
return round($bytes, $precision) . ' ' . $units[$pow];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ RUN apk add --no-cache \
|
|||||||
nano \
|
nano \
|
||||||
openssl \
|
openssl \
|
||||||
bash \
|
bash \
|
||||||
|
mysql-client \
|
||||||
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
|
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
|
||||||
|
|
||||||
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
|
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user