Skip to content

Instantly share code, notes, and snippets.

@kkiernan
Last active March 4, 2024 19:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kkiernan/bdd0954d0149b89c372a to your computer and use it in GitHub Desktop.
Save kkiernan/bdd0954d0149b89c372a to your computer and use it in GitHub Desktop.
Laravel Artisan command that runs the mysqldump utility
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MySqlDump extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:mysqldump {schema} {password}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs the mysqldump utility';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$ds = DIRECTORY_SEPARATOR;
$schema = $this->argument('schema');
$password = $this->argument('password');
$path = database_path() . $ds . 'backups' . $ds . date('Y') . $ds . date('m') . $ds;
$file = date('Y-m-d') . '_mysqldump.sql';
$command = sprintf('mysqldump %s -u forge -p\'%s\' > %s', $schema, $password, $path . $file);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
exec($command);
}
}
@cambur
Copy link

cambur commented Dec 12, 2023

Do you get this error?
mysqldump: [Warning] Using a password on the command line interface can be insecure.

@privyreza
Copy link

Its not an error, just a warning, but the dump will succeed

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