add postgresql (#118)

This commit is contained in:
Saeed Vaziry
2024-03-16 01:04:57 +01:00
committed by GitHub
parent a406491160
commit 287c9c2a8a
25 changed files with 366 additions and 150 deletions

View File

@ -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'
);
}
}

View File

@ -2,7 +2,10 @@
namespace App\SSH\Services\Database;
class Mariadb extends Mysql
class Mariadb extends AbstractDatabase
{
//
protected function getScriptsDir(): string
{
return 'mysql';
}
}

View File

@ -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';
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\SSH\Services\Database;
class Postgresql extends AbstractDatabase
{
protected function getScriptsDir(): string
{
return 'postgresql';
}
}

View 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

View 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"

View 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"

View 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"

View 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"

View 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();"

View 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();"

View 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();"

View 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();"

View 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();"

View 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"

View 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

View 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"