diff --git a/app/cli.php b/app/cli.php index 81fb1dff82..ac07c9fa50 100644 --- a/app/cli.php +++ b/app/cli.php @@ -7,6 +7,7 @@ use Appwrite\Event\Func; use Appwrite\Platform\Appwrite; use Utopia\CLI\CLI; use Utopia\Database\Validator\Authorization; +use Utopia\DSN\DSN; use Utopia\Platform\Service; use Utopia\App; use Utopia\CLI\Console; @@ -18,6 +19,14 @@ use Utopia\Database\Document; use Utopia\Logger\Log; use Utopia\Pools\Group; use Utopia\Registry\Registry; +use Utopia\Storage\Device; +use Utopia\Storage\Device\Backblaze; +use Utopia\Storage\Device\DOSpaces; +use Utopia\Storage\Device\Linode; +use Utopia\Storage\Device\Local; +use Utopia\Storage\Device\S3; +use Utopia\Storage\Device\Wasabi; +use Utopia\Storage\Storage; Authorization::disable(); @@ -121,6 +130,22 @@ CLI::setResource('queueForFunctions', function (Group $pools) { return new Func($pools->get('queue')->pop()->getResource()); }, ['pools']); +CLI::setResource('deviceLocal', function () { + return new Local(); +}); + +CLI::setResource('deviceFiles', function ($project) { + return getDevice(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); +}, ['project']); + +CLI::setResource('deviceFunctions', function ($project) { + return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $project->getId()); +}, ['project']); + +CLI::setResource('deviceBuilds', function ($project) { + return getDevice(APP_STORAGE_BUILDS . '/app-' . $project->getId()); +}, ['project']); + CLI::setResource('logError', function (Registry $register) { return function (Throwable $error, string $namespace, string $action) use ($register) { $logger = $register->get('logger'); diff --git a/app/console b/app/console index 39727607a0..5e2a40c1e3 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 39727607a036534a323101fea876cbe68b05f4f0 +Subproject commit 5e2a40c1e397bd341a432698c9d76a3f96315841 diff --git a/src/Appwrite/Platform/Tasks/FolderBackup.php b/src/Appwrite/Platform/Tasks/FolderBackup.php index 204d635ad5..2e76d3c013 100644 --- a/src/Appwrite/Platform/Tasks/FolderBackup.php +++ b/src/Appwrite/Platform/Tasks/FolderBackup.php @@ -2,12 +2,9 @@ namespace Appwrite\Platform\Tasks; -use Utopia\App; use Utopia\CLI\Console; use Utopia\Database\DateTime; -use Utopia\DSN\DSN; use Utopia\Platform\Action; -use Utopia\Storage\Device\DOSpaces; use Utopia\Storage\Device\Local; class FolderBackup extends Action @@ -21,7 +18,7 @@ class FolderBackup extends Action { $this ->desc('Folder backup and restore process') - ->callback(fn () => $this->action()); + ->callback(fn() => $this->action()); } public function action(): void @@ -29,16 +26,40 @@ class FolderBackup extends Action Console::title('Folder backup V1'); Console::success(APP_NAME . ' folder backup process v1 has started'); - $interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); - Console::loop(function () use ($interval) { + gc_enable(); + $time = 0; + while (!connection_aborted() || PHP_SAPI == "cli") { + $now = DateTime::now(); + + + $now = new \DateTime(); + $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $next = new \DateTime($now->format("Y-m-d 12:10:0.0")); + $next->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $sleep = $next->getTimestamp() - $now->getTimestamp(); + + /** + * If time passed the target time + */ + if ($sleep <= 0) { + $next->add(\DateInterval::createFromDateString('1 days')); + $sleep = $next->getTimestamp() - $now->getTimestamp(); + } + + console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Sleeping for ' . $sleep . ' seconds next run will be at [' . $next->format("Y-m-d H:i:s.v") . ']'); + + sleep($sleep); + $folders = [ 'cert' => APP_STORAGE_CERTIFICATES, ]; + $remote = getDevice('/'); + foreach ($folders as $key => $folder) { $local = new Local($folder); $filename = $key . '-' . date("Y-m-d") . '.tar.gz'; - $source = $local->getRoot() . '/' . $filename; + $source = $local->getRoot() . '/' . $filename; $destination = '/' . $key . '/' . $filename; $content = $local->getRoot() . '/*'; @@ -56,22 +77,19 @@ class FolderBackup extends Action ); try { - $connection = App::getEnv('_APP_CONNECTIONS_STORAGE', ''); - $dsn = new DSN($connection); - $remote = new DOSpaces( - '/', - $dsn->getUser(), - $dsn->getPassword(), - $dsn->getPath(), - $dsn->getParam('region') - ); - $result = $local->transfer($source, $destination, $remote); - var_dump($result); - Console::log('done'); + $local->transfer($source, $destination, $remote); + console::info("backing up local $source to $remote->getName() $destination"); } catch (\Exception $e) { Console::error($e->getMessage()); } + + if (PHP_SAPI == "cli") { + if ($time >= 60 * 5) { + $time = 0; + gc_collect_cycles(); + } + } } - }, $interval); + } } }