Skip to content

Instantly share code, notes, and snippets.

@bmadigan
Last active August 29, 2015 13:57
Show Gist options
  • Save bmadigan/9648453 to your computer and use it in GitHub Desktop.
Save bmadigan/9648453 to your computer and use it in GitHub Desktop.
Create A Mysqldump of the current snapshot of a mysql database for Codeception.
<?php
// Add to app/start/artisan.php
// Artisan::add(new MysqlDump);
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class MysqlDump extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'MysqlDump';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create A MysqlDump SQL dump into the test database for acceptance testing in Codeception';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$host = Config::get('database.connections.mysql.host');
$database = Config::get('database.connections.mysql.database');
$username = Config::get('database.connections.mysql.username');
$password = Config::get('database.connections.mysql.password');
$backupPath = app_path() . '/tests/_data/dump.sql';
// Sending some info to the console for really debugging purposes if needed.
$this->info('MysqlDump of database: ' . $database . ' - Export Started ...');
$this->info('... Processing into ... ' . $backupPath);
$this->info('We need your database password!');
$path = "/usr/bin/mysqldump";
$command = $path . " -u " . $username . " -p " . $database . " > " . $backupPath;
//$command = $path . " -u " . $username . " " . $database . " > " . $backupPath;
system($command);
$this->info('MysqlDump has been completed!');
}
}
@bmadigan
Copy link
Author

Note: The above code is used in a Vagrant VM so the paths may need to be changed depending on your configuration. It will also need to be run by SSH into your Vagrant server.

TIP:
If you need to know the path to your mysqldump command type: (osx/linux)

$>which mysqldump

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