Skip to content

Commit

Permalink
Improved backup
Browse files Browse the repository at this point in the history
  • Loading branch information
kreaweb.be committed May 1, 2024
1 parent 3c90912 commit dd24158
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/Http/Controllers/Back/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ class BackupController extends Controller
// ------------------------------------------------------------------------------
// To make this BACKUP controller work, you need to :
// ------------------------------------------------------------------------------
// 1. add and configure this to your .env :
// 1. install laravel-backup
// https://github.com/spatie/laravel-backup
//
// 2. add and configure this to your .env :
//
// BACKUP_DISK="backups"
// BACKUP_DAILY_CLEANUP="22:30"
// BACKUP_DAILY_RUN="23:00"
// BACKUP_MAIL_ADDRESS="webmaster@yourdomain.com"
//
// 2. configure this to a working mail system in your .env :
// 3. configure this to a working mail system in your .env :
// MAIL_MAILER=smtp
// MAIL_HOST=mailpit
// MAIL_PORT=1025
Expand All @@ -31,15 +34,15 @@ class BackupController extends Controller
// MAIL_FROM_ADDRESS="no-reply@yourdomain.com"
// MAIL_FROM_NAME="${APP_NAME}"
// ------------------------------------------------------------------------------
// 2. add this to your config/filesystem.php :
// 4. add this to your config/filesystem.php :
//
// env('BACKUP_DISK', 'backups') => [
// 'driver' => 'local',
// 'root' => storage_path('app/' . env('BACKUP_DISK', 'backups')),
// 'throw' => false,
// ],
// ------------------------------------------------------------------------------
// 3. configure this in your config/backup.php :
// 5. configure this in your config/backup.php :
//
// // backup --> destination --> disks :
// 'disks' => [
Expand All @@ -53,7 +56,7 @@ class BackupController extends Controller
// ------------------------------------------------------------------------------
public function index()
{
$disk = Storage::disk(env('BACKUP_DISK', 'backups'));
$disk = Storage::disk(config('app.backup_disk'));
$files = $disk->files(config('backup.backup.name'));

$backups = [];
Expand Down Expand Up @@ -108,11 +111,11 @@ public function create()

public function download($file_name)
{
$disk = Storage::disk(env('BACKUP_DISK', 'backups'));
$disk = Storage::disk(config('app.backup_disk'));
$file = config('backup.backup.name') . '/' . $file_name;

if ($disk->exists($file)) {
return Storage::download(env('BACKUP_DISK', 'backups') . '/' . $file);
return Storage::download(config('app.backup_disk') . '/' . $file);
} else {
$notification = [
'type' => 'warning',
Expand All @@ -126,7 +129,7 @@ public function download($file_name)

public function delete($file_name)
{
$disk = Storage::disk(env('BACKUP_DISK', 'backups'));
$disk = Storage::disk(config('app.backup_disk'));
$file = config('backup.backup.name') . '/' . $file_name;

if ($disk->exists($file)) {
Expand Down
11 changes: 11 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@
'store' => env('APP_MAINTENANCE_STORE', 'database'),
],

/*
|--------------------------------------------------------------------------
| Custom values used in the application outside of the config files
|--------------------------------------------------------------------------
*/

'backup_disk' => env('BACKUP_DISK', 'backups'),
'backup_daily_cleanup' => env('BACKUP_DAILY_CLEANUP', '22:30'),
'backup_daily_run' => env('BACKUP_DAILY_RUN', '23:00'),
'backup_mail_address' => env('BACKUP_MAIL_ADDRESS', 'webmaster@yourdomain.com'),

];
22 changes: 22 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schedule;

// --------------------------------------------------------------------------------
// schedule daily backup
// --------------------------------------------------------------------------------
Schedule::command('backup:clean')->daily()->at(config('app.backup_daily_cleanup'))
->onSuccess(function () {
Log::info('Backup (Scheduled) -- Cleanup succeeded');
})
->onFailure(function () {
Log::warning('Backup (Scheduled) -- Cleanup failed');
});

Schedule::command('backup:run --only-db')->daily()->at(config('app.backup_daily_run'))
->onSuccess(function () {
Log::info('Backup (Scheduled) -- Backup succeeded');
})
->onFailure(function () {
Log::warning('Backup (Scheduled) -- Backup failed');
});
// --------------------------------------------------------------------------------

Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
Expand Down

0 comments on commit dd24158

Please sign in to comment.