Restore command

This commit is contained in:
John 2025-04-27 19:56:44 +12:00
parent c1b9b2a152
commit 59561824c7
5 changed files with 19 additions and 82 deletions

View File

@ -71,10 +71,15 @@ int restore(const std::vector<std::string> &args)
return 1;
}
server_env env(server_name);
if (!env.is_valid()) {
std::cerr << "Error: Invalid server environment" << std::endl;
return 1;
}
std::string local_backups_dir = get_local_backup_path();
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string();
if (! std::filesystem::exists(local_backup_file_path)) {
std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl;
return 1;
@ -127,25 +132,25 @@ int restore(const std::vector<std::string> &args)
}
std::cout << "Backup complete." << std::endl;
}
{ // uninstalling the current service
{ // silently uninstalling the current service
std::cout << "2) Uninstalling current service..." << std::endl;
if (!uninstall(uninstall_args,true)) // silent=true
{
std::cerr << std::endl;
std::cerr << "Error: Uninstall failed, restore aborted." << std::endl;
return 1;
}
env.run_remote_template_command(service_name, "uninstall", {}, true);
}
{ // restore service from backup
std::cout << "2) Restoring service from backup..." << std::endl;
std::cout << "3) Restoring service from backup..." << std::endl;
env.run_remote_template_command(service_name, "restore", {local_backup_file_path}, true);
}
{ // initialise service
std::cout << "3) Initialising service..." << std::endl;
}
// healthcheck the service
std::cout << "4) Healthchecking service..." << std::endl;
std::string green_tick = "\033[32m✓\033[0m";
std::string red_cross = "\033[31m✗\033[0m";
if (!env.run_remote_template_command(service_name, "status", {}, true))
std::cout << green_tick << " Service is healthy." << std::endl;
else
std::cout << red_cross << " Service is NOT healthy." << std::endl;
// run the restore script
return 0;
}

View File

@ -281,8 +281,7 @@ HealthStatus service_runner::is_healthy()
}
// Run status script, does not display output.
std::vector<std::string> args; // not passed through yet.
if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", args))
if (!m_server_env.run_remote_template_command(m_service_info.service_name, "status", {}, true))
return HealthStatus::UNHEALTHY;
return HealthStatus::HEALTHY;
}

View File

@ -25,6 +25,4 @@ namespace dropshell {
bool create_service(const std::string& server_name, const std::string& template_name, const std::string& service_name, bool silent=false);
} // namespace dropshell
#endif

View File

@ -1,58 +0,0 @@
#include "status.hpp"
#include <iostream>
#include <fstream>
#include <chrono>
#include <ctime>
#include <sys/statvfs.h>
#include <sys/sysinfo.h>
namespace dropshell {
void check_status() {
// Get current time
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::cout << "System Status:" << std::endl;
std::cout << "Date: " << std::ctime(&time);
// Get uptime
struct sysinfo si;
if (sysinfo(&si) == 0) {
int days = si.uptime / 86400;
int hours = (si.uptime % 86400) / 3600;
int minutes = (si.uptime % 3600) / 60;
std::cout << "Uptime: " << days << " days, " << hours << " hours, " << minutes << " minutes" << std::endl;
}
// Get memory usage
std::ifstream meminfo("/proc/meminfo");
if (meminfo.is_open()) {
std::string line;
long total_mem = 0, free_mem = 0;
while (std::getline(meminfo, line)) {
if (line.find("MemTotal:") == 0) {
total_mem = std::stol(line.substr(9));
} else if (line.find("MemAvailable:") == 0) {
free_mem = std::stol(line.substr(13));
}
}
std::cout << "Memory Usage:" << std::endl;
std::cout << "Total: " << total_mem / 1024 << " MB" << std::endl;
std::cout << "Available: " << free_mem / 1024 << " MB" << std::endl;
}
// Get disk usage
struct statvfs vfs;
if (statvfs("/", &vfs) == 0) {
uint64_t total = vfs.f_blocks * vfs.f_frsize;
uint64_t free = vfs.f_bfree * vfs.f_frsize;
uint64_t used = total - free;
std::cout << "Disk Usage:" << std::endl;
std::cout << "Total: " << total / (1024*1024*1024) << " GB" << std::endl;
std::cout << "Used: " << used / (1024*1024*1024) << " GB" << std::endl;
std::cout << "Free: " << free / (1024*1024*1024) << " GB" << std::endl;
}
}
} // namespace dropshell

View File

@ -1,7 +0,0 @@
#ifndef STATUS_HPP
#define STATUS_HPP
namespace dropshell {
void check_status();
}
#endif