Skip to content

Instantly share code, notes, and snippets.

@mah3uz
Created January 16, 2018 06:21
Show Gist options
  • Save mah3uz/09d8d009290359b023420583082dca63 to your computer and use it in GitHub Desktop.
Save mah3uz/09d8d009290359b023420583082dca63 to your computer and use it in GitHub Desktop.
Backup, clean, list, and monitor multiple sites.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
class BackupCommand extends Command
{
protected $sites = [];
protected $signature = 'app:backup {--clean} {--list} {--monitor} {--run}';
protected $description = 'Backup all the sites';
public function handle()
{
if ($this->isSingleSiteCommand()) {
$this->runSingleSiteCommand();
return;
}
$this->runMultiSiteCommand();
}
protected function isSingleSiteCommand()
{
return in_array($this->backupType(), ['clean', 'run']);
}
protected function runSingleSiteCommand()
{
collect($this->sites)->each(function ($site) {
$this->setupSingleSiteConfig($site);
$this->callBackupCommand();
});
}
protected function runMultiSiteCommand()
{
$this->setupMultiSiteConfig();
$this->callBackupCommand();
}
protected function setupSingleSiteConfig($site)
{
Config::set(
'backup.backup.name',
$this->siteName($site)
);
Config::set(
'database.connections.mysql.database',
$site['database']
);
Config::set(
'backup.backup.source.files.include',
$this->siteIncludes($site)
);
}
protected function setupMultiSiteConfig()
{
Config::set(
'backup.monitorBackups',
$this->configForMultiSite()
);
}
protected function configForMultiSite()
{
$template = Config::get('backup.monitorBackups.0');
return array_map(function ($site) use ($template) {
return array_merge($template, [
'name' => $this->siteName($site),
]);
}, $this->sites);
}
protected function siteName($site)
{
return 'https://'.$site['domain'];
}
protected function siteIncludes($site)
{
return array_map(function ($path) use ($site) {
return base_path("../{$site['domain']}/$path");
}, $site['paths']);
}
protected function callBackupCommand()
{
$this->call('backup:'.$this->backupType());
}
protected function backupType()
{
return collect($this->options())
->only(['run', 'clean', 'list', 'monitor'])
->filter()
->keys()
->first() ?? 'run';
}
}
@mah3uz
Copy link
Author

mah3uz commented Jan 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment