Skip to content

Instantly share code, notes, and snippets.

@fitorec
Created March 2, 2020 19:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fitorec/d7f8003ff1b5501177068f00087cb51a to your computer and use it in GitHub Desktop.
Save fitorec/d7f8003ff1b5501177068f00087cb51a to your computer and use it in GitHub Desktop.
Agregar comando Artisan de Laravel para backups de la base de datos
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Dotenv\Dotenv;
/**
* Codigo inspirado en:
* - https://gist.github.com/phillipsharring/fb500556e7173a1c222d
*
* Mayor información de los commandos personalizados:
* - https://laravel.com/docs/5.8/artisan#generating-commands
*
*
*
* vendor/laravel/framework/src/Illuminate/Support/helpers.php:643
**/
class DbDump extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:dump';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Respalda la bd del host, si host es nula genera el respaldo de todas la BDs existentes en el sistema';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
$ds = DIRECTORY_SEPARATOR;
$host = env('DB_HOST');
$username = env('DB_USERNAME');
$password = env('DB_PASSWORD');
$database = env('DB_DATABASE');
$path = database_path('backups' . $ds);
$file = 'bd' . date('_Y-m-d') . '.sql';
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$this->line('<fg=cyan>Backup: </><fg=yellow;bg=black>'. $path . $file . '</>');
# Generamos el comando con mysqldump para exportar los datos
$command = sprintf(
'mysqldump --skip-comments --skip-compact --no-create-info'
. ' --skip-triggers --complete-insert --skip-add-locks'
. ' --disable-keys --lock-tables --host="%s" --user="%s" '
, $host, $username
);
if (!empty($password)) {
$command .= sprintf('--password="%s" ', $password);
}
$command .= sprintf('%s > "%s"', $database, $path . $file);
$this->line('<fg=green>CMD: </><fg=yellow;bg=black>'. $command . '</>');
exec($command, $output, $return);
if ($return) {
$this->line('<fg=red;bg=yellow>Error al intentar generar el Backup</>');
if (file_exists($path . $ds . $file)) {
unlink($path . $ds . $file);
}
return; // error
}
// Comprimiendo el archivo:
// mayor info: https://www.php.net/manual/es/function.gzopen.php
// Open the gz file (w9 is the highest compression)
$fileCompress = gzopen ($path . $ds . $file . '.gz', 'w9');
// Compress the file
gzwrite ($fileCompress, file_get_contents($path . $ds . $file));
// Close the gz file and we are done
gzclose($fileCompress);
// Generando el esquema
$path = database_path('backups' . $ds . 'schemas'. $ds);
$file = 'schema.sql';
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
# Generamos el comando con mysqldump para exportar la estructura
$command = sprintf(
'mysqldump --skip-comments --skip-compact '
. ' --no-data --host="%s" --user="%s" '
, $host, $username
);
if (!empty($password)) {
$command .= sprintf('--password="%s" ', $password);
}
$command .= sprintf(
'%s | sed "s/ AUTO_INCREMENT=[0-9]*//g" > "%s"',
$database, $path . $file
);
$this->line('<fg=magenta>Generando Schema</>');
exec($command, $output, $return);
if ($return) {
$this->line('<fg=red;bg=yellow>Error al intentar generar el Schema</>');
if (file_exists($path . $ds . $file)) {
unlink($path . $ds . $file);
}
return; // error
}
}
}
@karen05
Copy link

karen05 commented Jun 18, 2021

Hola, una pregunta, de casualidad sabes el porque al momento de ejecutar me marca un error con mysqldump, no lo reconoce como comando interno o externo, pero solo en la consola del proyecto, si se usas en la consola del sistema si lo reconoce, y si, si tengo las variables de entorno. pero ese error no me deja.
"CMD: mysqldump --skip-comments --skip-compact --no-create-info --skip-triggers --complete-insert --skip-add-locks --disable-keys --lock-tables --host="" --user="" > "C:\xampp\htdocs\carpeta\database\backups\bd_2021-06-18.sql"
"mysqldump" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
Error al intentar generar el Backup"

@ethgar-MTZ
Copy link

@karen05 tube el mismo problema en windows, lo que hice fue poner el path de mysql dump donde esta en windows, sin revisar la ubicación es algo asi "C:\xampp\bin\mysql\mysqldump.exe"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment