folder backup

This commit is contained in:
shimon
2023-02-26 14:07:26 +02:00
parent 58d2f7f3ce
commit b747e30d3f
3 changed files with 64 additions and 21 deletions
+25
View File
@@ -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');
+38 -20
View File
@@ -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);
}
}
}