Skip to content
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

Spatie backup command #21

Open
bkintanar opened this issue Jun 21, 2018 · 0 comments
Open

Spatie backup command #21

bkintanar opened this issue Jun 21, 2018 · 0 comments

Comments

@bkintanar
Copy link
Member

Moved from tenancy/multi-tenant#354

Thanks to @Thijmen

We need to add this to the documentation.

<?php

namespace App\Console\Commands;

use Hyn\Tenancy\Database\Connection;
use Hyn\Tenancy\Models\Website;
use Illuminate\Console\Command;

class BackupCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:backup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Backup all the tenants and system';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->backupSystem();
        $sites = Website::all();
        foreach ($sites as $site) {
            $this->setupConfigForSite($site);
            $this->callBackupCommand();
        };
    }

    protected function backupSystem()
    {
        \Config::set('backup.backup.source.databases', ['system']);
        \Config::set('backup.backup.name', 'system-backup');
        $this->call('backup:run', ['--only-db']);
    }

    protected function setupConfigForSite(Website $website)
    {
        $connection = app(Connection::class);
        $connection->set($website);
        \Config::set('backup.backup.source.databases', ['tenant']);
        \Config::set('backup.backup.name', 'tenant-' . $website->uuid);
        \Config::set('backup.backup.source.files.include', $this->siteIncludes($website));
    }
    

    protected function siteIncludes(Website $website)
    {
        return storage_path('app/tenancy/tenants/' . $website->uuid);
    }

    protected function callBackupCommand()
    {
        $this->call('backup:run');
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants