%PDF- %GIF98; %PNG;
Server : ApacheSystem : Linux host.digitalbabaji.in 4.18.0-513.11.1.el8_9.x86_64 #1 SMP Wed Jan 17 02:00:40 EST 2024 x86_64 User : addictionfreeind ( 1003) PHP Version : 7.2.34 Disable Function : exec,passthru,shell_exec,system Directory : /home/addictionfreeind/public_html/admin1/app/Console/Commands/ |
Upload File : |
<?php
namespace App\Console\Commands;
use App\Http\Controllers\DatabaseBackupSettingController;
use App\Models\DatabaseBackupSetting;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class AutoDatabaseBackup extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'create-database-backup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Auto create database backup';
/**
* Execute the console command.
*
* @return bool Returns true if backup was created, false otherwise
*/
public function handle(): bool
{
// Get the first record of the database backup setting
$backupSetting = DatabaseBackupSetting::first();
// If there's no record or if the status is inactive, return false
if (!$backupSetting || $backupSetting->status == 'inactive') {
return false;
}
// Get the backups list
$dbController = new DatabaseBackupSettingController();
$backups = $dbController->getBackup();
// Reverse the backups array to get the most recent backup
$backups = array_reverse($backups);
// If there's no backup, create one immediately
if (count($backups) == 0) {
Artisan::call('backup:run', ['--only-db' => true, '--disable-notifications' => true]);
return true;
}
// Calculate the difference between the most recent backup and today's date
$date = Carbon::parse(($backups)[0]['last_modified']);
$dateDifference = $date->diffInDays(now());
// If the difference is less than the backup_after_days setting, return false
if ($dateDifference < $backupSetting->backup_after_days) {
return false;
}
// Get the current time in the timezone set in the global setting
$nowTimeWithTimeZone = now()->setTimezone(global_setting()->timezone)->format('H:i:s');
$settingHourOfDay = Carbon::createFromFormat('H:i:s', $backupSetting->hour_of_day)->format('H:i:s');
// If the current time is equal or greater than the hour_of_day setting, create a backup
if ($nowTimeWithTimeZone >= $settingHourOfDay) {
Artisan::call('backup:run', ['--only-db' => true, '--disable-notifications' => true]);
return true;
}
return false;
}
}