create cron for backing up db

This commit is contained in:
2025-09-24 14:42:46 +03:30
parent c43909574e
commit fadc8379d9
7 changed files with 69 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
<?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');
}
}