mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 22:46:16 +00:00
add postgresql (#118)
This commit is contained in:
@ -13,4 +13,14 @@ final class Database extends Enum
|
||||
const MYSQL80 = 'mysql80';
|
||||
|
||||
const MARIADB = 'mariadb';
|
||||
|
||||
const POSTGRESQL12 = 'postgresql12';
|
||||
|
||||
const POSTGRESQL13 = 'postgresql13';
|
||||
|
||||
const POSTGRESQL14 = 'postgresql14';
|
||||
|
||||
const POSTGRESQL15 = 'postgresql15';
|
||||
|
||||
const POSTGRESQL16 = 'postgresql16';
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
use App\Models\BackupFile;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use App\SSH\HasScripts;
|
||||
@ -15,6 +16,8 @@ abstract class AbstractDatabase implements Database, ServiceInterface
|
||||
|
||||
protected Server $server;
|
||||
|
||||
abstract protected function getScriptsDir(): string;
|
||||
|
||||
public function __construct(Service $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
@ -29,4 +32,115 @@ public function install(): void
|
||||
$status = $this->server->systemd()->status($this->service->unit);
|
||||
$this->service->validateInstall($status);
|
||||
}
|
||||
|
||||
public function create(string $name): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/create.sh', [
|
||||
'name' => $name,
|
||||
]),
|
||||
'create-database'
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(string $name): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/delete.sh', [
|
||||
'name' => $name,
|
||||
]),
|
||||
'delete-database'
|
||||
);
|
||||
}
|
||||
|
||||
public function createUser(string $username, string $password, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/create-user.sh', [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
'host' => $host,
|
||||
]),
|
||||
'create-user'
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteUser(string $username, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/delete-user.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
]),
|
||||
'delete-user'
|
||||
);
|
||||
}
|
||||
|
||||
public function link(string $username, string $host, array $databases): void
|
||||
{
|
||||
$ssh = $this->server->ssh();
|
||||
|
||||
foreach ($databases as $database) {
|
||||
$ssh->exec(
|
||||
$this->getScript($this->getScriptsDir().'/link.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
'database' => $database,
|
||||
]),
|
||||
'link-user-to-database'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function unlink(string $username, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/unlink.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
]),
|
||||
'unlink-user-from-databases'
|
||||
);
|
||||
}
|
||||
|
||||
public function runBackup(BackupFile $backupFile): void
|
||||
{
|
||||
// backup
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/backup.sh', [
|
||||
'file' => $backupFile->name,
|
||||
'database' => $backupFile->backup->database->name,
|
||||
]),
|
||||
'backup-database'
|
||||
);
|
||||
|
||||
// upload to storage
|
||||
$upload = $backupFile->backup->storage->provider()->ssh($this->server)->upload(
|
||||
$backupFile->path(),
|
||||
$backupFile->storagePath(),
|
||||
);
|
||||
|
||||
// cleanup
|
||||
$this->server->ssh()->exec('rm '.$backupFile->name.'.zip');
|
||||
|
||||
$backupFile->size = $upload['size'];
|
||||
$backupFile->save();
|
||||
}
|
||||
|
||||
public function restoreBackup(BackupFile $backupFile, string $database): void
|
||||
{
|
||||
// download
|
||||
$backupFile->backup->storage->provider()->ssh($this->server)->download(
|
||||
$backupFile->storagePath(),
|
||||
$backupFile->name.'.zip',
|
||||
);
|
||||
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript($this->getScriptsDir().'/restore.sh', [
|
||||
'database' => $database,
|
||||
'file' => $backupFile->name,
|
||||
]),
|
||||
'restore-database'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,10 @@
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
class Mariadb extends Mysql
|
||||
class Mariadb extends AbstractDatabase
|
||||
{
|
||||
//
|
||||
protected function getScriptsDir(): string
|
||||
{
|
||||
return 'mysql';
|
||||
}
|
||||
}
|
||||
|
@ -2,121 +2,10 @@
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
use App\Models\BackupFile;
|
||||
use App\SSH\HasScripts;
|
||||
|
||||
class Mysql extends AbstractDatabase
|
||||
{
|
||||
use HasScripts;
|
||||
|
||||
public function create(string $name): void
|
||||
protected function getScriptsDir(): string
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/create.sh', [
|
||||
'name' => $name,
|
||||
]),
|
||||
'create-database'
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(string $name): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/delete.sh', [
|
||||
'name' => $name,
|
||||
]),
|
||||
'delete-database'
|
||||
);
|
||||
}
|
||||
|
||||
public function createUser(string $username, string $password, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/create-user.sh', [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
'host' => $host,
|
||||
]),
|
||||
'create-user'
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteUser(string $username, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/delete-user.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
]),
|
||||
'delete-user'
|
||||
);
|
||||
}
|
||||
|
||||
public function link(string $username, string $host, array $databases): void
|
||||
{
|
||||
$ssh = $this->server->ssh();
|
||||
|
||||
foreach ($databases as $database) {
|
||||
$ssh->exec(
|
||||
$this->getScript('mysql/link.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
'database' => $database,
|
||||
]),
|
||||
'link-user-to-database'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function unlink(string $username, string $host): void
|
||||
{
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/unlink.sh', [
|
||||
'username' => $username,
|
||||
'host' => $host,
|
||||
]),
|
||||
'unlink-user-from-databases'
|
||||
);
|
||||
}
|
||||
|
||||
public function runBackup(BackupFile $backupFile): void
|
||||
{
|
||||
// backup
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/backup.sh', [
|
||||
'file' => $backupFile->name,
|
||||
'database' => $backupFile->backup->database->name,
|
||||
]),
|
||||
'backup-database'
|
||||
);
|
||||
|
||||
// upload to storage
|
||||
$upload = $backupFile->backup->storage->provider()->ssh($this->server)->upload(
|
||||
$backupFile->path(),
|
||||
$backupFile->storagePath(),
|
||||
);
|
||||
|
||||
// cleanup
|
||||
$this->server->ssh()->exec('rm '.$backupFile->name.'.zip');
|
||||
|
||||
$backupFile->size = $upload['size'];
|
||||
$backupFile->save();
|
||||
}
|
||||
|
||||
public function restoreBackup(BackupFile $backupFile, string $database): void
|
||||
{
|
||||
// download
|
||||
$backupFile->backup->storage->provider()->ssh($this->server)->download(
|
||||
$backupFile->storagePath(),
|
||||
$backupFile->name.'.zip',
|
||||
);
|
||||
|
||||
$this->server->ssh()->exec(
|
||||
$this->getScript('mysql/restore.sh', [
|
||||
'database' => $database,
|
||||
'file' => $backupFile->name,
|
||||
]),
|
||||
'restore-database'
|
||||
);
|
||||
return 'mysql';
|
||||
}
|
||||
}
|
||||
|
11
app/SSH/Services/Database/Postgresql.php
Normal file
11
app/SSH/Services/Database/Postgresql.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\Database;
|
||||
|
||||
class Postgresql extends AbstractDatabase
|
||||
{
|
||||
protected function getScriptsDir(): string
|
||||
{
|
||||
return 'postgresql';
|
||||
}
|
||||
}
|
19
app/SSH/Services/Database/scripts/postgresql/backup.sh
Normal file
19
app/SSH/Services/Database/scripts/postgresql/backup.sh
Normal file
@ -0,0 +1,19 @@
|
||||
if ! sudo -u postgres pg_dump -d __database__ -f /var/lib/postgresql/__file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo mv /var/lib/postgresql/__file__.sql /home/vito/__file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo chown vito:vito /home/vito/__file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! DEBIAN_FRONTEND=noninteractive zip __file__.zip __file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! rm __file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
5
app/SSH/Services/Database/scripts/postgresql/create-user.sh
Executable file
5
app/SSH/Services/Database/scripts/postgresql/create-user.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo -u postgres psql -c "CREATE ROLE __username__ WITH LOGIN PASSWORD '__password__';"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "User __username__ created"
|
6
app/SSH/Services/Database/scripts/postgresql/create.sh
Normal file
6
app/SSH/Services/Database/scripts/postgresql/create.sh
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
if ! sudo -u postgres psql -c "CREATE DATABASE __name__"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Database __name__ created"
|
5
app/SSH/Services/Database/scripts/postgresql/delete-user.sh
Executable file
5
app/SSH/Services/Database/scripts/postgresql/delete-user.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo -u postgres psql -c "DROP USER __username__"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "User __username__ deleted"
|
5
app/SSH/Services/Database/scripts/postgresql/delete.sh
Executable file
5
app/SSH/Services/Database/scripts/postgresql/delete.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo -u postgres psql -c "DROP DATABASE __name__"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Database __name__ deleted"
|
11
app/SSH/Services/Database/scripts/postgresql/install-12.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/install-12.sh
Normal file
@ -0,0 +1,11 @@
|
||||
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||
|
||||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-12 -y
|
||||
|
||||
systemctl status postgresql
|
||||
|
||||
sudo -u postgres psql -c "SELECT version();"
|
11
app/SSH/Services/Database/scripts/postgresql/install-13.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/install-13.sh
Normal file
@ -0,0 +1,11 @@
|
||||
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||
|
||||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-13 -y
|
||||
|
||||
systemctl status postgresql
|
||||
|
||||
sudo -u postgres psql -c "SELECT version();"
|
11
app/SSH/Services/Database/scripts/postgresql/install-14.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/install-14.sh
Normal file
@ -0,0 +1,11 @@
|
||||
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||
|
||||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-14 -y
|
||||
|
||||
systemctl status postgresql
|
||||
|
||||
sudo -u postgres psql -c "SELECT version();"
|
11
app/SSH/Services/Database/scripts/postgresql/install-15.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/install-15.sh
Normal file
@ -0,0 +1,11 @@
|
||||
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||
|
||||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-15 -y
|
||||
|
||||
systemctl status postgresql
|
||||
|
||||
sudo -u postgres psql -c "SELECT version();"
|
11
app/SSH/Services/Database/scripts/postgresql/install-16.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/install-16.sh
Normal file
@ -0,0 +1,11 @@
|
||||
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||
|
||||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y
|
||||
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install postgresql-16 -y
|
||||
|
||||
systemctl status postgresql
|
||||
|
||||
sudo -u postgres psql -c "SELECT version();"
|
5
app/SSH/Services/Database/scripts/postgresql/link.sh
Executable file
5
app/SSH/Services/Database/scripts/postgresql/link.sh
Executable file
@ -0,0 +1,5 @@
|
||||
if ! sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE __database__ TO __username__;"; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
echo "Linking to __database__ finished"
|
11
app/SSH/Services/Database/scripts/postgresql/restore.sh
Normal file
11
app/SSH/Services/Database/scripts/postgresql/restore.sh
Normal file
@ -0,0 +1,11 @@
|
||||
if ! DEBIAN_FRONTEND=noninteractive unzip __file__.zip; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo -u postgres psql -d __database__ -f __file__.sql; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! rm __file__.sql __file__.zip; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
10
app/SSH/Services/Database/scripts/postgresql/unlink.sh
Executable file
10
app/SSH/Services/Database/scripts/postgresql/unlink.sh
Executable file
@ -0,0 +1,10 @@
|
||||
USER_TO_REVOKE='__username__'
|
||||
|
||||
DATABASES=$(sudo -u postgres psql -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
|
||||
|
||||
for DB in $DATABASES; do
|
||||
echo "Revoking privileges in database: $DB"
|
||||
sudo -u postgres psql -d "$DB" -c "REVOKE ALL PRIVILEGES ON DATABASE \"$DB\" FROM $USER_TO_REVOKE;"
|
||||
done
|
||||
|
||||
echo "Privileges revoked from $USER_TO_REVOKE"
|
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Exceptions\SSHCommandError;
|
||||
use App\SSH\HasScripts;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Dropbox extends AbstractStorage
|
||||
{
|
||||
@ -23,7 +24,8 @@ public function upload(string $src, string $dest): array
|
||||
$data = json_decode($upload, true);
|
||||
|
||||
if (isset($data['error'])) {
|
||||
throw new SSHCommandError('Failed to upload to Dropbox '.$data['error']);
|
||||
Log::error('Failed to upload to Dropbox', $data);
|
||||
throw new SSHCommandError('Failed to upload to Dropbox');
|
||||
}
|
||||
|
||||
return [
|
||||
|
Reference in New Issue
Block a user