PHP code example of amdadulhaq / database-backup-laravel

1. Go to this page and download the library: Download amdadulhaq/database-backup-laravel 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/ */

    

amdadulhaq / database-backup-laravel example snippets


// config/filesystems.php
's3' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
],

'r2' => [
    'driver' => 's3',
    'key' => env('R2_ACCESS_KEY_ID'),
    'secret' => env('R2_SECRET_ACCESS_KEY'),
    'region' => 'auto',
    'bucket' => env('R2_BUCKET'),
    'endpoint' => env('R2_ENDPOINT'), // https://<account-id>.r2.cloudflarestorage.com
    'use_path_style_endpoint' => true,
],

'minio' => [
    'driver' => 's3',
    'key' => env('MINIO_ACCESS_KEY'),
    'secret' => env('MINIO_SECRET_KEY'),
    'region' => 'us-east-1',
    'bucket' => env('MINIO_BUCKET'),
    'endpoint' => env('MINIO_ENDPOINT'), // http://minio:9000
    'use_path_style_endpoint' => true,
],

'spaces' => [
    'driver' => 's3',
    'key' => env('DO_SPACES_KEY'),
    'secret' => env('DO_SPACES_SECRET'),
    'region' => env('DO_SPACES_REGION'), // e.g. nyc3
    'bucket' => env('DO_SPACES_BUCKET'),
    'endpoint' => env('DO_SPACES_ENDPOINT'), // https://nyc3.digitaloceanspaces.com
],

'b2' => [
    'driver' => 's3',
    'key' => env('B2_KEY_ID'),
    'secret' => env('B2_APP_KEY'),
    'region' => env('B2_REGION'), // e.g. us-west-004
    'bucket' => env('B2_BUCKET'),
    'endpoint' => env('B2_ENDPOINT'), // https://s3.us-west-004.backblazeb2.com
],

use Illuminate\Support\Facades\Schedule;

Schedule::command('db:backup')->dailyAt('02:00')->withoutOverlapping();

use AmdadulHaq\DatabaseBackup\Events\BackupFailed;
use Illuminate\Support\Facades\Event;

Event::listen(BackupFailed::class, function (BackupFailed $event): void {
    logger()->error("DB backup failed for {$event->connection}", [
        'error' => $event->exception->getMessage(),
    ]);
});
bash
php artisan vendor:publish --tag=database-backup-config
bash
# restore the most recent backup for the connection
php artisan db:restore --latest

# restore a specific file from the disk
php artisan db:restore backups/pgsql/pgsql-app-2026-09-01_020000.sql.gz

# skip the "this overwrites the database" prompt (for scripts)
php artisan db:restore --latest --force
bash
DB_BACKUP_IT_DRIVER=pgsql DB_BACKUP_IT_PORT=5432 DB_BACKUP_IT_DATABASE=backup_it \
DB_BACKUP_IT_USERNAME=postgres DB_BACKUP_IT_PASSWORD=secret \
vendor/bin/pest tests/IntegrationTest.php