PHP code example of avcodewizard / laravel-backup

1. Go to this page and download the library: Download avcodewizard/laravel-backup library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

avcodewizard / laravel-backup example snippets


return [
    'destinations' => [
        'local' => [
            'enabled' => true,
            'path' => storage_path('backups'),
        ],
        's3' => [
            'enabled' => false,
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'bucket' => env('AWS_BUCKET'),
            'endpoint' => env('AWS_ENDPOINT'), // Optional: for MinIO, DO Spaces
            'path' => 'backups',
        ],
        'google_drive' => [
            'enabled' => false,
            // OAuth 2.0 credentials - get refresh token using: php artisan backup:google-auth
            'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
            'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
            'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
            'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID'), // Optional: specific folder ID
            'path' => 'backups',
        ],
    ],
    'keep_days' => 5, // Automatically delete backups older than 5 days
    'cleanup_scope' => 'all', // 'all' = clean cloud + local, 'local' = clean local only
    'backup_storage_directory' => false, // true or false - storage backup only if s3 or google_drive enabled
    'check_access' => false, // Enable/disable role-based access to UI
    'allowed_roles' => [], // Role Names Example: ['Admin', 'Super-Admin','Developer', 'Manager']
];

Route::prefix('laravel-backup')
    ->middleware(['web', \Avcodewizard\LaravelBackup\Http\Middleware\CheckLaravelBackupAccess::class])
    ->group(function () {
        Route::get('/', [BackupController::class, 'index'])->name('laravel-backup.index');
        Route::get('/create', [BackupController::class, 'create'])->name('laravel-backup.create');
        Route::get('/download', [BackupController::class, 'download'])->name('laravel-backup.download');
        Route::delete('/delete', [BackupController::class, 'delete'])->name('laravel-backup.delete');
    });

use Illuminate\Support\Facades\Schedule;

Schedule::call(function () {
    Artisan::call('backup:run');
})->name('backup:run')->withoutOverlapping()->daily();

$schedule->command('backup:run')->daily();

if (!config('laravelBackup.check_access')) return $next($request);

$user = Auth::user();
if (!$user) {
    abort(403, 'Unauthorized - no user authenticated.');
}

if (!method_exists($user, 'hasRole')) {
    abort(403, 'User Role Not Implemented!');
}

if (!$user->hasAnyRole(config('laravelBackup.allowed_roles'))) {
    abort(403, 'Unauthorized - insufficient permission.');
}

return $next($request);

'destinations' => [
    's3' => [
        'enabled' => true,
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'bucket' => env('AWS_BUCKET'),
        'endpoint' => env('AWS_ENDPOINT'), // Optional: for MinIO, DO Spaces
        'path' => 'backups', // Folder in your bucket
    ],
],

'destinations' => [
    'google_drive' => [
        'enabled' => true,
        'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID'),
        'path' => 'backups',
    ],
],
bash
php artisan queue:work
bash
php artisan backup:run
bash
php artisan vendor:publish --tag=laravel-backup
bash
composer 
bash
php artisan backup:google-auth
bash
php artisan backup:google-auth --client-id=YOUR_CLIENT_ID --client-secret=YOUR_CLIENT_SECRET
bash
php artisan backup:google-auth