PHP code example of onamfc / laravel-database-archiver

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

    

onamfc / laravel-database-archiver example snippets


'storage' => [
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'bucket' => env('DB_ARCHIVER_S3_BUCKET'),
    ],
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app/archives'),
    ],
],

'tables' => [
    'users' => [
        'enabled' => true,
        'criteria' => [
            'column' => 'created_at',
            'operator' => '<',
            'value' => '6 months ago',
        ],
        'format' => 'json',
        'storage' => 's3',
        'path' => 'archives/users/{date}',
        'schedule' => 'daily',
        'delete_after_archive' => false,
    ],
    'logs' => [
        'enabled' => true,
        'criteria' => [
            'column' => 'created_at',
            'operator' => '<',
            'value' => '3 months ago',
        ],
        'format' => 'parquet',
        'storage' => 's3',
        'path' => 'archives/logs/{date}',
        'schedule' => 'weekly',
        'delete_after_archive' => true,
        'additional_criteria' => [
            ['column' => 'level', 'operator' => '=', 'value' => 'debug'],
        ],
    ],
],

use YourVendor\LaravelDbArchiver\Services\ArchiveService;

class YourController extends Controller
{
    public function archiveData(ArchiveService $archiveService)
    {
        // Archive a specific table
        $result = $archiveService->archiveTable('users');
        
        // Archive all configured tables
        $results = $archiveService->archiveAll();
        
        // Get status
        $status = $archiveService->getStatus();
        
        return response()->json($result);
    }
}

'criteria' => [
    'column' => 'created_at',
    'operator' => '<',
    'value' => '6 months ago', // Carbon-parseable string
],

// Or use a closure for complex logic
'criteria' => [
    'column' => 'status',
    'operator' => '=',
    'value' => function () {
        return config('app.archive_status');
    },
],

'additional_criteria' => [
    ['column' => 'status', 'operator' => '=', 'value' => 'inactive'],
    ['column' => 'last_login', 'operator' => '<', 'value' => '1 year ago'],
],

'path' => 'archives/{table}/{date}', // archives/users/2024-01-15
'path' => 'backups/{table}/year={Y}/month={m}', // backups/users/year=2024/month=01

'storage' => [
    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('DB_ARCHIVER_S3_BUCKET'),
        'endpoint' => env('AWS_ENDPOINT'), // For S3-compatible services
    ],
],

'storage' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app/archives'),
    ],
],

$logs = \YourVendor\LaravelDbArchiver\Models\ArchiveLog::where('table_name', 'users')
    ->latest()
    ->get();

'logging' => [
    'enabled' => true,
    'channel' => 'daily',
    'level' => 'info',
],
bash
php artisan vendor:publish --tag=db-archiver-config
bash
php artisan vendor:publish --tag=db-archiver-migrations
php artisan migrate