Problem: Backups wurden ohne Root-Rechte erstellt. Dadurch wurden Datei- berechtigungen (UID/GID, rwx, ACLs, xattrs) nicht korrekt uebernommen. Root-Dateien konnten nicht gelesen werden. Backups waren unvollstaendig. Loesung: - rsync_manager: create_snapshot() nutzt jetzt sudo fuer rsync - rsync_manager: --numeric-ids Flag hinzugefuegt fuer korrekte UID/GID - snapshot_manager: mkdir mit sudo fuer Root-Besitz der Snapshots - snapshot_manager: delete_snapshot mit sudo (Snapshots sind Root-owned) - main_window: Sudo-Passwort-Dialog vor jedem Backup - main_window: Dialog-Text erweitert (Backup erfordert sudo) - main.cpp: --sudo-password Argument fuer CLI/Cron-Modus - main.cpp: Interaktive Passwort-Eingabe im CLI-Modus - README.md: Build-Abhaengigkeiten fuer alle Distros hinzugefuegt - README.md: Rechte-Tabelle aktualisiert - README.md: Cronjob-Beispiel mit Passwort-Speicherung aktualisiert Betroffene Dateien: src/rsync_manager.h, src/rsync_manager.cpp src/snapshot_manager.h, src/snapshot_manager.cpp src/main_window.h, src/main_window.cpp src/main.cpp README.md
170 lines
4.7 KiB
C++
170 lines
4.7 KiB
C++
#include <gtk/gtk.h>
|
|
#include <glib.h>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <cstdio>
|
|
#include <ctime>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include "config.h"
|
|
#include "rsync_manager.h"
|
|
#include "snapshot_manager.h"
|
|
#include "main_window.h"
|
|
|
|
static FILE* log_file = nullptr;
|
|
static bool log_to_stdout = false;
|
|
|
|
static std::string get_log_path() {
|
|
const char* home = getenv("HOME");
|
|
if (!home) home = ".";
|
|
std::string dir = std::string(home) + "/.config/x3-backup";
|
|
std::string cmd = "mkdir -p \"" + dir + "\"";
|
|
(void)system(cmd.c_str());
|
|
return dir + "/x3-backup.log";
|
|
}
|
|
|
|
static void log_handler(const gchar* domain, GLogLevelFlags level, const gchar* message, gpointer) {
|
|
const char* level_str = "INFO";
|
|
if (level & G_LOG_LEVEL_WARNING) level_str = "WARN";
|
|
if (level & G_LOG_LEVEL_CRITICAL) level_str = "CRIT";
|
|
if (level & G_LOG_LEVEL_ERROR) level_str = "ERROR";
|
|
|
|
time_t now = time(nullptr);
|
|
struct tm* t = localtime(&now);
|
|
char timebuf[32];
|
|
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", t);
|
|
|
|
char line[2048];
|
|
snprintf(line, sizeof(line), "[%s] [%s] %s: %s\n", timebuf, level_str, domain ? domain : "app", message);
|
|
|
|
if (log_to_stdout) {
|
|
fputs(line, stdout);
|
|
fflush(stdout);
|
|
}
|
|
if (log_file) {
|
|
fputs(line, log_file);
|
|
fflush(log_file);
|
|
}
|
|
}
|
|
|
|
static void setup_logging(bool cli_mode) {
|
|
const char* env_log = g_getenv("X3_BACKUP_LOG");
|
|
if (env_log && strcmp(env_log, "1") == 0) {
|
|
log_to_stdout = true;
|
|
}
|
|
|
|
if (cli_mode) {
|
|
log_to_stdout = true;
|
|
}
|
|
|
|
std::string path = get_log_path();
|
|
log_file = fopen(path.c_str(), "a");
|
|
if (!log_file) {
|
|
g_warning("Cannot open log file: %s", path.c_str());
|
|
}
|
|
|
|
g_log_set_handler("x3-backup", (GLogLevelFlags)(G_LOG_LEVEL_MASK), log_handler, nullptr);
|
|
g_log_set_handler(nullptr, (GLogLevelFlags)(G_LOG_LEVEL_MASK), log_handler, nullptr);
|
|
|
|
g_message("=== x3-backup started ===");
|
|
g_message("Log file: %s", path.c_str());
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
gtk_init(&argc, &argv);
|
|
|
|
bool system_mode = false;
|
|
bool backup_now = false;
|
|
bool show_help = false;
|
|
bool cli_mode = false;
|
|
std::string sudo_password;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
if (strcmp(argv[i], "--system-backup") == 0) {
|
|
system_mode = true;
|
|
backup_now = true;
|
|
cli_mode = true;
|
|
} else if (strcmp(argv[i], "--backup") == 0) {
|
|
backup_now = true;
|
|
cli_mode = true;
|
|
} else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
|
|
show_help = true;
|
|
} else if (strcmp(argv[i], "--log") == 0) {
|
|
log_to_stdout = true;
|
|
} else if (strcmp(argv[i], "--sudo-password") == 0 && i + 1 < argc) {
|
|
sudo_password = argv[++i];
|
|
}
|
|
}
|
|
|
|
setup_logging(cli_mode);
|
|
|
|
if (show_help) {
|
|
g_print("x3-Backup - GTK3 Backup/Snapshot Tool\n\n");
|
|
g_print("Usage: x3-backup [OPTIONS]\n\n");
|
|
g_print("Options:\n");
|
|
g_print(" --backup Create a backup immediately and exit\n");
|
|
g_print(" --system-backup Create a full system backup and exit\n");
|
|
g_print(" --sudo-password PW Provide sudo password for backup (required for root permissions)\n");
|
|
g_print(" --log Print log to stdout\n");
|
|
g_print(" --help, -h Show this help\n\n");
|
|
g_print("Without options, x3-backup starts in GUI mode with system tray.\n\n");
|
|
g_print("Note: Backups require sudo to preserve file ownership and permissions.\n");
|
|
g_print(" Use --sudo-password or run interactively for password prompt.\n\n");
|
|
g_print("Log file: ~/.config/x3-backup/x3-backup.log\n");
|
|
return 0;
|
|
}
|
|
|
|
Config config;
|
|
config.load();
|
|
|
|
if (backup_now) {
|
|
if (system_mode) {
|
|
config.set_system_backup_mode(true);
|
|
config.settings().source_path = "/";
|
|
g_message("System backup mode: backing up entire system");
|
|
}
|
|
|
|
/* Prompt for sudo password if not provided */
|
|
if (sudo_password.empty()) {
|
|
g_print("Sudo password required for backup (to preserve file permissions):\n");
|
|
g_print("Password: ");
|
|
fflush(stdout);
|
|
char* pw = nullptr;
|
|
size_t len = 0;
|
|
ssize_t nread = getline(&pw, &len, stdin);
|
|
if (nread > 0 && pw[nread - 1] == '\n') pw[nread - 1] = '\0';
|
|
if (pw && *pw) {
|
|
sudo_password = std::string(pw);
|
|
}
|
|
free(pw);
|
|
if (sudo_password.empty()) {
|
|
g_warning("No password provided, backup may have incorrect permissions");
|
|
}
|
|
}
|
|
|
|
RsyncManager rsync(config);
|
|
SnapshotManager snapshots(config, rsync);
|
|
|
|
g_message("Starting backup with sudo...");
|
|
snapshots.cleanup_old_snapshots();
|
|
std::string ts = snapshots.create_snapshot(
|
|
system_mode ? "System backup" : "Manual backup",
|
|
sudo_password);
|
|
if (!ts.empty()) {
|
|
g_message("Backup completed: %s", ts.c_str());
|
|
} else {
|
|
g_warning("Backup failed!");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MainWindow window(config);
|
|
window.run();
|
|
|
|
g_message("=== x3-backup exiting ===");
|
|
if (log_file) fclose(log_file);
|
|
|
|
return 0;
|
|
}
|