First Commit
This commit is contained in:
Executable
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class DbBackupCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'db:backup';
|
||||
protected static $defaultDescription = 'Cria um backup do banco de dados';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setHelp('Este comando gera um dump SQL do banco de dados atual.')
|
||||
->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Diretório para salvar o backup', 'storage/backups')
|
||||
->addOption('compress', 'c', InputOption::VALUE_NONE, 'Comprimir o backup (gzip)');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$output->writeln('<info>Iniciando backup do banco de dados...</info>');
|
||||
|
||||
$host = $_ENV['DB_HOST'] ?? 'localhost';
|
||||
$port = $_ENV['DB_PORT'] ?? '3306';
|
||||
$database = $_ENV['DB_DATABASE'] ?? 'syscms';
|
||||
$username = $_ENV['DB_USERNAME'] ?? 'root';
|
||||
$password = $_ENV['DB_PASSWORD'] ?? '';
|
||||
|
||||
$backupDir = $input->getOption('path');
|
||||
|
||||
// Se o caminho for relativo, resolvemos a partir da raiz do projeto
|
||||
if (!str_starts_with($backupDir, '/')) {
|
||||
$backupDir = dirname(__DIR__, 3) . '/' . ltrim($backupDir, '/');
|
||||
}
|
||||
|
||||
if (!is_dir($backupDir)) {
|
||||
if (!mkdir($backupDir, 0755, true)) {
|
||||
$output->writeln("<error>Não foi possível criar o diretório de backup: {$backupDir}</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
$filename = $database . '_' . date('Y-m-d_H-i-s') . '.sql';
|
||||
$fullPath = rtrim($backupDir, '/') . '/' . $filename;
|
||||
|
||||
// Escapamos a senha para o shell
|
||||
$escapedPassword = escapeshellarg($password);
|
||||
|
||||
// Usamos mysqldump para criar o backup
|
||||
// Note: Se o mysqldump não estiver no PATH, este comando pode falhar
|
||||
$command = "mysqldump --host={$host} --port={$port} --user={$username} --password={$escapedPassword} {$database} > " . escapeshellarg($fullPath);
|
||||
|
||||
if ($input->getOption('compress')) {
|
||||
$command .= " && gzip " . escapeshellarg($fullPath);
|
||||
$fullPath .= '.gz';
|
||||
}
|
||||
|
||||
exec($command . " 2>&1", $outputArray, $resultCode);
|
||||
|
||||
if ($resultCode === 0) {
|
||||
$output->writeln("<info>Backup concluído com sucesso: {$fullPath}</info>");
|
||||
return Command::SUCCESS;
|
||||
} else {
|
||||
$output->writeln("<error>Erro ao realizar o backup (Código: {$resultCode}).</error>");
|
||||
$output->writeln("<error>Saída: " . implode("\n", $outputArray) . "</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user