dgd-backupper-php/app/Commands/MysqlBackupCommand.php

80 lines
2.2 KiB
PHP

<?php
namespace App\Commands;
use App\Utils\BackupManager;
use App\Utils\Builders\DatabaseBuilder;
use App\Utils\Credential;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
class MysqlBackupCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'backup:mysql
{--container= : Mysql container name or id}
{--user= : Mysql username}
{--password= : Mysql password}
{--folder= : Export folder for backup}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Backup mysql databases.';
/**
* Execute the console command.
*
* @return mixed
* @throws \Throwable
*/
public function handle()
{
$container = $this->option('container');
$user = $this->option('user');
$password = $this->option('password');
$bm = new BackupManager();
$this->line("<fg=blue>|-> Backing up mysql databases...</>");
$credentials = new Credential();
$credentials->setUsername($user);
$credentials->setPassword($password);
$builder = new DatabaseBuilder();
$builder->setCredential($credentials);
$builder->setContainerName($container);
$backupFolder = $this->option('folder');
if (!$backupFolder) {
$backupFolder = env('BACKUP_FOLDER');
}
$bm->setBackupFolder($backupFolder)
->setBuilder($builder)->execute();
if ($bm->getShell()->getReturnValue() == 0) {
$this->line("<fg=green> |-> Mysql databases successfully backed up.</>");
} else {
$this->line("<fg=red> |-> Mysql databases could not be backed up.</>");
}
return;
}
/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}