folder backup

This commit is contained in:
shimon
2023-02-26 18:36:42 +02:00
parent b747e30d3f
commit e585142490
5 changed files with 97 additions and 13 deletions
+2 -1
View File
@@ -133,7 +133,8 @@ RUN chmod +x /usr/local/bin/doctor && \
chmod +x /usr/local/bin/worker-messaging && \
chmod +x /usr/local/bin/worker-webhooks && \
chmod +x /usr/local/bin/worker-usage && \
chmod +x /usr/local/bin/folder-backup
chmod +x /usr/local/bin/folder-backup && \
chmod +x /usr/local/bin/folder-restore
# Letsencrypt Permissions
RUN mkdir -p /etc/letsencrypt/live/ && chmod -Rf 755 /etc/letsencrypt/live/
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
php /usr/src/code/app/cli.php folder-restore $@
+3 -1
View File
@@ -16,6 +16,7 @@ use Appwrite\Platform\Tasks\Vars;
use Appwrite\Platform\Tasks\Version;
use Appwrite\Platform\Tasks\VolumeSync;
use Appwrite\Platform\Tasks\FolderBackup;
use Appwrite\Platform\Tasks\FolderRestore;
class Tasks extends Service
{
@@ -35,6 +36,7 @@ class Tasks extends Service
->addAction(SDKs::getName(), new SDKs())
->addAction(VolumeSync::getName(), new VolumeSync())
->addAction(Specs::getName(), new Specs())
->addAction(FolderBackup::getName(), new FolderBackup());
->addAction(FolderBackup::getName(), new FolderBackup())
->addAction(FolderRestore::getName(), new FolderRestore());
}
}
+22 -11
View File
@@ -17,7 +17,7 @@ class FolderBackup extends Action
public function __construct()
{
$this
->desc('Folder backup and restore process')
->desc('Folder backup process')
->callback(fn() => $this->action());
}
@@ -29,12 +29,9 @@ class FolderBackup extends Action
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 = new \DateTime($now->format("Y-m-d 16:35.0"));
$next->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$sleep = $next->getTimestamp() - $now->getTimestamp();
@@ -52,6 +49,7 @@ class FolderBackup extends Action
$folders = [
'cert' => APP_STORAGE_CERTIFICATES,
'config' => APP_STORAGE_CONFIG,
];
$remote = getDevice('/');
@@ -61,16 +59,15 @@ class FolderBackup extends Action
$filename = $key . '-' . date("Y-m-d") . '.tar.gz';
$source = $local->getRoot() . '/' . $filename;
$destination = '/' . $key . '/' . $filename;
$content = $local->getRoot() . '/*';
// for ($i = 0; $i < 1000; $i++) {
// file_put_contents($root->getRoot() . '/' . $i . '.txt', '');
// }
// for ($i = 0; $i < 1000; $i++) {
// file_put_contents($local->getRoot() . '/' . $i . '.txt', '');
// }
$stdout = '';
$stderr = '';
Console::execute(
'tar --exclude ' . $filename . ' -zcf ' . $source . ' ' . $content,
'cd ' . $folder . ' && tar --exclude ' . $filename . ' -zcf ' . $source . '*',
'',
$stdout,
$stderr
@@ -78,7 +75,21 @@ class FolderBackup extends Action
try {
$local->transfer($source, $destination, $remote);
console::info("backing up local $source to $remote->getName() $destination");
console::info("backing up local $source to {$remote->getName()} $destination");
/**
* Clean up
*/
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
for ($i = 0; $i < 20; $i++) {
$now->sub(\DateInterval::createFromDateString('1 days'));
if ($i >= 10) {
$destination = '/' . $key . '/' . $key . '-' . $now->format("Y-m-d") . '.tar.gz';
console::info("Trying to delete from {$remote->getName()} $destination");
$remote->delete($destination);
}
}
} catch (\Exception $e) {
Console::error($e->getMessage());
}
@@ -0,0 +1,67 @@
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\CLI\Console;
use Utopia\Platform\Action;
use Utopia\Storage\Device\Local;
class FolderRestore extends Action
{
public static function getName(): string
{
return 'folder-restore';
}
public function __construct()
{
$this
->desc('Folder restore process')
->callback(fn() => $this->action());
}
public function action(): void
{
$folders = [
'cert' => APP_STORAGE_CERTIFICATES,
'config' => APP_STORAGE_CONFIG,
];
$folder = \strtolower(Console::confirm('Please enter the folder you wish to restore'));
$date = Console::confirm('Please enter the date of the backup file in datetime format(Y-m-d)');
if (!array_key_exists($folder, $folders)) {
console::Error('Unknown folder given');
exit;
}
if (!is_a(\DateTime::createFromFormat("Y-m-d", $date), 'DateTime')) {
console::Error('Unknown date format');
exit;
}
$remote = getDevice('/');
$local = new Local();
$filename = $folder . '-' . $date . '.tar.gz';
$destination = $folders[$folder] . '/' . $filename;
$source = '/' . $folder . '/' . $filename;
try {
$remote->transfer($source, $destination, $local);
$stdout = '';
$stderr = '';
Console::execute(
'cd ' . $folders[$folder] . ' && tar -xf ' . $filename,
'',
$stdout,
$stderr
);
console::info("restoring from {$remote->getName()} $source to $destination");
} catch (\Exception $e) {
Console::error($e->getMessage());
}
}
}