Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

How to call backup:run from view

Hello, i am using this https://spatie.be/docs/laravel-backup/v5/installation-and-setup package to make backup for my site. Every think work okay and if i run backup:run on CLI It save me .zip on directory what i have chose to save backup db and system. What i want now is to call this function by a view, and iam looking best way to do this from view. Any idea about this? Should i use route?

0 likes
29 replies
samehdev's avatar

Can not apply from view Create an ajax function to be called by api if you don't want to refresh the page

Maison012's avatar

@samehdev Its not problem if page will be reload or refresh. I just need a way to call php artisan backup:run from a button witch is on my site dashboard

Maison012's avatar

@Sinnbeck i have read this but dont know how to call comand function manually onmy view

Sinnbeck's avatar

@usertxr You dont call it in the view. You have a form that submits to a controller, and in that controller you call the command.

Maison012's avatar

@Sinnbeck And what if i call Artisan::call('backup:run') directly from controller with route? I have tryed inthis way and i see it work but dont save backedup file on directory

Sinnbeck's avatar

@usertxr To test it out you can make a test endpoint like this

Route::get('test-backup', function() {
    $exitCode = Artisan::call('backup:run');
    dd($exitCode);
});

Call the url in the browser, and see what error code it dumps (hopefully 0)

Maison012's avatar

@Sinnbeck If i do this i get

 Unable to prepare route [test-backup] for serialization. Uses Closure.

on terminal when i run route:cache

Sinnbeck's avatar

@usertxr Yes of course.. But why are you trying to cache right now? Where are just testing stuff..

Maison012's avatar

@Sinnbeck Couse When i try do call test-backup on url i found nothing. How can i use test-backup if i dont run php artisan route:cache before?

Sinnbeck's avatar

@usertxr You dont want to cache in developement.. Remove the caching and then try php artisan route:clear

1 like
Maison012's avatar

@Sinnbeck Okay. I have done. What i get from this

Route::get('test-backup', function() {
    $exitCode = Artisan::call('backup:run');
    dd($exitCode);
});
1
Sinnbeck's avatar

@usertxr Good. Now you at least know that it fails.. I cannot think of a way to see the error. Perhaps there is a way to dump it. Or check the laravel.log file

Maison012's avatar

@Sinnbeck just print this on logs

[2022-02-08 15:47:42] local.ERROR: 1  

If i try

Log::error($exitCode);
Sinnbeck's avatar

@usertxr try this

Route::get('test-backup', function() {
    $exitCode = Artisan::call('backup:run');
    dd($exitCode, Artisan::output());
}); 
anilkumarthakur60's avatar

if the project is hosted then create cron jobs command and setup depending on your requirement

/usr/bin/php /home/user_name/public_html/artisan backup:run
https://docs.cpanel.net/cpanel/advanced/cron-jobs/

or you can create the route that run artisan command for eg

Route::get('backup-run',function(){
Artisan::call('backup:run');
return back();
});
Maison012's avatar

@anilkumarthakur60 I have tryed to add a route to call my artisan function. And it not worikg as i expect. I mean comand can executed but it not save backup file on direcory \storage\app\backup .

samehdev's avatar

@usertxr Is the file saved somewhere else? Have you tried calling it through the terminal and does it work or does an error appear in the terminal?

Maison012's avatar

@samehdev No is not saved anywhere else. I have tryed by Terminal and if i do from terminal , it save file on directory i show you below

anilkumarthakur60's avatar

@usertxr

view file

<a href="{{route('backuprun')}}"  class="btn btn-sm btn-success"> Backup Run</a>

route file

Route::get('backup-run',function(){
Artisan::call('backup:run');
return back();
})->name('backuprun');
Maison012's avatar

@anilkumarthakur60 I have tryed in this way before and i get same error. When i run php artisan route:cache

 Unable to prepare route [backup-run] for serialization. Uses Closure.

Also if i call backup-run without route:cache . Function is done but is not saving backedup files to my directory

anilkumarthakur60's avatar

@usertxr you need to modify the database.php

  'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'dump' => [
                'dump_binary_path' => 'C:\xampp\mysql\bin', // only the path, so without `mysqldump` or `pg_dump`
                'use_single_transaction',
                'timeout' => 60 * 5, // 5 minute timeout


            ],
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
Maison012's avatar

@anilkumarthakur60

Route::get('test-backup', function() {
    $exitCode = Artisan::call('backup:run');
});

This is route. Still controler or view blade. Just calling route to url

Maison012's avatar

@sinnbeck @anilkumarthakur60 @samehdev I found this error using clocwork > premission denied

Spatie\Backup\Events\BackupHasFailed exception: ErrorExceptionbackupDestination: null
exception: ErrorException
*message: "mkdir(): Permission denied"
~string: ""
*code: 0
*file: "/srv/www/htdocs/crm/vendor/spatie/temporary-directory/src/TemporaryDirectory.php"
*line: 43
~trace: Array(66)
~previous: null
*severity: 2
backupDestination: null

Also response time 1434 ms RESPONSE TIME

Please or to participate in this conversation.