Can not apply from view Create an ajax function to be called by api if you don't want to refresh the page
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?
@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
@Sinnbeck i have read this but dont know how to call comand function manually onmy view
@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.
@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
@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)
@Sinnbeck If i do this i get
Unable to prepare route [test-backup] for serialization. Uses Closure.
on terminal when i run route:cache
@usertxr Yes of course.. But why are you trying to cache right now? Where are just testing stuff..
@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?
@usertxr You dont want to cache in developement.. Remove the caching and then try php artisan route:clear
@Sinnbeck Okay. I have done. What i get from this
Route::get('test-backup', function() {
$exitCode = Artisan::call('backup:run');
dd($exitCode);
});
1
@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
@Sinnbeck Is no error on laravel.log
@Sinnbeck just print this on logs
[2022-02-08 15:47:42] local.ERROR: 1
If i try
Log::error($exitCode);
@usertxr try this
Route::get('test-backup', function() {
$exitCode = Artisan::call('backup:run');
dd($exitCode, Artisan::output());
});
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();
});
@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 .
@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?
@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 I dont need to make backup automaticaly. I need to make when i press the backup button on site admin section
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');
@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
@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'),
]) : [],
],
@anilkumarthakur60 Its same even if i change to this configuration
@usertxr show me your route, view, controller file ...
Route::get('test-backup', function() {
$exitCode = Artisan::call('backup:run');
});
This is route. Still controler or view blade. Just calling route to url
@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.