ADD: Se implementó el comando para realizar respaldos automáticos de la base de datos

This commit is contained in:
Juan Felipe Zapata Moreno 2025-12-03 13:09:47 -06:00
parent 02220407ff
commit 6af33e4503
3 changed files with 47 additions and 0 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@
/public/vendor /public/vendor
/storage/*.key /storage/*.key
/storage/pail /storage/pail
/storage/app/backup
/vendor /vendor
.env .env
.env.backup .env.backup

View File

@ -0,0 +1,45 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class BackupCron extends Command
{
protected $signature = 'backup:cron';
protected $description = 'Respaldo automático de la base de datos';
/**
* Ejecutar comando
*/
public function handle()
{
$filename = 'backup-' . date('Y-m-d_H-i-s') . '.sql';
$containerPath = '/tmp/' . $filename;
$hostPath = storage_path('app/backup/' . $filename);
// Crear backup dentro del contenedor MySQL
$dumpCommand = sprintf(
'docker exec repuve-backend-v1-mysql-1 sh -c "mysqldump --no-tablespaces -u%s -p%s %s > %s"',
env('DB_USERNAME'),
env('DB_PASSWORD'),
env('DB_DATABASE'),
$containerPath
);
exec($dumpCommand, $output, $returnCode);
// Copiar del contenedor al host
$copyCommand = sprintf(
'docker cp repuve-backend-v1-mysql-1:%s %s',
$containerPath,
$hostPath
);
exec($copyCommand);
// Limpiar archivo temporal del contenedor
exec("docker exec repuve-backend-v1-mysql-1 rm " . $containerPath);
$this->info('Backup creado: ' . $filename);
}
}

View File

@ -8,3 +8,4 @@
} }
Schedule::call(new DeleteResetPasswords)->hourly(); Schedule::call(new DeleteResetPasswords)->hourly();
Schedule::command('backup:cron')->dailyAt('00:00');