-
-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backup Multitenant DBS #926
Comments
It depends how your environment is configured. Can you post your config? |
atymic... I too faced the similar problem... I have multiple databases sharing one connection... |
Hey guys. I have a table with the list of all my clients in a "main" bank with the client name and the database name of it.
So I make a foreach with each client and initiate the connection with his bank and I make the call to the artisan to run the backup: run for this client.
I hope this helps. |
For Hyn\MultiTenant 5.4, to do backups of the entire app and all registered tenant databases I created the following custom command which calls a custom BackupJobFactory which overrides the default factory which only does one database, and instead calls a custom TenantDbSelector which loops through and processes all the tenant dbs. Feel free to use it for inspiration for your own situation. namespace App\Console\Commands;
use App\Services\BackupJobFactory;
use Exception;
use Spatie\Backup\Commands\BackupCommand as SpatieBackupCommand;
use Spatie\Backup\Events\BackupHasFailed;
class BackupCommand extends SpatieBackupCommand
{
protected $signature = 'backup:tenants {--disable-notifications}';
public function handle()
{
consoleOutput()->comment('Starting backup...');
$disableNotifications = $this->option('disable-notifications');
try {
$backupJob = BackupJobFactory::createFromArray(config('backup'));
if ($disableNotifications) {
$backupJob->disableNotifications();
}
$backupJob->run();
consoleOutput()->comment('Backup completed!');
} catch (Exception $exception) {
consoleOutput()->error("Backup failed because: {$exception->getMessage()}.");
if (! $disableNotifications) {
event(new BackupHasFailed($exception));
}
return 1;
}
}
} <?php
namespace App\Services;
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
use Spatie\Backup\Tasks\Backup\BackupJob;
use Spatie\Backup\Tasks\Backup\BackupJobFactory as SpatieBackupJobFactory;
class BackupJobFactory extends SpatieBackupJobFactory
{
public static function createFromArray(array $config): BackupJob
{
return (new BackupJob())
->setFileSelection(static::createFileSelection($config['backup']['source']['files']))
->setDbDumpers(
static::createDbDumpers($config['backup']['source']['databases'])
->merge(TenantBackupDbSelector::getTenantDatabaseConnections())
)
->setBackupDestinations(BackupDestinationFactory::createFromArray($config['backup']));
}
} <?php
namespace App\Services;
use Hyn\Tenancy\Models\Website;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Backup\Tasks\Backup\DbDumperFactory;
use Spatie\DbDumper\Databases\MySql;
use Spatie\DbDumper\DbDumper;
class TenantBackupDbSelector extends DbDumperFactory
{
public static function getTenantDatabaseConnections(): Collection
{
$websites = Website::all();
return $websites->map(function ($tenant) {
return self::createConnection($tenant->uuid);
});
}
public static function createConnection(string $tenantDbName): DbDumper
{
$dbConfig = config('database.connections.' . config('tenancy.db.system-connection-name', 'system'));
$dbDumper = static::forDriver($dbConfig['driver'])
->setHost(Arr::first(Arr::wrap($dbConfig['host'] ?? '')))
->setDbName($tenantDbName)
->setUserName($dbConfig['username'] ?? '')
->setPassword($dbConfig['password'] ?? '');
if ($dbDumper instanceof MySql) {
$dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? '');
}
if (isset($dbConfig['port'])) {
$dbDumper = $dbDumper->setPort($dbConfig['port']);
}
if (isset($dbConfig['dump'])) {
$dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper);
}
return $dbDumper;
}
} I also enabled encryption using the approach suggested by @skollro here: https://simonkollross.de/posts/creating-encrypted-backups-of-laravel-apps |
Dear contributor, because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it. |
@drbyte , thanks for your code. It helped me a lot.
|
That's lifesaver bro thanks! |
@bsormagec |
|
Master Database plus all tenants database backup.`<?php namespace App\Console\Commands; use App\Models\Tenant;
} |
is there a way to backup multi tenant databases, keeping in mind that there is only one database connection
The text was updated successfully, but these errors were encountered: