fix: adapt ServersBackupController to new codebase after rebase

- Remove deleted ServerController dependency; implement runHostScript,
  downloadFileFromHost, uploadFileToHostPublic directly using SshSession
  and libssh::Client which are the current SSH abstractions
- Change constructor from std::shared_ptr<Settings> to QPointer<SecureQSettings>
  to match CoreController's settings type
- Register PageSettingsServerBackup, PageSettingsServerBackupRestored,
  PageSettingsServerRestoreMode in qml.qrc (were in resources.qrc which
  was removed from the codebase)
This commit is contained in:
NickVs2015
2026-05-22 15:24:01 +03:00
parent 7112ab98c0
commit 0ca5279e02
3 changed files with 62 additions and 15 deletions
@@ -1,4 +1,6 @@
#include "serversBackupController.h"
#include "secureQSettings.h"
#include "core/utils/utilities.h"
#include <QDebug>
#include <QDir>
#include <QRegularExpression>
@@ -24,11 +26,10 @@
#include "ui/models/servers_model.h"
#include "ui/models/containers_model.h"
ServersBackupController::ServersBackupController(std::shared_ptr<Settings> settings, ServersModel *serversModel, QObject *parent)
ServersBackupController::ServersBackupController(SecureQSettings *settings, ServersModel *serversModel, QObject *parent)
: QObject(parent)
, m_settings(settings)
, m_serversModel(serversModel)
, m_serverController(new ServerController(settings, this))
, m_status(Idle)
, m_backupDir("/var/backups/amnezia")
, m_restoreReplaceMode(false)
@@ -87,7 +88,7 @@ void ServersBackupController::createBackup(const ServerCredentials &credentials)
};
// Run script on server
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
@@ -165,7 +166,7 @@ void ServersBackupController::createContainerBackup(const ServerCredentials &cre
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
@@ -221,7 +222,7 @@ void ServersBackupController::createContainersBackup(const ServerCredentials &cr
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
@@ -268,7 +269,7 @@ void ServersBackupController::fetchBackupList(const ServerCredentials &credentia
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
QList<BackupInfo> backups = parseBackupList(m_currentOutput);
@@ -310,7 +311,7 @@ void ServersBackupController::restoreBackup(const ServerCredentials &credentials
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
// Check output for errors, even if script exited with code 0
bool hasError = m_currentOutput.contains("[ERROR]") ||
@@ -362,7 +363,7 @@ void ServersBackupController::checkBackupStatus(const ServerCredentials &credent
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
QJsonObject status = parseBackupStatus(m_currentOutput);
@@ -438,7 +439,7 @@ void ServersBackupController::downloadBackup(const ServerCredentials &credential
setProgress(25, tr("Starting file transfer..."));
ErrorCode error = m_serverController->downloadFileFromHost(credentials, remotePath, actualLocalPath);
ErrorCode error = downloadFileFromHost(credentials, remotePath, actualLocalPath);
if (error == ErrorCode::NoError) {
// qDebug() << "Backup downloaded to:" << actualLocalPath;
@@ -582,7 +583,7 @@ void ServersBackupController::uploadBackup(const ServerCredentials &credentials,
setProgress(25, tr("Starting file transfer..."));
ErrorCode error = m_serverController->uploadFileToHostPublic(credentials, actualLocalPath, remotePath,
ErrorCode error = uploadFileToHostPublic(credentials, actualLocalPath, remotePath,
libssh::ScpOverwriteMode::ScpOverwriteExisting);
qDebug() << "Upload result, error code:" << static_cast<int>(error);
@@ -763,7 +764,7 @@ void ServersBackupController::deleteBackup(const ServerCredentials &credentials,
return handleStdErr(data, m_currentError);
};
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
if (error == ErrorCode::NoError) {
setStatus(Success);
@@ -1482,3 +1483,36 @@ void ServersBackupController::trySetDefaultContainer()
emit defaultServerAndContainerSet();
}
}
ErrorCode ServersBackupController::runHostScript(const ServerCredentials &credentials, const QString &script,
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdOut,
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdErr)
{
const QString fileName = "/tmp/amnezia_" + Utils::getRandomString(16) + ".sh";
ErrorCode e = m_sshSession.uploadFileToHost(credentials, script.toUtf8(), fileName);
if (e != ErrorCode::NoError)
return e;
e = m_sshSession.runScript(credentials, QString("sudo bash %1").arg(fileName), cbStdOut, cbStdErr);
m_sshSession.runScript(credentials, QString("sudo rm -f %1").arg(fileName));
return e;
}
ErrorCode ServersBackupController::downloadFileFromHost(const ServerCredentials &credentials, const QString &remotePath, const QString &localPath)
{
libssh::Client sshClient;
ErrorCode e = sshClient.connectToHost(credentials);
if (e != ErrorCode::NoError)
return e;
return sshClient.scpFileDownload(remotePath, localPath);
}
ErrorCode ServersBackupController::uploadFileToHostPublic(const ServerCredentials &credentials, const QString &localPath, const QString &remotePath,
libssh::ScpOverwriteMode overwriteMode)
{
libssh::Client sshClient;
ErrorCode e = sshClient.connectToHost(credentials);
if (e != ErrorCode::NoError)
return e;
return sshClient.scpFileCopy(overwriteMode, localPath, remotePath, "backup_file");
}
@@ -10,10 +10,13 @@
class QTemporaryFile;
class ServersModel;
class SecureQSettings;
#include <QPointer>
#include "core/controllers/serverController.h"
#include "core/defs.h"
#include "containers/containers_defs.h"
#include "core/utils/selfhosted/sshSession.h"
using namespace amnezia;
@@ -31,7 +34,7 @@ class ServersBackupController : public QObject
Q_OBJECT
public:
explicit ServersBackupController(std::shared_ptr<Settings> settings, ServersModel *serversModel, QObject *parent = nullptr);
explicit ServersBackupController(SecureQSettings *settings, ServersModel *serversModel, QObject *parent = nullptr);
~ServersBackupController();
/**
@@ -360,10 +363,17 @@ private:
*/
void trySetDefaultContainer();
ErrorCode runHostScript(const ServerCredentials &credentials, const QString &script,
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdOut = nullptr,
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdErr = nullptr);
ErrorCode downloadFileFromHost(const ServerCredentials &credentials, const QString &remotePath, const QString &localPath);
ErrorCode uploadFileToHostPublic(const ServerCredentials &credentials, const QString &localPath, const QString &remotePath,
libssh::ScpOverwriteMode overwriteMode = libssh::ScpOverwriteMode::ScpOverwriteExisting);
private:
std::shared_ptr<Settings> m_settings;
QPointer<SecureQSettings> m_settings;
ServersModel *m_serversModel;
ServerController *m_serverController;
SshSession m_sshSession;
BackupStatus m_status;
QString m_backupDir;
QString m_currentOutput;
+3
View File
@@ -106,8 +106,11 @@
<file>Pages2/PageSettingsKillSwitch.qml</file>
<file>Pages2/PageSettingsKillSwitchExceptions.qml</file>
<file>Pages2/PageSettingsLogging.qml</file>
<file>Pages2/PageSettingsServerBackup.qml</file>
<file>Pages2/PageSettingsServerBackupRestored.qml</file>
<file>Pages2/PageSettingsServerData.qml</file>
<file>Pages2/PageSettingsServerInfo.qml</file>
<file>Pages2/PageSettingsServerRestoreMode.qml</file>
<file>Pages2/PageSettingsServerProtocol.qml</file>
<file>Pages2/PageSettingsServerProtocols.qml</file>
<file>Pages2/PageSettingsServerServices.qml</file>