|
|
|
|
@@ -31,6 +31,7 @@ use Froxlor\Api\ResourceEntity;
|
|
|
|
|
use Froxlor\Database\Database;
|
|
|
|
|
use Froxlor\FileDir;
|
|
|
|
|
use Froxlor\FroxlorLogger;
|
|
|
|
|
use Froxlor\Settings;
|
|
|
|
|
use Froxlor\UI\Response;
|
|
|
|
|
use Froxlor\Validate\Validate;
|
|
|
|
|
use PDO;
|
|
|
|
|
@@ -40,6 +41,14 @@ use PDO;
|
|
|
|
|
*/
|
|
|
|
|
class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
{
|
|
|
|
|
const SUPPORTED_TYPES = [
|
|
|
|
|
'local',
|
|
|
|
|
'ftp',
|
|
|
|
|
'sftp',
|
|
|
|
|
'rsync',
|
|
|
|
|
's3',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* lists all backup storages entries
|
|
|
|
|
*
|
|
|
|
|
@@ -61,7 +70,7 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
public function listing()
|
|
|
|
|
{
|
|
|
|
|
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] list backups");
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_INFO, "[API] list backup storages");
|
|
|
|
|
$query_fields = [];
|
|
|
|
|
$result_stmt = Database::prepare("
|
|
|
|
|
SELECT * FROM `" . TABLE_PANEL_BACKUP_STORAGES . "`
|
|
|
|
|
@@ -103,28 +112,28 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create a backup storage by given id
|
|
|
|
|
* create a backup storage
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* required, backup storage type
|
|
|
|
|
* @param string $destination_path
|
|
|
|
|
* required, destination path for backup storage
|
|
|
|
|
* @param string $description
|
|
|
|
|
* description for backup storage
|
|
|
|
|
* required, description for backup storage
|
|
|
|
|
* @param string $region
|
|
|
|
|
* region for backup storage (used for S3)
|
|
|
|
|
* optional, required if type=s3. Region for backup storage (used for S3)
|
|
|
|
|
* @param string $bucket
|
|
|
|
|
* bucket for backup storage (used for S3)
|
|
|
|
|
* optional, required if type=s3. Bucket for backup storage (used for S3)
|
|
|
|
|
* @param string $hostname
|
|
|
|
|
* hostname for backup storage
|
|
|
|
|
* optional, required if type != local. Hostname for backup storage
|
|
|
|
|
* @param string $username
|
|
|
|
|
* username for backup storage (also used as access key for S3)
|
|
|
|
|
* optional, required if type != local. Username for backup storage (also used as access key for S3)
|
|
|
|
|
* @param string $password
|
|
|
|
|
* password for backup storage (also used as secret key for S3)
|
|
|
|
|
* optional, required if type != local. Password for backup storage (also used as secret key for S3)
|
|
|
|
|
* @param string $pgp_public_key
|
|
|
|
|
* pgp public key for backup storage
|
|
|
|
|
* optional, pgp public key for backup storage
|
|
|
|
|
* @param string $retention
|
|
|
|
|
* retention for backup storage
|
|
|
|
|
* optional, retention for backup storage (default 3)
|
|
|
|
|
*
|
|
|
|
|
* @access admin
|
|
|
|
|
* @return string json-encoded array
|
|
|
|
|
@@ -136,16 +145,39 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
// required parameters
|
|
|
|
|
$type = $this->getParam('type');
|
|
|
|
|
$destination_path = $this->getParam('destination_path');
|
|
|
|
|
$description = $this->getParam('description');
|
|
|
|
|
|
|
|
|
|
// type related requirements
|
|
|
|
|
$optional_flags = [
|
|
|
|
|
'region' => true,
|
|
|
|
|
'bucket' => true,
|
|
|
|
|
'hostname' => true,
|
|
|
|
|
'username' => true,
|
|
|
|
|
'password' => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!in_array($type, self::SUPPORTED_TYPES)) {
|
|
|
|
|
throw new Exception("Unsupported storage type: '" . $type . "'", 406);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($type != 'local') {
|
|
|
|
|
$optional_flags['hostname'] = false;
|
|
|
|
|
$optional_flags['username'] = false;
|
|
|
|
|
$optional_flags['password'] = false;
|
|
|
|
|
}
|
|
|
|
|
if ($type == 's3') {
|
|
|
|
|
$optional_flags['region'] = false;
|
|
|
|
|
$optional_flags['bucket'] = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parameters
|
|
|
|
|
$description = $this->getParam('description', true, null);
|
|
|
|
|
$region = $this->getParam('region', true, null);
|
|
|
|
|
$bucket = $this->getParam('bucket', true, null);
|
|
|
|
|
$hostname = $this->getParam('hostname', true, null);
|
|
|
|
|
$username = $this->getParam('username', true, null);
|
|
|
|
|
$password = $this->getParam('password', true, null);
|
|
|
|
|
$region = $this->getParam('region', $optional_flags['region']);
|
|
|
|
|
$bucket = $this->getParam('bucket', $optional_flags['bucket']);
|
|
|
|
|
$hostname = $this->getParam('hostname', $optional_flags['hostname']);
|
|
|
|
|
$username = $this->getParam('username', $optional_flags['username']);
|
|
|
|
|
$password = $this->getParam('password', $optional_flags['password']);
|
|
|
|
|
$pgp_public_key = $this->getParam('pgp_public_key', true, null);
|
|
|
|
|
$retention = $this->getParam('retention', true, null);
|
|
|
|
|
$retention = $this->getParam('retention', true, 3);
|
|
|
|
|
|
|
|
|
|
// validation
|
|
|
|
|
$destination_path = FileDir::makeCorrectDir(Validate::validate($destination_path, 'destination_path', Validate::REGEX_DIR, '', [], true));
|
|
|
|
|
@@ -158,7 +190,7 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
Response::standardError('gnupgextensionnotavailable', '', true);
|
|
|
|
|
}
|
|
|
|
|
// check if the pgp public key is a valid key
|
|
|
|
|
putenv('GNUPGHOME='.sys_get_temp_dir());
|
|
|
|
|
putenv('GNUPGHOME=' . sys_get_temp_dir());
|
|
|
|
|
if (gnupg_import(gnupg_init(), $pgp_public_key) === false) {
|
|
|
|
|
Response::standardError('invalidpgppublickey', '', true);
|
|
|
|
|
}
|
|
|
|
|
@@ -204,22 +236,22 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
];
|
|
|
|
|
Database::pexecute($stmt, $params, true, true);
|
|
|
|
|
$id = Database::lastInsertId();
|
|
|
|
|
$this->logger()->logAction($this->isAdmin() ? FroxlorLogger::ADM_ACTION : FroxlorLogger::USR_ACTION, LOG_NOTICE, "[API] edited backup storage for '" . $result['id'] . "'");
|
|
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
$result = $this->apiCall('BackupStorages.get', [
|
|
|
|
|
'id' => $id
|
|
|
|
|
]);
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] added backup storage '" . $result['description'] . "' (" . $result['type'] . ")");
|
|
|
|
|
return $this->response($result);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("Not allowed to execute given command.", 403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return an admin entry by id
|
|
|
|
|
* return a backup storage entry by id
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* optional, the backup-storage-id
|
|
|
|
|
* the backup-storage-id
|
|
|
|
|
*
|
|
|
|
|
* @access admin
|
|
|
|
|
* @return string json-encoded array
|
|
|
|
|
@@ -239,7 +271,7 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
];
|
|
|
|
|
$result = Database::pexecute_first($result_stmt, $params, true, true);
|
|
|
|
|
if ($result) {
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_INFO, "[API] get backup storage for '" . $result['id'] . "'");
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_INFO, "[API] get backup storage '" . $result['description'] . "'");
|
|
|
|
|
return $this->response($result);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("Backup storage with " . $id . " could not be found", 404);
|
|
|
|
|
@@ -251,27 +283,27 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
* update a backup storage by given id
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* required, the backup-storage-id
|
|
|
|
|
* required, the backup-storage-id
|
|
|
|
|
* @param string $type
|
|
|
|
|
* backup storage type
|
|
|
|
|
* optional, backup storage type
|
|
|
|
|
* @param string $destination_path
|
|
|
|
|
* destination path for backup storage
|
|
|
|
|
* optional, destination path for backup storage
|
|
|
|
|
* @param string $description
|
|
|
|
|
* description for backup storage
|
|
|
|
|
* required, description for backup storage
|
|
|
|
|
* @param string $region
|
|
|
|
|
* region for backup storage (used for S3)
|
|
|
|
|
* optional, region for backup storage (used for S3)
|
|
|
|
|
* @param string $bucket
|
|
|
|
|
* bucket for backup storage (used for S3)
|
|
|
|
|
* optional, bucket for backup storage (used for S3)
|
|
|
|
|
* @param string $hostname
|
|
|
|
|
* hostname for backup storage
|
|
|
|
|
* optional, hostname for backup storage
|
|
|
|
|
* @param string $username
|
|
|
|
|
* username for backup storage (also used as access key for S3)
|
|
|
|
|
* optional, username for backup storage (also used as access key for S3)
|
|
|
|
|
* @param string $password
|
|
|
|
|
* password for backup storage (also used as secret key for S3)
|
|
|
|
|
* optional, password for backup storage (also used as secret key for S3)
|
|
|
|
|
* @param string $pgp_public_key
|
|
|
|
|
* pgp public key for backup storage
|
|
|
|
|
* optional, pgp public key for backup storage
|
|
|
|
|
* @param string $retention
|
|
|
|
|
* retention for backup storage
|
|
|
|
|
* optional, retention for backup storage (default 3)
|
|
|
|
|
*
|
|
|
|
|
* @access admin
|
|
|
|
|
* @return string json-encoded array
|
|
|
|
|
@@ -295,27 +327,61 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
$destination_path = $this->getParam('destination_path', true, $result['destination_path']);
|
|
|
|
|
$hostname = $this->getParam('hostname', true, $result['hostname']);
|
|
|
|
|
$username = $this->getParam('username', true, $result['username']);
|
|
|
|
|
$password = $this->getParam('password', true, $result['password']);
|
|
|
|
|
$password = $this->getParam('password', true, '');
|
|
|
|
|
$pgp_public_key = $this->getParam('pgp_public_key', true, $result['pgp_public_key']);
|
|
|
|
|
$retention = $this->getParam('retention', true, $result['retention']);
|
|
|
|
|
|
|
|
|
|
if (!in_array($type, self::SUPPORTED_TYPES)) {
|
|
|
|
|
throw new Exception("Unsupported storage type: '" . $type . "'", 406);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($type != 'local') {
|
|
|
|
|
if (empty($hostname)) {
|
|
|
|
|
throw new Exception("Field 'hostname' cannot be empty", 406);
|
|
|
|
|
}
|
|
|
|
|
if (empty($username)) {
|
|
|
|
|
throw new Exception("Field 'username' cannot be empty", 406);
|
|
|
|
|
}
|
|
|
|
|
$password = Validate::validate($password, 'password', '', '', [], true);
|
|
|
|
|
}
|
|
|
|
|
if ($type == 's3') {
|
|
|
|
|
if (empty($region)) {
|
|
|
|
|
throw new Exception("Field 'region' cannot be empty", 406);
|
|
|
|
|
}
|
|
|
|
|
if (empty($bucket)) {
|
|
|
|
|
throw new Exception("Field 'bucket' cannot be empty", 406);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validation
|
|
|
|
|
$destination_path = FileDir::makeCorrectDir(Validate::validate($destination_path, 'destination_path', Validate::REGEX_DIR, '', [], true));
|
|
|
|
|
// TODO: add more validation
|
|
|
|
|
|
|
|
|
|
// pgp public key validation
|
|
|
|
|
if (!empty($pgp_public_key)) {
|
|
|
|
|
if (!empty($pgp_public_key) && $pgp_public_key != $result['pgp_public_key']) {
|
|
|
|
|
// check if gnupg extension is loaded
|
|
|
|
|
if (!extension_loaded('gnupg')) {
|
|
|
|
|
Response::standardError('gnupgextensionnotavailable', '', true);
|
|
|
|
|
}
|
|
|
|
|
// check if the pgp public key is a valid key
|
|
|
|
|
putenv('GNUPGHOME='.sys_get_temp_dir());
|
|
|
|
|
putenv('GNUPGHOME=' . sys_get_temp_dir());
|
|
|
|
|
if (gnupg_import(gnupg_init(), $pgp_public_key) === false) {
|
|
|
|
|
Response::standardError('invalidpgppublickey', '', true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($password)) {
|
|
|
|
|
$stmt = Database::prepare("UPDATE `" . TABLE_PANEL_BACKUP_STORAGES . "`
|
|
|
|
|
SET `password` = :password
|
|
|
|
|
WHERE `id` = :id
|
|
|
|
|
");
|
|
|
|
|
Database::pexecute($stmt, [
|
|
|
|
|
"id" => $id,
|
|
|
|
|
"password" => $password
|
|
|
|
|
], true, true);
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] updated password for backup-storage '" . $result['description'] . "'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update
|
|
|
|
|
$stmt = Database::prepare("
|
|
|
|
|
UPDATE `" . TABLE_PANEL_BACKUP_STORAGES . "`
|
|
|
|
|
@@ -326,7 +392,6 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
`destination_path` = :destination_path,
|
|
|
|
|
`hostname` = :hostname,
|
|
|
|
|
`username` = :username,
|
|
|
|
|
`password` = :password,
|
|
|
|
|
`pgp_public_key` = :pgp_public_key,
|
|
|
|
|
`retention` = :retention
|
|
|
|
|
WHERE `id` = :id
|
|
|
|
|
@@ -340,12 +405,11 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
"destination_path" => $destination_path,
|
|
|
|
|
"hostname" => $hostname,
|
|
|
|
|
"username" => $username,
|
|
|
|
|
"password" => $password,
|
|
|
|
|
"pgp_public_key" => $pgp_public_key,
|
|
|
|
|
"retention" => $retention,
|
|
|
|
|
];
|
|
|
|
|
Database::pexecute($stmt, $params, true, true);
|
|
|
|
|
$this->logger()->logAction($this->isAdmin() ? FroxlorLogger::ADM_ACTION : FroxlorLogger::USR_ACTION, LOG_NOTICE, "[API] edited backup storage for '" . $result['id'] . "'");
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] edited backup storage '" . $result['description'] . "'");
|
|
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
$result = $this->apiCall('BackupStorages.get', [
|
|
|
|
|
@@ -376,6 +440,33 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
'id' => $id
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// validate no-one's using it
|
|
|
|
|
|
|
|
|
|
// settings
|
|
|
|
|
if ($id == Settings::Get('backup.default_storage')) {
|
|
|
|
|
throw new Exception("Given backup storage is currently set as default storage and cannot be deleted.", 406);
|
|
|
|
|
}
|
|
|
|
|
// customers
|
|
|
|
|
$sel_stmt = Database::prepare("
|
|
|
|
|
SELECT COUNT(*) as num_storage_users
|
|
|
|
|
FROM `" . TABLE_PANEL_CUSTOMERS . "`
|
|
|
|
|
WHERE `backup` = :id
|
|
|
|
|
");
|
|
|
|
|
$storage_users_result = Database::pexecute_first($sel_stmt, ['id' => $id]);
|
|
|
|
|
if ($storage_users_result && $storage_users_result['num_storage_users'] > 0) {
|
|
|
|
|
throw new Exception("Given backup storage is currently assigned to " . $storage_users_result['num_storage_users'] . " customers and cannot be deleted.", 406);
|
|
|
|
|
}
|
|
|
|
|
// existing backups
|
|
|
|
|
$sel_stmt = Database::prepare("
|
|
|
|
|
SELECT COUNT(*) as num_storage_backups
|
|
|
|
|
FROM `" . TABLE_PANEL_BACKUPS . "`
|
|
|
|
|
WHERE `storage_id` = :id
|
|
|
|
|
");
|
|
|
|
|
$storage_backups_result = Database::pexecute_first($sel_stmt, ['id' => $id]);
|
|
|
|
|
if ($storage_backups_result && $storage_backups_result['num_storage_backups'] > 0) {
|
|
|
|
|
throw new Exception("Given backup storage has still " . $storage_backups_result['num_storage_backups'] . " backups on it and cannot be deleted.", 406);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete
|
|
|
|
|
$stmt = Database::prepare("
|
|
|
|
|
DELETE FROM `" . TABLE_PANEL_BACKUP_STORAGES . "`
|
|
|
|
|
@@ -385,7 +476,7 @@ class BackupStorages extends ApiCommand implements ResourceEntity
|
|
|
|
|
"id" => $id
|
|
|
|
|
];
|
|
|
|
|
Database::pexecute($stmt, $params, true, true);
|
|
|
|
|
$this->logger()->logAction($this->isAdmin() ? FroxlorLogger::ADM_ACTION : FroxlorLogger::USR_ACTION, LOG_NOTICE, "[API] deleted backup storage for '" . $result['id'] . "'");
|
|
|
|
|
$this->logger()->logAction(FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] deleted backup storage '" . $result['description'] . "'");
|
|
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
return $this->response(true);
|
|
|
|
|
|