more work on backup-storages; add backup cli-command

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2023-09-04 10:54:59 +02:00
parent 338b855947
commit 9d2077ddee
10 changed files with 1075 additions and 151 deletions

View File

@@ -202,7 +202,7 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
// validation
$description = Validate::validate($description, 'description', Validate::REGEX_DESC_TEXT, '', [], true);
$reload_cmd = Validate::validate($reload_cmd, 'reload_cmd', '/^[a-z0-9\/\._\- ]+$/i', '', [], true);
$reload_cmd = Validate::validate($reload_cmd, 'reload_cmd', '/^[a-z0-9\/\._\-@ ]+$/i', '', [], true);
$sel_stmt = Database::prepare("SELECT `id` FROM `".TABLE_PANEL_FPMDAEMONS."` WHERE `reload_cmd` = :rc");
$dupcheck = Database::pexecute_first($sel_stmt, ['rc' => $reload_cmd]);
if ($dupcheck && $dupcheck['id']) {
@@ -327,7 +327,7 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
// validation
$description = Validate::validate($description, 'description', Validate::REGEX_DESC_TEXT, '', [], true);
$reload_cmd = Validate::validate($reload_cmd, 'reload_cmd', '/^[a-z0-9\/\._\- ]+$/i', '', [], true);
$reload_cmd = Validate::validate($reload_cmd, 'reload_cmd', '/^[a-z0-9\/\._\-@ ]+$/i', '', [], true);
$sel_stmt = Database::prepare("SELECT `id` FROM `".TABLE_PANEL_FPMDAEMONS."` WHERE `reload_cmd` = :rc");
$dupcheck = Database::pexecute_first($sel_stmt, ['rc' => $reload_cmd]);
if ($dupcheck && $dupcheck['id'] != $id) {

View File

@@ -2,15 +2,30 @@
namespace Froxlor\Backup\Storages;
use Aws\S3\S3Client;
use Froxlor\FileDir;
class S3 extends Storage
{
private S3Client $s3_client;
/**
* @return bool
*/
public function init(): bool
{
// TODO: Implement init() method.
$raw_credentials = [
'credentials' => [
'key' => $this->sData['storage']['username'] ?? '',
'secret' => $this->sData['storage']['password'] ?? ''
],
'endpoint' => $this->sData['storage']['hostname'] ?? '',
'region' => $this->sData['storage']['region'] ?? '',
'version' => 'latest',
'use_path_style_endpoint' => true
];
$this->s3_client = new S3Client($raw_credentials);
}
/**
@@ -23,7 +38,11 @@ class S3 extends Storage
*/
protected function putFile(string $filename, string $tmp_source_directory): string
{
return "";
$this->s3_client->putObject([
'Bucket' => $this->sData['storage']['bucket'],
'Key' => $filename,
'SourceFile' => FileDir::makeCorrectFile($tmp_source_directory . '/' . $filename),
]);
}
/**
@@ -32,7 +51,15 @@ class S3 extends Storage
*/
protected function rmFile(string $filename): bool
{
// TODO: Implement removeOld() method.
$result = $this->s3_client->deleteObject([
'Bucket' => $this->sData['storage']['bucket'],
'Key' => $filename,
]);
if ($result['DeleteMarker']) {
return true;
}
return false;
}
/**

View File

@@ -5,6 +5,7 @@ namespace Froxlor\Backup\Storages;
use Exception;
use Froxlor\Database\Database;
use Froxlor\FileDir;
use Froxlor\Settings;
abstract class Storage
{
@@ -19,7 +20,8 @@ abstract class Storage
public function __construct(array $storage_data)
{
$this->sData = $storage_data;
$this->tmpDirectory = FileDir::makeCorrectDir(sys_get_temp_dir() . '/backup-' . $this->sData['loginname']);
$tmpDirectory = Settings::Get('backup.backup_tmp_dir') ?: sys_get_temp_dir();
$this->tmpDirectory = FileDir::makeCorrectDir($tmpDirectory . '/backup-' . $this->sData['loginname']);
}
/**

View File

@@ -0,0 +1,129 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can also view it online at
* https://files.froxlor.org/misc/COPYING.txt
*
* @copyright the authors
* @author Froxlor team <team@froxlor.org>
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
*/
namespace Froxlor\Cli;
use Exception;
use Froxlor\Backup\Storages\StorageFactory;
use Froxlor\Database\Database;
use Froxlor\Froxlor;
use Froxlor\Settings;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
final class BackupCommand extends CliCommand
{
protected function configure()
{
$this->setName('froxlor:backup');
$this->setDescription('Various backup actions');
$this->addOption('list', 'L', InputOption::VALUE_OPTIONAL, 'List backups (optionally pass a customer loginname to list backups of a specific user)')
->addOption('create', 'c', InputOption::VALUE_REQUIRED, 'Manually run a backup task for given customer (loginname)')
->addOption('delete', 'd', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Remove given backup by id');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$result = $this->validateRequirements($input, $output);
require Froxlor::getInstallDir() . '/lib/functions.php';
// set error-handler
@set_error_handler([
'\\Froxlor\\Api\\Api',
'phpErrHandler'
]);
if (!Settings::Get('backup.enabled')) {
$output->writeln('<error>Backup feature not enabled.</>');
$result = self::INVALID;
}
if ($result == self::SUCCESS) {
try {
$loginname = "";
$userinfo = [];
if ($input->hasArgument('user')) {
$loginname = $input->getArgument('user');
$userinfo = $this->getUserByName($loginname, false);
}
$do_list = $input->getOption('list');
$do_create = $input->getOption('create');
$do_delete = $input->getOption('delete');
if ($do_list === false && $do_create === false && $do_delete === false) {
$output->writeln('<error>No option given, nothing to do.</>');
return self::INVALID;
}
// list
if ($do_list !== false) {
if ($do_list === null) {
// all customers
} elseif ($do_list !== false) {
// specific customer
}
} elseif ($do_create !== false) {
$stmt = Database::prepare("SELECT
customerid,
loginname,
adminid,
backup,
guid,
documentroot
FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE `backup` > 0 AND `loginname` = :loginname
");
$customer = Database::pexecute_first($stmt, ['loginname' => $do_create]);
if (empty($customer)) {
$output->writeln('<error>Given customer not found or customer has backup=off</>');
return self::INVALID;
}
$backupStorage = StorageFactory::fromStorageId($customer['backup'], $customer);
$output->writeln("Initializing storage");
$backupStorage->init();
$output->writeln("Preparing files and folders");
$backupStorage->prepareFiles();
$output->writeln("Creating backup file");
$backupStorage->createFromFiles();
$output->writeln("Removing older backups by retention");
$backupStorage->removeOld();
$output->writeln('<info>Backup created successfully</>');
}
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</>');
$result = self::FAILURE;
}
}
return $result;
}
}