Skip to main content

ErugoOSS/Erugo

738 stars
2 code files
View ErugoOSS/Erugo on GitHub

app/Jobs/backUpDatabase.php

Open in GitHub
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
 
class backUpDatabase implements ShouldQueue
{
use Queueable;
 
public function handle(): void
{
Log::info('Backing up database');
 
$databasePath = config('database.connections.sqlite.database');
 
$backupPath = storage_path('app/backups/');
 
if (!file_exists($backupPath)) {
mkdir($backupPath, 0777, true);
}
 
$backupName = 'database_backup_' . now()->format('Y-m-d_H-i-s') . '.sqlite';
copy($databasePath, $backupPath . $backupName);
 
Log::info('Database backup created: ' . $backupName);
 
$backups = glob($backupPath . '*.sqlite');
$prunedBackups = 0;
foreach ($backups as $backup) {
if (filemtime($backup) < now()->subDays(7)->timestamp) {
unlink($backup);
$prunedBackups++;
}
}
 
Log::info('Database backups pruned: ' . $prunedBackups);
}
}

routes/console.php

Open in GitHub
use App\Jobs\backUpDatabase;
 
// ...
Schedule::job(backUpDatabase::class)->daily();
 
// ...
 
Artisan::command('back-up-database', function () {
backUpDatabase::dispatch();
})->purpose('Back up the database');

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.