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

93 lines
2.4 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');
if (!$container) {
$container = env('MYSQL_CONTAINER_NAME');
}
if (!$user) {
$user = env('MYSQL_USER');
}
if (!$password) {
$password = env('MYSQL_PASSWORD');
}
$bm = new BackupManager();
$this->line("<fg=blue>|-> Backing up mysql databases...</>");
$credentials = new Credential();
$credentials->setUsername($user);
$credentials->setPassword($password);
$backupFolder = $this->option('folder');
if (!$backupFolder) {
$backupFolder = env('BACKUP_FOLDER');
}
$builder = new DatabaseBuilder();
$builder->setCredential($credentials);
$builder->setContainerName($container)
->setBackupFolder($backupFolder);
$bm->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();
}
}