52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
class BackupDatabaseCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'db:backup';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'physical database backup';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
$path = config('global_variables.BACKUP_TARGET_DIR');
|
|
$user = config('global_variables.BACKUP_USER');
|
|
$password = config('global_variables.BACKUP_PASSWORD');
|
|
$time = date('Y-m-d');
|
|
|
|
$backup = Process::path($path)->forever()->run("sudo mariadb-backup --backup --target-dir={$path}/{$time} --user={$user} --password={$password}");
|
|
|
|
if ($backup->failed()) {
|
|
Log::channel('mariadb-backup')->info("Backup failed at {$time}");
|
|
}
|
|
|
|
$prepare = Process::path($path)->forever()->run("sudo mariadb-backup --prepare --target-dir={$path}/{$time}");
|
|
|
|
if ($prepare->failed()) {
|
|
Log::channel('mariadb-backup')->info("Preparation failed at {$time}");
|
|
}
|
|
|
|
Log::channel('mariadb-backup')->info("Backup successful at {$time}");
|
|
|
|
$this->info('done');
|
|
}
|
|
}
|