Laravel backup with spatie/laravel-backup - There is no disk set for the backup destination - Google Cloud Storage - laravel

I'm unable to backup to GCS from Laravel app
using spatie/laravel-backup
$ php artisan backup:run
Starting backup...
Dumping database reviewbooster...
Determining files to backup...
Zipping 720 files...
Created zip containing 720 files. Size is 29.86 MB
Copying zip to disk named gcs...
Copying zip failed because: There is no disk set for the backup destination.
Backup completed!
filesystems.php
'gcs' => [
'driver' => 'gcs',
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'project-id'),
'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
'visibility' => 'private', // optional: public|private
],
backup.php
'destination' => [
/*
* The filename prefix used for the backup zip file.
*/
'filename_prefix' => '',
/*
* The disk names on which the backups will be stored.
*/
'disks' => [
'gcs',
],
],

I just had this same problem.
For me, the root cause was that my .env file had incorrect variables names that didn't match what Laravel expected in config/filesystems.php.
For example, I had:
AWS_KEY=xxx
AWS_SECRET=xxx
AWS_REGION=us-east-1
AWS_BUCKET=xxx
instead of:
AWS_ACCESS_KEY_ID=xxx
AWS_SECRET_ACCESS_KEY=xxx
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=xxx
P.S. Once I fixed that, I tried running the backup again and got a new error (403 Forbidden, Access Denied, Error executing "PutObject").
So then I visited https://console.aws.amazon.com/iam/home?region=us-east-1#/users/ and clicked "Add inline policy" for the relevant user. (See this answer.)

Related

Why is the array empty on get allFiles with Laravel storage

I have an Laravel application witch works with an ftp server. Till now, I used the ftp service from hostpoint.ch. Everything works well.
Now, the customer like to use his own ftp server. So I created a new disk for the new ftp.
'ftp_customer' => [
'driver' => 'ftp',
'host' => 'xxx.xxx.xx.x',
'username' => 'xxx',
'password' => 'xxx',
'port' => 21,
'passive' => true,
'root'=> '/',
],
To test the server, I created the following script:
Storage::disk('ftp_customer')->put('file1.txt', 'Contents');
$files = Storage::disk('ftp_customer')->allFiles();
dd($files);
On the server, the file1.txt exists:
-rw-rw-rw- 1 ftp ftp 8 Dec 20 13:43 file1.txt
Therefore, I know, that the put method works.
But the get allFiles returns always an empty array.
I tried to create first a folder and use it. Also, I tried to change the config.
I'm not sure if it creates problems, because it's a Windows server.
Does anyone have an idea how to solve it?

Laravel Storage not storing created PDF files

I am facing an issue where my created PDF's are not being stored in laravel.
I have the following set in my filesystems.php under the disks tag
'completedforms' => [
'driver' => 'local',
'root' => storage_path('app/storage/completedforms'),
],
In my controller I have the following:
$pdf = PDF::loadView('report.form19', compact('generalreport','monthlyRoll', 'nightsInMonth', 'groupfee', 'subs', 'wing','weeksinmonth', 'meetingnights', 'lastRollMap', 'month_name', 'totalmember', 'totalcadets', 'totalnco', 'totalto', 'totalofficer'));
Storage::disk('completedforms')->put('Form 19 - '.$month_name . ' ' . $lastRollMap->roll_year.'.pdf', $pdf->output());
return $pdf->download ('Form 19 - '.$month_name . ' ' . $lastRollMap->roll_year.'.pdf');
I have added use Illuminate\Support\Facades\Storage; to my Controller
The pdf is created and downloads no issues, however the file is not being saved in the located as difined for the disk in the filesystems.php.
I have created the folder manually to ensure it exists. I have even set the file name to test.php to remove any variables fromm the file name
storage_path already starts at: /storage,
so change it like so:
'root' => storage_path('/desired'),

Error: Unsupported DB driver for windows and wamp

I already config my .env file and db.php file for craft with the same information, before i used 'mysql' as driver but i try it as empty and throws the same error.
db.php
<?php
/**
* Database Configuration
*
* All of your system's database connection settings go in here. You can see a
* list of the available settings in vendor/craftcms/cms/src/config/DbConfig.php.
*
* #see craft\config\DbConfig
*/
return [
'driver' => getenv(''),
'server' => getenv('localhost'),
'user' => getenv('root'),
'password' => getenv('****'),
'database' => getenv('craftyblog'),
'schema' => getenv(''),
'tablePrefix' => getenv(''),
'port' => getenv('')
];
.env
# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="dev"
# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY="******"
# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER=""
# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"
# The database username to connect with
DB_USER="root"
# The database password to connect with
DB_PASSWORD="****"
# The name of the database to select
DB_DATABASE="craftyblog"
# The database schema that will be used (PostgreSQL only)
DB_SCHEMA=""
# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""
# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT=""
DEFAULT_SITE_URL=""
And i'm using WAMP with this versions:
PHP 7.1.16
Apache 2.4.33
MySQL 5.7.21
I expect solve the problem, thank you.
In your config.php, it appears you're trying to pull in environment variables via getenv(), but you're passing along the actual values you want to use as strings to the getenv() function instead of the environment variable name. The values are set in the .env file so it's more portable for collaborative developers.
In that .env file, there isn't have an environment variable set for the database driver, so you can just pass a string to 'driver' instead in config.php.
If you'd like to pull the values from your .env file, pass the variable names as strings for the environment variables in the expected format for getenv(), like so:
config.php
return [
'driver' => 'mysql',
'server' => getenv('DB_SERVER'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'database' => getenv('DB_DATABASE'),
'schema' => getenv('DB_SCHEMA'),
'tablePrefix' => getenv('DB_TABLE_PREFIX'),
'port' => getenv('DB_PORT')
];
Your .env file already seems setup for everything, so you should be good to go. However, to use the values from the .env file in the config.php file, you're going to need to pass the variable names as strings. Hope this helps!

Laravel change filesystem disks path on run time

I am aware of the filesystems.php to create disks and I'm currently using it, having ~~ 20 disks configured.
I have a new problem with these, I'm currently trying to prefix to every disk, a string. The problem is that the paths are being saved when the php artisan config:cache is run but I need to change them on run time, as n example, for User Sergio it would need to append Sergio/ to the following disk for example:
//filesystems.php
'random' => [
'driver' => 'local',
'root' => storage_path('app/random'),
],
Then
Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
//outputs /var/www/html/project/storage/app/random
and the goal is setting configurations in for example the middleware i'm currently setting the tentant database already like this
//Middleware
Config::set('database.connections.tenant.database', "Sergio");
DB::reconnect('tenant');
I can currently set the paths correctly with
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
But i'm worried since that if before that line I try to reach to the path, the storage saves the initial path in memory instead of re-fetching it after it is altered.
For example. doing this without any middleware.
$fullPath1 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
Config::set('filesystems.disks.random.root',storage_path('app/Sergio/random'));
$fullPath2 = Storage::disk("random")->getDriver()->getAdapter()->getPathPrefix();
What was intended to happen is that $fullPath1 would output the initial path which is /var/www/html/project/storage/app/random and then $fullPath2 would output /var/www/html/project/storage/app/Sergio/random
Is there any way of letting the Storage know that I've changed the disks local paths?
How about adding a new config instead of updating the already loaded one, something like this:
private function addNewDisk(string $diskName)
{
config(['filesystems.disk.' . $diskName => [
'driver' => 'local',
'root' => storage_path('app/' . $diskName),
]]);
}
and prior to using the Storage facade, call above method that way the config will be updated and when you use new disk, it will try to resolve again based on updated config.
{
....
$this->addNewDisk('new_random');
Storage::disk('new_random')->get('abc.txt'); // or any another method
...
}

Multi storage paths

I have server with 2 hdd. I want to use one for all media files, which are on storage/app/media for current media files and other for all files before 2018.
How can I switch storage path depends on query I make.
All media are saved on media table. I can make changes on table if needed.
In your config\filesystem.php update disk array and add your new disks like this:
'public_two' => [
'driver' => 'local',
'root' => storage_path('app/public/public_two'),
'url' => env('APP_URL').'/storage/public_two',
'visibility' => 'public',
],
and you call your diks like this: Storage::disk('public_two');
Please be free to read the FileSystem Documentation

Resources