Working on backup/restore.
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
This commit is contained in:
parent
263edd9b50
commit
fb6974b51a
@ -16,18 +16,19 @@
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
|
||||
|
||||
namespace dropshell {
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int backupdata_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> backupdata_name_list = {"backupdata", "bd", "backup", "bup"};
|
||||
|
||||
// Static registration
|
||||
struct BackupDataCommandRegister {
|
||||
BackupDataCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
backupdata_name_list,
|
||||
struct BackupDataCommandRegister
|
||||
{
|
||||
BackupDataCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({backupdata_name_list,
|
||||
backupdata_handler,
|
||||
shared_commands::std_autocomplete_allowall,
|
||||
false, // hidden
|
||||
@ -45,11 +46,12 @@ struct BackupDataCommandRegister {
|
||||
Note: This command will not create any data or configuration.
|
||||
It will simply backup the data on the remote server, saving it to a local file.
|
||||
Restore the data with restore.
|
||||
)"
|
||||
});
|
||||
)"});
|
||||
}
|
||||
} backupdata_command_register;
|
||||
|
||||
namespace shared_commands
|
||||
{
|
||||
|
||||
bool backupdata_service(const std::string &server, const std::string &service)
|
||||
{
|
||||
@ -69,7 +71,8 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
|
||||
const std::string command = "backup";
|
||||
|
||||
if (!gTemplateManager().template_command_exists(sinfo.template_name, command)) {
|
||||
if (!gTemplateManager().template_command_exists(sinfo.template_name, command))
|
||||
{
|
||||
info << service << " has no data to backup" << std::endl;
|
||||
debug << "(no backup script for " << sinfo.template_name << ")" << std::endl;
|
||||
return true; // nothing to back up.
|
||||
@ -79,11 +82,9 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
std::string remote_service_template_path = remotepath::service_template(server, service);
|
||||
std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
||||
std::string remote_service_config_path = remotepath::service_config(server, service);
|
||||
if (!server_env.check_remote_items_exist({
|
||||
remotepath::service(server, service),
|
||||
if (!server_env.check_remote_items_exist({remotepath::service(server, service),
|
||||
remote_command_script_file,
|
||||
remotefile::service_env(server, service)})
|
||||
)
|
||||
remotefile::service_env(server, service)}))
|
||||
{
|
||||
error << "Error: Required service directories not found on remote server" << std::endl;
|
||||
info << "Is the service installed?" << std::endl;
|
||||
@ -94,14 +95,16 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
std::string remote_backups_dir = remotepath::backups(server);
|
||||
debug << "Remote backups directory on " << server << ": " << remote_backups_dir << std::endl;
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("",mkdir_cmd, {}), cMode::Defaults)) {
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Defaults))
|
||||
{
|
||||
error << "Failed to create backups directory on server" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create backups directory locally if it doesn't exist
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty()) {
|
||||
if (local_backups_dir.empty())
|
||||
{
|
||||
error << "Error: Local backups directory not found" << std::endl;
|
||||
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
@ -111,7 +114,8 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
|
||||
// Get current datetime for backup filename
|
||||
shared_commands::cBackupFileName backup_filename_construction(server, service, sinfo.template_name);
|
||||
if (!backup_filename_construction.is_valid()) {
|
||||
if (!backup_filename_construction.is_valid())
|
||||
{
|
||||
error << "Invalid backup filename" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@ -126,13 +130,15 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
|
||||
{ // Run backup script
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
|
||||
if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) {
|
||||
if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}}))
|
||||
{
|
||||
error << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy backup file from server to local
|
||||
if (!shared_commands::scp_file_from_remote(server_env, remote_backup_file_path, local_backup_file_path, false)) {
|
||||
if (!shared_commands::scp_file_from_remote(server_env, remote_backup_file_path, local_backup_file_path, false))
|
||||
{
|
||||
error << "Failed to copy backup file from server" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@ -142,31 +148,27 @@ bool backupdata_service(const std::string& server, const std::string& service)
|
||||
info << " dropshell restore " << server << " " << service << " " << backup_filename << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace shared_commands
|
||||
|
||||
int backupdata_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{
|
||||
error << "Server name is required" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
ASSERT(ctx.args.size() == 2, "Invalid number of arguments");
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
|
||||
if (ctx.args.size() < 2)
|
||||
if (service == "all")
|
||||
{
|
||||
// backup all services on the server
|
||||
maketitle("Backing up data for all services on " + server);
|
||||
bool okay = true;
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
okay &= backupdata_service(server, service.service_name);
|
||||
for (const LocalServiceInfo &si : services)
|
||||
okay &= shared_commands::backupdata_service(server, si.service_name);
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return backupdata_service(server, service);
|
||||
return shared_commands::backupdata_service(server, service) ? 0 : 1;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@ -11,7 +11,7 @@ namespace dropshell
|
||||
{
|
||||
|
||||
int create_service_handler(const CommandContext &ctx);
|
||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, bool silent);
|
||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name);
|
||||
void create_service_autocomplete(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> create_service_name_list = {"create-service"};
|
||||
@ -45,7 +45,7 @@ namespace dropshell
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
std::string template_name = safearg(ctx.args, 2);
|
||||
|
||||
return create_service(server, template_name, service, false) ? 0 : 1;
|
||||
return create_service(server, template_name, service) ? 0 : 1;
|
||||
}
|
||||
|
||||
void create_service_autocomplete(const CommandContext &ctx)
|
||||
@ -58,12 +58,12 @@ namespace dropshell
|
||||
{
|
||||
std::set<std::string> templates = gTemplateManager().get_template_list();
|
||||
for (const auto &template_name : templates)
|
||||
std::cout << template_name << std::endl;
|
||||
rawout << template_name << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, bool silent)
|
||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name)
|
||||
{
|
||||
if (server_name.empty() || template_name.empty() || service_name.empty())
|
||||
return false;
|
||||
@ -72,44 +72,34 @@ namespace dropshell
|
||||
|
||||
if (service_dir.empty())
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
||||
std::cerr << "Please check the server name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all servers with 'dropshell servers'" << std::endl;
|
||||
std::cerr << "You can create a new server with 'dropshell create-server " << server_name << "'" << std::endl;
|
||||
}
|
||||
error << "Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
||||
info << "Please check the server name is correct and try again" << std::endl;
|
||||
info << "You can list all servers with 'dropshell servers'" << std::endl;
|
||||
info << "You can create a new server with 'dropshell create-server " << server_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (std::filesystem::exists(service_dir))
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Service already exists: " << service_name << std::endl;
|
||||
std::cerr << "Current service path: " << service_dir << std::endl;
|
||||
}
|
||||
error << "Service already exists: " << service_name << std::endl;
|
||||
debug << "Current service path: " << service_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
template_info tinfo = gTemplateManager().get_template_info(template_name);
|
||||
if (!tinfo.is_set())
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
|
||||
std::cerr << "Please check the template name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all templates with 'dropshell templates'" << std::endl;
|
||||
std::cerr << "You can create a new template with 'dropshell create-template " << template_name << "'" << std::endl;
|
||||
}
|
||||
error << "Template '" << template_name << "' not found" << std::endl;
|
||||
info << "Please check the template name is correct and try again" << std::endl;
|
||||
info << "You can list all templates with 'dropshell templates'" << std::endl;
|
||||
info << "You can create a new template with 'dropshell create-template " << template_name << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// check template is all good.
|
||||
if (!gTemplateManager().test_template(tinfo.local_template_path()))
|
||||
{
|
||||
if (!silent)
|
||||
std::cerr << "Error: Template '" << template_name << "' is not valid" << std::endl;
|
||||
error << "Template '" << template_name << "' is not valid" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -119,14 +109,11 @@ namespace dropshell
|
||||
// copy the template config files to the service directory
|
||||
recursive_copy(tinfo.local_template_path() / "config", service_dir);
|
||||
|
||||
if (!silent)
|
||||
{
|
||||
std::cout << "Service " << service_name << " created successfully" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "To complete the installation, please:" << std::endl;
|
||||
std::cout << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
|
||||
std::cout << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl;
|
||||
}
|
||||
info << "Service " << service_name << " created successfully" << std::endl;
|
||||
info << std::endl;
|
||||
info << "To complete the installation, please:" << std::endl;
|
||||
info << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
|
||||
info << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -53,11 +53,13 @@ namespace dropshell
|
||||
}
|
||||
} install_command_register;
|
||||
|
||||
namespace shared_commands
|
||||
{
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// install service over ssh : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool install_service(const std::string &server, const std::string &service, bool silent)
|
||||
bool install_service(const std::string &server, const std::string &service)
|
||||
{
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
@ -104,7 +106,7 @@ namespace dropshell
|
||||
debug << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_template(server, service) << "/" << std::endl;
|
||||
if (!shared_commands::rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(server, service),
|
||||
server_env, silent))
|
||||
server_env, false))
|
||||
{
|
||||
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
||||
return false;
|
||||
@ -114,7 +116,7 @@ namespace dropshell
|
||||
debug << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_config(server, service) << std::endl;
|
||||
if (!shared_commands::rsync_tree_to_remote(localpath::service(server, service), remotepath::service_config(server, service),
|
||||
server_env, silent))
|
||||
server_env, false))
|
||||
{
|
||||
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
||||
return false;
|
||||
@ -123,7 +125,7 @@ namespace dropshell
|
||||
// Run install script
|
||||
{
|
||||
info << "Running " << service_info.template_name << " install script on " << server << "..." << std::endl;
|
||||
server_env.run_remote_template_command(service, "install", {}, silent, {});
|
||||
server_env.run_remote_template_command(service, "install", {}, false, {});
|
||||
}
|
||||
|
||||
// print health tick
|
||||
@ -131,6 +133,7 @@ namespace dropshell
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace shared_commands
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// update_dropshell
|
||||
@ -305,7 +308,8 @@ namespace dropshell
|
||||
info << "done." << std::endl;
|
||||
|
||||
// add in bb64. We can't use execute_remote_command() here, as that relies on bb64 which we're installing!
|
||||
info << "Installing bb64 on " << server << "..." << std::endl << std::flush;
|
||||
info << "Installing bb64 on " << server << "..." << std::endl
|
||||
<< std::flush;
|
||||
|
||||
std::string remote_cmd =
|
||||
"ssh -p " + server_env.get_SSH_INFO().port + " " + server_env.get_SSH_INFO().user + "@" + server_env.get_SSH_INFO().host +
|
||||
@ -362,7 +366,7 @@ namespace dropshell
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!install_service(server, service.service_name, false))
|
||||
if (!shared_commands::install_service(server, service.service_name))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
@ -370,7 +374,7 @@ namespace dropshell
|
||||
else
|
||||
{ // install the specific service.
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return install_service(server, service, false) ? 0 : 1;
|
||||
return shared_commands::install_service(server, service) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,16 +10,18 @@
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int nuke_handler(const CommandContext &ctx);
|
||||
static std::vector<std::string> nuke_name_list = {"nuke"};
|
||||
|
||||
// Static registration
|
||||
struct NukeCommandRegister {
|
||||
NukeCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
nuke_name_list,
|
||||
struct NukeCommandRegister
|
||||
{
|
||||
NukeCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({nuke_name_list,
|
||||
nuke_handler,
|
||||
shared_commands::std_autocomplete,
|
||||
false, // hidden
|
||||
@ -41,12 +43,14 @@ struct NukeCommandRegister {
|
||||
both on the dropshell host and on the remote server.
|
||||
|
||||
Use with caution!
|
||||
)"
|
||||
});
|
||||
)"});
|
||||
}
|
||||
} nuke_command_register;
|
||||
|
||||
int nuke_one(std::string server, std::string service)
|
||||
namespace shared_commands
|
||||
{
|
||||
|
||||
bool nuke_service(const std::string &server, const std::string &service)
|
||||
{
|
||||
server_env_manager server_env(server);
|
||||
|
||||
@ -107,8 +111,9 @@ int nuke_one(std::string server, std::string service)
|
||||
|
||||
info << "Nuked service " << service << " on server " << server << std::endl;
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
} // namespace shared_commands
|
||||
|
||||
int nuke_handler(const CommandContext &ctx)
|
||||
{
|
||||
@ -135,14 +140,14 @@ int nuke_handler(const CommandContext &ctx)
|
||||
if (entry.is_directory() && entry.path().filename().string().find(".") != 0)
|
||||
{
|
||||
std::string service_name = entry.path().filename().string();
|
||||
rval |= nuke_one(server, service_name);
|
||||
rval |= (shared_commands::nuke_service(server, service_name) ? 0 : 1);
|
||||
}
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nuke_one(server, service);
|
||||
return (shared_commands::nuke_service(server, service) ? 0 : 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,21 +16,21 @@
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
|
||||
|
||||
namespace dropshell {
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int restoredata_handler(const CommandContext &ctx);
|
||||
|
||||
void restoredata_autocomplete(const CommandContext &ctx);
|
||||
|
||||
|
||||
static std::vector<std::string> restoredata_name_list = {"restoredata", "rd", "restore", "rest"};
|
||||
|
||||
// Static registration
|
||||
struct RestoreDataCommandRegister {
|
||||
RestoreDataCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
restoredata_name_list,
|
||||
struct RestoreDataCommandRegister
|
||||
{
|
||||
RestoreDataCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({restoredata_name_list,
|
||||
restoredata_handler,
|
||||
restoredata_autocomplete,
|
||||
false, // hidden
|
||||
@ -38,49 +38,177 @@ struct RestoreDataCommandRegister {
|
||||
true, // requires_install
|
||||
3, // min_args (after command)
|
||||
3, // max_args (after command)
|
||||
"restoredata SERVER SERVICE BACKUP_FILE",
|
||||
"restoredata SERVER SERVICE BACKUP_FILE|latest",
|
||||
"Restore data for a service on a server, overwriting the existing data.",
|
||||
// heredoc
|
||||
R"(
|
||||
restoredata SERVER SERVICE BACKUP_FILE Restore data to a service on a server. Destructive.
|
||||
restoredata SERVER SERVICE latest Restore the latest backup for the given service.
|
||||
|
||||
Note: This command will not create any service configuration, you need
|
||||
to have a valid service installed first.
|
||||
The backup file must be in the local backups directory.
|
||||
|
||||
WARNING: This will permanently overwrite the service's data on the remote server!
|
||||
)"
|
||||
});
|
||||
)"});
|
||||
}
|
||||
} restoredata_command_register;
|
||||
|
||||
|
||||
std::vector<shared_commands::cBackupFileName> get_backup_files(const std::string &server, const std::string &match_service = "", const std::string &match_template_name = "")
|
||||
{
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||
{
|
||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<shared_commands::cBackupFileName> backups;
|
||||
for (const auto &entry : std::filesystem::directory_iterator(local_backups_dir))
|
||||
{
|
||||
if (!entry.is_regular_file())
|
||||
continue;
|
||||
std::string filename = entry.path().filename().string();
|
||||
shared_commands::cBackupFileName backup_details(filename);
|
||||
if (backup_details.is_valid())
|
||||
if (match_service.empty() || backup_details.get_service() == match_service)
|
||||
if (match_template_name.empty() || backup_details.get_template_name() == match_template_name)
|
||||
backups.push_back(backup_details);
|
||||
}
|
||||
|
||||
// sort backups by datetime
|
||||
std::sort(backups.begin(), backups.end(), [](const shared_commands::cBackupFileName &a, const shared_commands::cBackupFileName &b)
|
||||
{ return a.get_datetime() > b.get_datetime(); });
|
||||
|
||||
return backups;
|
||||
}
|
||||
|
||||
|
||||
int restoredata_handler(const CommandContext &ctx)
|
||||
{
|
||||
ASSERT(ctx.args.size() == 2, "Invalid number of arguments");
|
||||
ASSERT(ctx.args.size() == 3, "Invalid number of arguments");
|
||||
std::string server = ctx.args[0];
|
||||
std::string service = ctx.args[1];
|
||||
std::string backup_file = ctx.args[2];
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid()) {
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
error << "Server " << server << " is not valid" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
{
|
||||
error << "Service " << service << " is not valid" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
shared_commands::cBackupFileName backup_details(backup_file);
|
||||
if (!backup_details.is_valid()) {
|
||||
std::optional<shared_commands::cBackupFileName> backup_details;
|
||||
if (backup_file == "latest")
|
||||
{ // special case.
|
||||
std::vector<shared_commands::cBackupFileName> backups = get_backup_files(server, service, service_info.template_name); // this service only (and also match template in case something changed there!).
|
||||
if (backups.empty())
|
||||
{
|
||||
error << "No backups found for " << server << "/" << service << std::endl;
|
||||
debug << "Template also has to match with the service template: " << service_info.template_name << std::endl;
|
||||
return 1;
|
||||
}
|
||||
backup_details = backups[0];
|
||||
} else {
|
||||
backup_details = shared_commands::cBackupFileName(backup_file);
|
||||
}
|
||||
|
||||
bool backup_details_okay = backup_details.has_value() && backup_details->is_valid();
|
||||
if (!backup_details_okay)
|
||||
{
|
||||
error << "Invalid backup file: " << backup_file << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||
{
|
||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string();
|
||||
if (!std::filesystem::exists(local_backup_file_path))
|
||||
{
|
||||
error << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (backup_details->get_template_name() != service_info.template_name)
|
||||
{
|
||||
error << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
||||
debug << "Backup template: " << backup_details->get_template_name() << std::endl;
|
||||
debug << "Service template: " << service_info.template_name << std::endl;
|
||||
debug << "Backup file: " << local_backup_file_path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
info << "Restoring " << backup_details->get_datetime() << " backup of " << backup_details->get_service() << " taken from " << backup_details->get_server() << ", onto " << server << "/" << service << std::endl;
|
||||
info << std::endl;
|
||||
warning << "*** ALL DATA FOR " << server << "/" << service << " WILL BE OVERWRITTEN! ***" << std::endl;
|
||||
|
||||
// run the restore script
|
||||
info << "OK, here goes..." << std::endl;
|
||||
|
||||
{ // backup existing service
|
||||
info << "1) Backing up old service... " << std::endl;
|
||||
if (!shared_commands::backupdata_service(server, service))
|
||||
{
|
||||
error << "Error: Backup failed, restore aborted." << std::endl;
|
||||
error << "You can try using dropshell install " << server << " " << service << " to install the service afresh." << std::endl;
|
||||
error << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
info << "Backup complete." << std::endl;
|
||||
}
|
||||
|
||||
{ // nuke the old service
|
||||
info << "2) Nuking old service..." << std::endl;
|
||||
if (!shared_commands::nuke_service(server, service))
|
||||
return 1;
|
||||
}
|
||||
|
||||
{ // restore service from backup
|
||||
info << "3) Restoring service data from backup..." << std::endl;
|
||||
std::string remote_backups_dir = remotepath::backups(server);
|
||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_file;
|
||||
|
||||
// Copy backup file from local to server
|
||||
if (!shared_commands::scp_file_to_remote(server_env, local_backup_file_path, remote_backup_file_path, false))
|
||||
{
|
||||
error << "Failed to copy backup file from local to server" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
|
||||
server_env.run_remote_template_command(service, "restore", {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||
|
||||
{ // installing fresh service
|
||||
info << "4) Non-destructive install of fresh service..." << std::endl;
|
||||
if (!shared_commands::install_service(server, service))
|
||||
return 1;
|
||||
}
|
||||
|
||||
{ // healthcheck the service
|
||||
info << "5) Healthchecking service..." << std::endl;
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
bool healthy = (server_env.run_remote_template_command(service, "status", {}, false, {}));
|
||||
info << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void restoredata_autocomplete(const CommandContext &ctx)
|
||||
{
|
||||
shared_commands::std_autocomplete(ctx);
|
||||
@ -89,49 +217,27 @@ void restoredata_autocomplete(const CommandContext& ctx)
|
||||
std::string server = ctx.args[0];
|
||||
std::string service = ctx.args[1];
|
||||
|
||||
std::vector<shared_commands::cBackupFileName> backups;
|
||||
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info)) {
|
||||
if (!SIvalid(service_info))
|
||||
{
|
||||
error << "Service " << service << " is not valid" << std::endl;
|
||||
return;
|
||||
}
|
||||
std::string template_name = service_info.template_name;
|
||||
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) {
|
||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||
return;
|
||||
}
|
||||
for (const auto& entry : std::filesystem::directory_iterator(local_backups_dir)) {
|
||||
if (!entry.is_regular_file()) continue;
|
||||
std::string filename = entry.path().filename().string();
|
||||
shared_commands::cBackupFileName backup_details(filename);
|
||||
if (backup_details.is_valid()) {
|
||||
if (backup_details.get_template_name() == template_name) {
|
||||
backups.push_back(backup_details);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sort backups by datetime
|
||||
std::sort(backups.begin(), backups.end(), [](const shared_commands::cBackupFileName& a, const shared_commands::cBackupFileName& b) {
|
||||
return a.get_datetime() > b.get_datetime();
|
||||
});
|
||||
std::vector<shared_commands::cBackupFileName> backups = get_backup_files(server, "", template_name); // any service, but must match template.
|
||||
|
||||
// print most recent backup for each {host,service} pair
|
||||
std::map<std::string, std::string> unique_backups;
|
||||
for (const auto& backup : backups) {
|
||||
for (const auto &backup : backups)
|
||||
{
|
||||
std::string key = backup.get_server() + "-" + backup.get_service();
|
||||
if (unique_backups.find(key) == unique_backups.end()) {
|
||||
if (unique_backups.find(key) == unique_backups.end())
|
||||
unique_backups[key] = backup.get_filename();
|
||||
}
|
||||
}
|
||||
for (const auto& [key, value] : unique_backups) {
|
||||
for (const auto &[key, value] : unique_backups)
|
||||
rawout << value << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace dropshell
|
@ -81,6 +81,18 @@ namespace dropshell
|
||||
bool scp_file_to_remote(const server_env_manager &server_env, const std::string &local_path, const std::string &remote_path, bool silent);
|
||||
bool scp_file_from_remote(const server_env_manager &server_env, const std::string &remote_path, const std::string &local_path, bool silent);
|
||||
|
||||
// defined in backupdata.cpp, used by restoredata.cpp.
|
||||
bool backupdata_service(const std::string& server, const std::string& service);
|
||||
|
||||
// defined in uninstall.cpp
|
||||
bool uninstall_service(const std::string &server, const std::string &service);
|
||||
|
||||
// defined in nuke.cpp
|
||||
bool nuke_service(const std::string &server, const std::string &service);
|
||||
|
||||
// defined in install.cpp
|
||||
bool install_service(const std::string &server, const std::string &service);
|
||||
|
||||
} // namespace shared_commands
|
||||
} // namespace dropshell
|
||||
#endif
|
||||
|
@ -41,9 +41,9 @@ namespace dropshell
|
||||
}
|
||||
} uninstall_command_register;
|
||||
|
||||
bool uninstall_service(const std::string &server, const std::string &service, bool silent = false)
|
||||
namespace shared_commands {
|
||||
bool uninstall_service(const std::string &server, const std::string &service)
|
||||
{
|
||||
if (!silent)
|
||||
maketitle("Uninstalling " + service + " on " + server);
|
||||
|
||||
server_env_manager server_env(server);
|
||||
@ -62,24 +62,22 @@ namespace dropshell
|
||||
|
||||
// 3. Run uninstall script if it exists
|
||||
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
|
||||
if (!silent)
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||
warning << "Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
|
||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), silent))
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), false))
|
||||
{
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||
if (!silent)
|
||||
info << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
||||
}
|
||||
else if (!silent)
|
||||
else
|
||||
warning << "Failed to remove remote service directory" << std::endl;
|
||||
|
||||
if (!silent)
|
||||
info << "Completed service " << service << " uninstall on " << server << std::endl;
|
||||
return true;
|
||||
}
|
||||
} // namespace shared_commands
|
||||
|
||||
int uninstall_handler(const CommandContext &ctx)
|
||||
{
|
||||
@ -98,14 +96,14 @@ namespace dropshell
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!uninstall_service(server, service.service_name))
|
||||
if (!shared_commands::uninstall_service(server, service.service_name))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return uninstall_service(server, service) ? 0 : 1;
|
||||
return shared_commands::uninstall_service(server, service) ? 0 : 1;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
Loading…
x
Reference in New Issue
Block a user