Created
January 16, 2018 06:21
-
-
Save mah3uz/09d8d009290359b023420583082dca63 to your computer and use it in GitHub Desktop.
Backup, clean, list, and monitor multiple sites.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://timacdonald.me/backup-multiple-sites-frameworks-laravel-backup/