ADD: Se implementó el comando para realizar respaldos automáticos de la base de datos
This commit is contained in:
parent
02220407ff
commit
6af33e4503
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,6 +8,7 @@
|
||||
/public/vendor
|
||||
/storage/*.key
|
||||
/storage/pail
|
||||
/storage/app/backup
|
||||
/vendor
|
||||
.env
|
||||
.env.backup
|
||||
|
||||
45
app/Console/Commands/BackupCron.php
Normal file
45
app/Console/Commands/BackupCron.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@ -8,3 +8,4 @@
|
||||
}
|
||||
|
||||
Schedule::call(new DeleteResetPasswords)->hourly();
|
||||
Schedule::command('backup:cron')->dailyAt('00:00');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user