From 0e9286d2f5cd952280a6c0a4c168ce85fad1cd7e Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 28 Feb 2023 19:32:30 +0200 Subject: [PATCH] folder backup --- app/console | 2 +- src/Appwrite/Platform/Tasks/Backup.php | 71 +++++++++++--------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/console b/app/console index 5e2a40c1e3..39727607a0 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit 5e2a40c1e397bd341a432698c9d76a3f96315841 +Subproject commit 39727607a036534a323101fea876cbe68b05f4f0 diff --git a/src/Appwrite/Platform/Tasks/Backup.php b/src/Appwrite/Platform/Tasks/Backup.php index b3814f2931..6daed16e4c 100644 --- a/src/Appwrite/Platform/Tasks/Backup.php +++ b/src/Appwrite/Platform/Tasks/Backup.php @@ -20,41 +20,39 @@ class Backup extends Action ->callback(fn() => $this->action()); } + /** + * @throws \Exception + */ public function action(): void { Console::title('Backup V1'); Console::success(APP_NAME . ' backup process v1 has started'); - gc_enable(); - $time = 0; - while (!connection_aborted() || PHP_SAPI == "cli") { - $now = new \DateTime(); - $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); - $next = new \DateTime($now->format("Y-m-d 16:0.0")); - $next->setTimezone(new \DateTimeZone(date_default_timezone_get())); - $sleep = $next->getTimestamp() - $now->getTimestamp(); + $now = new \DateTime(); + $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $next = new \DateTime($now->format("Y-m-d 17:25.0")); + $next->setTimezone(new \DateTimeZone(date_default_timezone_get())); + $delay = $next->getTimestamp() - $now->getTimestamp(); - /** - * If time passed the target time - */ - if ($sleep <= 0) { - $next->add(\DateInterval::createFromDateString('1 days')); - $sleep = $next->getTimestamp() - $now->getTimestamp(); - } + /** + * If time passed for the target day. + */ + if ($delay <= 0) { + $next->add(\DateInterval::createFromDateString('1 days')); + $delay = $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") . ']'); + $folders = [ + 'cert' => APP_STORAGE_CERTIFICATES, + 'config' => APP_STORAGE_CONFIG, + ]; - sleep($sleep); + $sleep = 86400; //24 hours + $sleep = 60; + $remote = getDevice('/'); - $total = 0; - $start = \microtime(true); - - $folders = [ - 'cert' => APP_STORAGE_CERTIFICATES, - 'config' => APP_STORAGE_CONFIG, - ]; - - $remote = getDevice('/'); + Console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Delaying for ' . $delay . ' setting loop to [' . $next->format("Y-m-d H:i:s.v") . ']'); + Console::loop(function () use ($delay, $sleep, $folders, $remote) { foreach ($folders as $key => $folder) { $local = new Local($folder); @@ -62,19 +60,20 @@ class Backup extends Action $source = $local->getRoot() . '/' . $filename; $destination = '/' . $key . '/' . $filename; - for ($i = 0; $i < 1000; $i++) { - file_put_contents($local->getRoot() . '/' . $i . '.txt', ''); - } +// for ($i = 0; $i < 1000; $i++) { +// file_put_contents($local->getRoot() . '/' . $i . '.txt', ''); +// } $stdout = ''; $stderr = ''; + Console::execute( 'cd ' . $folder . ' && tar --exclude ' . $filename . ' -zcf ' . $source . ' *', '', $stdout, $stderr ); - var_dump($local->exists($source)); + try { if (!$local->exists($source)) { continue; @@ -84,7 +83,7 @@ class Backup extends Action Console::info("Backing up local $source to {$remote->getName()} $destination"); /** - * Remote storage old backup files clean-up + * Backup folder, long tail cleanup. */ $now = new \DateTime(); $now->setTimezone(new \DateTimeZone(date_default_timezone_get())); @@ -99,15 +98,7 @@ class Backup extends Action } catch (\Exception $e) { Console::error($e->getMessage()); } - - if (PHP_SAPI == "cli") { - if ($time >= 60 * 5) { - $time = 0; - gc_collect_cycles(); - } - } } - $total = (microtime(true) - $start); - } + }, $sleep, $delay); } }