Last active
March 4, 2024 19:19
-
-
Save kkiernan/bdd0954d0149b89c372a to your computer and use it in GitHub Desktop.
Laravel Artisan command that runs the mysqldump utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its not an error, just a warning, but the dump will succeed