Skip to content

Instantly share code, notes, and snippets.

@Plytas
Last active April 27, 2019 14:55
Show Gist options
  • Select an option

  • Save Plytas/c416c55201e5a864e2ed69328944c633 to your computer and use it in GitHub Desktop.

Select an option

Save Plytas/c416c55201e5a864e2ed69328944c633 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Hyn\Tenancy\Contracts\Database\PasswordGenerator;
use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository;
use Illuminate\Config\Repository as Config;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
class TenantBackupCommand extends Command
{
use ConfirmableTrait;
/** @var PasswordGenerator */
protected $passwordGenerator;
/** @var Config */
protected $config;
/** @var WebsiteRepository */
private $websites;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tenant:backup {--force : Force the operation to run when in production.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Backups tenant databases';
public function __construct(PasswordGenerator $passwordGenerator, Config $config)
{
$this->websites = app(WebsiteRepository::class);
$this->passwordGenerator = $passwordGenerator;
$this->config = $config;
parent::__construct();
}
public function handle()
{
$websites = $this->websites->query()->get();
$system_config = $this->config->get('database.connections.system');
foreach ($websites as $website)
{
$website_config = $system_config;
$website_config['database'] = $website->uuid;
$website_config['username'] = $website->uuid;
$website_config['password'] = $this->passwordGenerator->generate($website);
$this->config->set('database.connections.'.$website->uuid, $website_config);
$this->config->push('backup.backup.source.databases', $website->uuid);
}
$this->call('backup:run');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment