Backup using Spatie Multitenant Package #1502
-
Hi, is there a way to backup multi tenant databases, keeping in mind that i'm using Spatie laravel-multitenancy package with multi databases approach. Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I have just fixed the issue. Create Task file, in order to change backup databases & dilename_prefix parameter on-the-fly.
<?php
namespace App\Tasks;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
class SwitchBackupDatabaseTask implements SwitchTenantTask
{
public function __construct(protected ?array $originalBackupDatabase= null, protected ?string $originalBackupFilenamePrefix= null)
{
$this->originalBackupDatabase??= config('backup.backup.source.databases');
$this->originalBackupFilenamePrefix??= config('backup.backup.destination.filename_prefix');
}
public function makeCurrent(Tenant $tenant): void
{
config()->set('backup.backup.source.databases', ['tenant']);
config()->set('backup.backup.destination.filename_prefix', $tenant->database . '-');
}
public function forgetCurrent(): void
{
config()->set('backup.backup.source.databases', $this->originalBackupDatabase);
config()->set('backup.backup.destination.filename_prefix', $this->originalBackupFilenamePrefix);
}
} Then publishing this task file on multitenancy config file.
'switch_tenant_tasks' => [
// add tasks here
....
App\Tasks\SwitchBackupDatabaseTask::class,
....
],
'databases' => [
'landlord',
], 'filename_prefix' => 'landlord-',
Then you can backup your app by running:
Thank you |
Beta Was this translation helpful? Give feedback.
-
In extend to @sys-auditing's solution one could change the backup name instead of prefixing the files. Task<?php
namespace App\Multitenancy\Tasks;
use Illuminate\Support\Facades\Config;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
class SwitchBackupDatabaseTask implements SwitchTenantTask
{
public function __construct(
protected ?string $originalBackupName = null,
protected ?array $originalBackupDatabase = null,
)
{
$this->originalBackupName ??= Config::get('backup.backup.name');
$this->originalBackupDatabase ??= Config::get('backup.backup.source.databases');
}
public function makeCurrent(Tenant $tenant): void
{
Config::set('backup.backup.name', $tenant->slug);
Config::set('backup.backup.source.databases', ['tenant']);
}
public function forgetCurrent(): void
{
Config::set('backup.backup.name', $this->originalBackupName);
Config::set('backup.backup.source.databases', $this->originalBackupDatabase);
}
} config/backup.php<?php
return [
'backup' => [
/*
* The name of this application. You can use this name to monitor
* the backups.
*/
'name' => 'landlord',
// ..
] Custom commands(adjust as needed, I only run backups for databases in these commands) Artisan::command('backup-all', function () {
$this->call('backup:run', ['--only-db' => true]);
$this->call('tenants:artisan', ['artisanCommand' => 'backup:run --only-db']);
});
Artisan::command('cleanup-backups', function () {
$this->call('backup:clean');
$this->call('tenants:artisan', ['artisanCommand' => 'backup:clean']);
}); |
Beta Was this translation helpful? Give feedback.
I have just fixed the issue.
Create Task file, in order to change backup databases & dilename_prefix parameter on-the-fly.
\\SwitchBackupDatabaseTask.php