From 2aa3b1e29692102e38c1d33114e08ff4fb90466e Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 13 Aug 2023 13:36:25 +0300 Subject: [PATCH] nproc + setContainerId as function --- src/Appwrite/Platform/Tasks/Backup.php | 52 +++++++++++++++++++----- src/Appwrite/Platform/Tasks/Restore.php | 54 +++++++++++++++++++------ 2 files changed, 82 insertions(+), 24 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/Backup.php b/src/Appwrite/Platform/Tasks/Backup.php index b10a644ae9..e73f04ea6c 100644 --- a/src/Appwrite/Platform/Tasks/Backup.php +++ b/src/Appwrite/Platform/Tasks/Backup.php @@ -19,11 +19,11 @@ class Backup extends Action public const BACKUP_INTERVAL_SECONDS = 60 * 60 * 4; // 4 hours; public const COMPRESS_ALGORITHM = 'lz4'; public const CONFIG_PATH = '/etc/my.cnf'; - public const PROCESSORS = 4; protected ?DSN $dsn = null; protected ?string $database = null; protected ?DOSpaces $s3 = null; protected string $xtrabackupContainerId; + protected int $processors = 1; /** * @throws Exception @@ -61,7 +61,7 @@ class Backup extends Action $dsn = new DSN(App::getEnv('_APP_CONNECTIONS_BACKUPS_STORAGE')); $this->s3 = new DOSpaces('/' . $database . '/full', $dsn->getUser(), $dsn->getPassword(), $dsn->getPath(), $dsn->getParam('region')); } catch (\Exception $e) { - Console::error($e->getMessage() . 'Invalid DSN.'); + Console::error($e->getMessage()); Console::exit(); } @@ -89,14 +89,9 @@ class Backup extends Action sleep($sleep); } } while ($attempts < $max); - $containerId = shell_exec('docker ps -f "name=xtrabackup" --format "{{.ID}}"'); - $containerId = str_replace(PHP_EOL, '', $containerId); - if (empty($containerId)) { - Console::error('Xtrabackup Container ID not found'); - Console::exit(); - } - $this->xtrabackupContainerId = $containerId; + $this->setProcessors(); + $this->setContainerId(); Console::loop(function () { $this->start(); @@ -152,9 +147,9 @@ class Backup extends Action '--safe-slave-backup-timeout=300', '--check-privileges', // checks if Percona XtraBackup has all the required privileges. '--target-dir=' . $target, - '--parallel=' . self::PROCESSORS, + '--parallel=' . $this->processors, '--compress=' . self::COMPRESS_ALGORITHM, - '--compress-threads=' . self::PROCESSORS, + '--compress-threads=' . $this->processors, '--rsync', // https://docs.percona.com/percona-xtrabackup/8.0/accelerate-backup-process.html '2> ' . $logfile, ]; @@ -266,4 +261,39 @@ class Backup extends Action } return null; } + + public function setProcessors() + { + $stdout = ''; + $stderr = ''; + Console::execute('nproc', '', $stdout, $stderr); + if (!empty($stderr)) { + Console::error('Error setting processors: ' . $stderr); + Console::exit(); + } + + $processors = str_replace(PHP_EOL, '', $stdout); + $processors = intval($processors); + $processors = $processors === 0 ? 1 : $processors; + $this->processors = $processors; + } + + public function setContainerId() + { + $stdout = ''; + $stderr = ''; + Console::execute('docker ps -f "name=xtrabackup" --format "{{.ID}}"', '', $stdout, $stderr); + if (!empty($stderr)) { + Console::error('Error setting container Id: ' . $stderr); + Console::exit(); + } + + $containerId = str_replace(PHP_EOL, '', $stdout); + if (empty($containerId)) { + Console::error('Xtrabackup Container ID not found'); + Console::exit(); + } + + $this->xtrabackupContainerId = $containerId; + } } diff --git a/src/Appwrite/Platform/Tasks/Restore.php b/src/Appwrite/Platform/Tasks/Restore.php index d9782c78d2..6904d83b58 100644 --- a/src/Appwrite/Platform/Tasks/Restore.php +++ b/src/Appwrite/Platform/Tasks/Restore.php @@ -17,24 +17,17 @@ class Restore extends Action { public const BACKUPS_PATH = '/backups'; public const DATADIR = '/var/lib/mysql'; - public const PROCESSORS = 4; protected ?DSN $dsn = null; protected string $database; protected ?DOSpaces $s3 = null; protected string $xtrabackupContainerId; + protected int $processors = 1; public function __construct() { $this->checkEnvVariables(); - - $containerId = shell_exec('docker ps -aqf "name=xtrabackup"'); - $containerId = str_replace(PHP_EOL, '', $containerId); - if (empty($containerId)) { - Console::error('Xtrabackup Container ID not found'); - Console::exit(); - } - - $this->xtrabackupContainerId = $containerId; + $this->setProcessors(); + $this->setContainerId(); $this ->desc('Restore a DB') @@ -173,8 +166,8 @@ class Restore extends Action '--decompress', '--strict', '--remove-original', // Removes *.lz4 compressed files - '--parallel=' . self::PROCESSORS, - '--compress-threads=' . self::PROCESSORS, + '--parallel=' . $this->processors, + '--compress-threads=' . $this->processors, '--target-dir=' . $target, '2> ' . $logfile, ]; @@ -243,7 +236,7 @@ class Restore extends Action '--strict', '--target-dir=' . $target, '--datadir=' . $datadir, - '--parallel=' . self::PROCESSORS, + '--parallel=' . $this->processors, '2> ' . $logfile, ]; @@ -290,4 +283,39 @@ class Restore extends Action } return null; } + + public function setProcessors() + { + $stdout = ''; + $stderr = ''; + Console::execute('nproc', '', $stdout, $stderr); + if (!empty($stderr)) { + Console::error('Error setting processors: ' . $stderr); + Console::exit(); + } + + $processors = str_replace(PHP_EOL, '', $stdout); + $processors = intval($processors); + $processors = $processors === 0 ? 1 : $processors; + $this->processors = $processors; + } + + public function setContainerId() + { + $stdout = ''; + $stderr = ''; + Console::execute('docker ps -f "name=xtrabackup" --format "{{.ID}}"', '', $stdout, $stderr); + if (!empty($stderr)) { + Console::error('Error setting container Id: ' . $stderr); + Console::exit(); + } + + $containerId = str_replace(PHP_EOL, '', $stdout); + if (empty($containerId)) { + Console::error('Xtrabackup Container ID not found'); + Console::exit(); + } + + $this->xtrabackupContainerId = $containerId; + } }