Skip to content

Commit

Permalink
Create SinglestoreBackup class
Browse files Browse the repository at this point in the history
  • Loading branch information
miguilimzero committed Mar 14, 2023
1 parent a219357 commit 1520d36
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 50 deletions.
58 changes: 8 additions & 50 deletions src/Console/SinglestoreBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Srdante\LaravelSinglestoreBackup\SinglestoreBackup;

class SinglestoreBackupCommand extends Command
{
Expand Down Expand Up @@ -39,29 +40,15 @@ public function handle()
*/
$this->warn('Starting backup... This might take a while.');

[$database, $bucket, $config, $credentials] = $this->getParameters();

$with = '';
if ($this->option('init')) {
$with = 'WITH INIT';
}
if ($this->option('differential')) {
$with = 'WITH DIFFERENTIAL';
}

$timeout = '';
if ($this->option('timeout')) {
$timeout = "TIMEOUT {$this->option('timeout')}";
}

/*
* Do backup query
*/
$rawQuery = "BACKUP DATABASE {$database} {$with} TO S3 ? {$timeout} CONFIG ? CREDENTIALS ?";
$rawQuery = preg_replace('/\s+/', ' ', $rawQuery);
$singlestoreBackup = new SinglestoreBackup(
$this->option('init'),
$this->option('differential'),
$this->option('timeout'),
$this->option('multipart_chunk_size_mb'),
);

try {
$result = DB::select($rawQuery, [$bucket, $config, $credentials]);
$result = $singlestoreBackup->executeQuery();
} catch (QueryException $e) {
$this->error('Backup failed. Please check your SingleStore backup credentials.');
$this->error($e->getMessage());
Expand All @@ -73,33 +60,4 @@ public function handle()

return Command::SUCCESS;
}

/**
* Get query binding parameters.
*
* @return array
*/
public function getParameters()
{
$config = [
'endpoint_url' => (string) config('singlestore-backup.endpoint'),
];

if ($this->option('multipart_chunk_size_mb')) {
$config[]['multipart_chunk_size_mb'] = $this->option('multipart_chunk_size_mb');
}

return [
(string) config('database.connections.singlestore.database'),

(string) config('singlestore-backup.bucket'),

json_encode($config),

json_encode([
'aws_access_key_id' => (string) config('singlestore-backup.access_key'),
'aws_secret_access_key' => (string) config('singlestore-backup.secret_key'),
]),
];
}
}
78 changes: 78 additions & 0 deletions src/SinglestoreBackup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Srdante\LaravelSinglestoreBackup;

use Illuminate\Support\Facades\DB;

class SinglestoreBackup
{
/**
* Construct method.
*/
public function __construct(
protected bool $init = false,
protected bool $differential = false,
protected ?int $timeout = null,
protected ?int $multipartChunkSizeMb = null
) {
if ($init && $differential) {
throw new \InvalidArgumentException('You can\'t use "$init" and "$differential" attributes at the same time.');
}
}

/**
* Generate query and execute it.
*/
public function executeQuery(): array
{
// Get parameters
[$database, $bucket, $config, $credentials] = static::getParameters();

// Mount statements
$with = '';
if ($this->init) {
$with = 'WITH INIT';
}
if ($this->differential) {
$with = 'WITH DIFFERENTIAL';
}

$timeoutStatement = '';
if ($this->timeout) {
$timeoutStatement = "TIMEOUT {$this->timeout}";
}

// Mount query
$rawQuery = "BACKUP DATABASE {$database} {$with} TO S3 ? {$timeoutStatement} CONFIG ? CREDENTIALS ?";
$rawQuery = preg_replace('/\s+/', ' ', $rawQuery);

return DB::select($rawQuery, [$bucket, $config, $credentials]);
}

/**
* Get query binding parameters.
*/
protected function getParameters(): array
{
$config = [
'endpoint_url' => (string) config('singlestore-backup.endpoint'),
];

if ($this->multipartChunkSizeMb) {
$config[]['multipart_chunk_size_mb'] = $this->multipartChunkSizeMb;
}

return [
(string) config('database.connections.singlestore.database'),

(string) config('singlestore-backup.bucket'),

json_encode($config),

json_encode([
'aws_access_key_id' => (string) config('singlestore-backup.access_key'),
'aws_secret_access_key' => (string) config('singlestore-backup.secret_key'),
]),
];
}
}

0 comments on commit 1520d36

Please sign in to comment.