<?php namespace Bundles\Core\Backup\Commands;
use \CodeIgniter\CLI\BaseCommand;
use \CodeIgniter\CLI\CLI;
class DatabaseBackupCommand extends BaseCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'Core';
/**
* The Command's name
*
* @var string
*/
protected $name = 'core:backup:database';
/**
* The Command's short description
*
* @var string
*/
protected $description = 'Dump the contents of a database';
/**
* The Command's usage
*
* @var string
*/
protected $usage = 'core:backup:database';
/**
* The Command's Arguments
*
* @var array
*/
protected $arguments = [];
/**
* The Command's Options
*
* @var array
*/
protected $options = [];
public function run(array $params)
{
// Get benchmark tool
$benchmark = \Config\Services::timer();
$benchmark->start('core:database:backup');
// Get database configuration
$config = new \Config\Database;
// The name of the directory where we need to save backup.
$directoryName = WRITEPATH . 'backup/database';
// Check if the directory already exists.
if(!is_dir($directoryName))
{
// Directory does not exist, so lets create it.
mkdir($directoryName, 0755, true);
}
\Spatie\DbDumper\Databases\MySql::create()
->setDbName($config->default['database'])
->setUserName($config->default['username'])
->setPassword($config->default['password'])
->addExtraOption('--host ' . $config->default['hostname'])
->dumpToFile(sprintf('%s/%s-%s.sql', $directoryName, $config->default['database'], date('d-m-Y-H:i')));
$benchmark->stop('core:database:backup');
// Show command execution details
CLI::write('Total execution time: '. CLI::color($benchmark->getElapsedTime('core:database:backup'), 'yellow'));
}
}