82 lines
2.6 KiB
PHP
Executable File
82 lines
2.6 KiB
PHP
Executable File
<?php namespace GromIT\MysqlBackup\Console;
|
|
|
|
use Carbon\Carbon;
|
|
use Gromit\MysqlBackup\Models\MysqlBackup;
|
|
use Gromit\MysqlBackup\Models\MysqlBackupSettings;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use October\Rain\Support\Facades\Config;
|
|
|
|
class RunBackup extends Command
|
|
{
|
|
/**
|
|
* @var string The console command name.
|
|
*/
|
|
protected $name = 'mysqlbackup:run';
|
|
|
|
/**
|
|
* @var string The console command description.
|
|
*/
|
|
protected $description = 'Create mysql database dump-file';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$config = Config::get('database.connections.' . Config::get('database.default'));
|
|
$settings = MysqlBackupSettings::instance();
|
|
if ($config['driver'] === 'mysql' && !empty($settings->get('mysqldump_path'))) {
|
|
if (!Storage::exists('backup')) {
|
|
Storage::makeDirectory('backup');
|
|
}
|
|
|
|
if (!empty($config['password'])) {
|
|
$command = $settings->get('mysqldump_path') . " --user=" . $config['username'] . " --password=" . $config['password'] . " --host=" . $config['host'] . " " . $config['database'];
|
|
} else {
|
|
$command = $settings->get('mysqldump_path') . " --user=" . $config['username'] . " --host=" . $config['host'] . " " . $config['database'];
|
|
}
|
|
|
|
$gzip = !empty(exec("which gzip")) ? exec("which gzip") : null;
|
|
|
|
if (is_null($gzip)) {
|
|
$filename = Carbon::now()->format('d-m-Y___H-i-s') . '.sql';
|
|
$system = system($command . " > " . storage_path('app/backup/' . $filename));
|
|
} else {
|
|
$filename = Carbon::now()->format('d-m-Y___H-i-s') . '.sql.gz';
|
|
$system = system($command . " | " . $gzip . " -c > " . storage_path('app/backup/' . $filename));
|
|
}
|
|
if ($system !== false) {
|
|
MysqlBackup::create(['filename' => $filename]);
|
|
$this->output->success(trans('gromit.mysqlbackup::lang.console.done'));
|
|
}else {
|
|
$this->output->error(trans('gromit.mysqlbackup::lang.console.error'));
|
|
}
|
|
}else {
|
|
$this->output->error(trans('gromit.mysqlbackup::lang.console.error'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the console command arguments.
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getArguments()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Get the console command options.
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getOptions()
|
|
{
|
|
return [];
|
|
}
|
|
}
|