structure

This commit is contained in:
fogelito
2023-07-12 14:55:09 +03:00
parent c6be81592f
commit 748abb29cf
9 changed files with 226 additions and 13 deletions
+5
View File
@@ -2,6 +2,9 @@
namespace Appwrite\Platform\Services;
use Appwrite\Platform\Tasks\Backup;
use Appwrite\Platform\Tasks\DbBackup;
use Appwrite\Platform\Tasks\DbRestore;
use Utopia\Platform\Service;
use Appwrite\Platform\Tasks\Doctor;
use Appwrite\Platform\Tasks\Install;
@@ -48,6 +51,8 @@ class Tasks extends Service
->addAction(CalcUsersStats::getName(), new CalcUsersStats())
->addAction(CalcTierStats::getName(), new CalcTierStats())
->addAction(PatchDeleteProjectCollections::getName(), new PatchDeleteProjectCollections())
->addAction(DbBackup::getName(), new DbBackup())
->addAction(DbRestore::getName(), new DbRestore())
;
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\Platform\Action;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Validator\Hostname;
class DbBackup extends Action
{
protected string $directory = '/var/lib/mysql';
public static string $backups = '/backups';
protected string $containerName = 'appwrite-mariadb';
public static function getName(): string
{
return 'db-backup';
}
public function __construct()
{
//docker compose exec appwrite backup
$this
->desc('Backup a DB')
->param('domain', App::getEnv('_APP_DOMAIN', ''), new Hostname(), 'Domain.', true)
->callback(fn ($domain) => $this->action($domain));
}
/**
* @throws \Exception
*/
public function action(string $domain): void
{
if (empty(App::getEnv('_APP_CONNECTIONS_DB_PROJECT', ''))) {
Console::error('Can\'t read .env variables');
Console::exit();
}
$time = date('Y-m-d_H:i:s');
Console::log('Backup started:' . $time);
// Console::log('creating directory:' . $this->backups);
// if (!file_exists($this->backups) && !mkdir($this->backups, 0755, true)) {
// Console::error('Error creating directory: ' . $this->backups);
// Console::exit();
// }
$stdout = '';
$stderr = '';
$cmd = 'docker stop ' . $this->containerName;
Console::log($cmd);
$code = Console::execute($cmd, '', $stdout, $stderr);
if (!empty($stderr)) {
Console::error($stderr);
Console::exit();
}
Console::log($stdout);
$filename = $time . '.tar.gz';
$file = self::$backups . '/' . $filename;
// $cmd = 'tar czf ' . $file . ' --absolute-names ' . $this->directory;
//
// $cmd = '
// cd /var/lib/mysql
// tar czf ' . $file . ' --absolute-names *';
// Extract:::
// tar --strip-components=4 -xf 2023-07-11_12:40:47.tar.gz -C ./a2
$stdout = '';
$stderr = '';
$cmd = 'cd ' . $this->directory . ' && tar zcf ' . $file . ' .';
$code = Console::execute($cmd, '', $stdout, $stderr);
Console::log($cmd);
Console::log($stdout);
if (!empty($stderr)) {
Console::error($stderr);
Console::exit();
}
$stdout = '';
$stderr = '';
$cmd = 'docker start ' . $this->containerName;
$code = Console::execute($cmd, '', $stdout, $stderr);
Console::log($stdout);
if (!empty($stderr)) {
Console::error($stderr);
Console::exit();
}
Console::loop(function () {
Console::log('Hello');
}, 1);
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\Platform\Action;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Validator\Hostname;
class DbRestore extends Action
{
public string $extract = '/extracts';
public static function getName(): string
{
return 'db-restore';
}
public function __construct()
{
//docker compose exec appwrite backup
$this
->desc('Restore a DB')
->param('domain', App::getEnv('_APP_DOMAIN', ''), new Hostname(), 'Domain.', true)
->callback(fn ($domain) => $this->action($domain));
}
/**
* @throws \Exception
*/
public function action(string $domain): void
{
$filename = '2023-07-12_09:37:47.tar.gz';
Console::log('Restoring backup' . $filename);
$file = DbBackup::$backups . '/' . $filename;
if (!file_exists($file)) {
Console::error('File not found: ' . $file);
Console::exit();
}
// Todo: shut down target container
if (!file_exists($this->extract) && !mkdir($this->extract, 0755, true)) {
Console::error('Error creating directory: ' . $this->extract);
Console::exit();
}
$stdout = '';
$stderr = '';
$start = microtime(true);
Console::log($start);
$cmd = 'tar -xzf ' . $file . ' -C ' . $this->extract;
Console::log($cmd);
$code = Console::execute($cmd, '', $stdout, $stderr);
Console::log('time end ' . (microtime(true) - $start));
if (!empty($stderr)) {
Console::error($stderr);
Console::exit();
}
Console::log($stdout);
// // Todo: start down target container
Console::log("Restore Finish");
}
}