create cron for backing up db
This commit is contained in:
51
app/Console/Commands/BackupDatabaseCommand.php
Normal file
51
app/Console/Commands/BackupDatabaseCommand.php
Normal 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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user