feat: nexus backup ohne Migration, install.sh sichert die Datenbank vor jedem Update und nennt sie beim Rückfall
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -27,6 +27,30 @@ const MIGRATIONS: &[&str] = &[
|
|||||||
/// Schema-Version, die dieses Crate erzeugt.
|
/// Schema-Version, die dieses Crate erzeugt.
|
||||||
pub const LATEST_SCHEMA_VERSION: i64 = MIGRATIONS.len() as i64;
|
pub const LATEST_SCHEMA_VERSION: i64 = MIGRATIONS.len() as i64;
|
||||||
|
|
||||||
|
/// Schreibt eine vollständige, in sich stimmige Kopie der Datenbank nach
|
||||||
|
/// `out` (`VACUUM INTO`). Die Datenbank wird nur lesend geöffnet und **nicht**
|
||||||
|
/// migriert, damit die Sicherung vor einem Update den alten Stand enthält.
|
||||||
|
/// Das geht auch, während das Gateway läuft. `out` darf noch nicht existieren.
|
||||||
|
pub fn backup_file(db_path: &std::path::Path, out: &std::path::Path) -> Result<()> {
|
||||||
|
if out.exists() {
|
||||||
|
return Err(Error::Db(format!("{} gibt es schon", out.display())));
|
||||||
|
}
|
||||||
|
if let Some(parent) = out.parent() {
|
||||||
|
std::fs::create_dir_all(parent).map_err(|source| Error::Io {
|
||||||
|
path: parent.to_path_buf(),
|
||||||
|
source,
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
let conn = rusqlite::Connection::open_with_flags(
|
||||||
|
db_path,
|
||||||
|
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
||||||
|
)
|
||||||
|
.map_err(db)?;
|
||||||
|
conn.execute("VACUUM INTO ?1", [out.display().to_string()])
|
||||||
|
.map_err(db)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn db(e: rusqlite::Error) -> Error {
|
pub(crate) fn db(e: rusqlite::Error) -> Error {
|
||||||
Error::Db(e.to_string())
|
Error::Db(e.to_string())
|
||||||
}
|
}
|
||||||
@ -199,6 +223,42 @@ mod tests {
|
|||||||
assert!(!alt.aborted);
|
assert!(!alt.aborted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sicherung_ist_vollstaendig_und_migriert_die_quelle_nicht() {
|
||||||
|
let ordner = Testordner::neu();
|
||||||
|
let pfad = ordner.0.join("nexus.db");
|
||||||
|
{
|
||||||
|
let conn = rusqlite::Connection::open(&pfad).unwrap();
|
||||||
|
conn.execute_batch(MIGRATIONS[0]).unwrap();
|
||||||
|
conn.pragma_update(None, "user_version", 1).unwrap();
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO anfrage_log (zeit, korrelation, status) VALUES ('2026-11-09T10:00:00Z', 'req_alt', 200)",
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
let ziel = ordner.0.join("sicherung/vorher.db");
|
||||||
|
backup_file(&pfad, &ziel).unwrap();
|
||||||
|
// Quelle unverändert auf Version 1.
|
||||||
|
let quelle = rusqlite::Connection::open(&pfad).unwrap();
|
||||||
|
let v: i64 = quelle
|
||||||
|
.pragma_query_value(None, "user_version", |r| r.get(0))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(v, 1);
|
||||||
|
// Die Kopie hat Version 1 und die Daten.
|
||||||
|
let kopie = rusqlite::Connection::open(&ziel).unwrap();
|
||||||
|
let v: i64 = kopie
|
||||||
|
.pragma_query_value(None, "user_version", |r| r.get(0))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(v, 1);
|
||||||
|
let n: i64 = kopie
|
||||||
|
.query_row("SELECT COUNT(*) FROM anfrage_log", [], |r| r.get(0))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(n, 1);
|
||||||
|
// Zweimal dasselbe Ziel wird abgelehnt.
|
||||||
|
assert!(backup_file(&pfad, &ziel).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn neuere_datenbank_wird_abgelehnt() {
|
fn neuere_datenbank_wird_abgelehnt() {
|
||||||
let ordner = Testordner::neu();
|
let ordner = Testordner::neu();
|
||||||
|
|||||||
@ -27,6 +27,7 @@ Aufruf:
|
|||||||
nexus model add --name N --engine maya --maya-config PFAD.json [--gpus 0,1] [--disabled]
|
nexus model add --name N --engine maya --maya-config PFAD.json [--gpus 0,1] [--disabled]
|
||||||
nexus model list | enable --name N | disable --name N | discover [--engine llama.cpp|ollama|maya]
|
nexus model list | enable --name N | disable --name N | discover [--engine llama.cpp|ollama|maya]
|
||||||
nexus diag [--out DATEI.tar.gz] [--ohne-engine-logs] Diagnose-Archiv ohne Prompts und Keys
|
nexus diag [--out DATEI.tar.gz] [--ohne-engine-logs] Diagnose-Archiv ohne Prompts und Keys
|
||||||
|
nexus backup [--out DATEI.db] Kopie der Datenbank (ohne sie zu migrieren; auch bei laufendem Gateway)
|
||||||
nexus help Diese Hilfe
|
nexus help Diese Hilfe
|
||||||
|
|
||||||
Die Konfiguration kommt aus --config, sonst NEXUS_CONFIG, sonst ./config.toml, sonst gelten Standardwerte.
|
Die Konfiguration kommt aus --config, sonst NEXUS_CONFIG, sonst ./config.toml, sonst gelten Standardwerte.
|
||||||
@ -45,6 +46,7 @@ async fn main() -> ExitCode {
|
|||||||
Some("key") => admin::key(&args[1..]),
|
Some("key") => admin::key(&args[1..]),
|
||||||
Some("model") => admin::model(&args[1..]).await,
|
Some("model") => admin::model(&args[1..]).await,
|
||||||
Some("diag") => diag::command(&args[1..]),
|
Some("diag") => diag::command(&args[1..]),
|
||||||
|
Some("backup") => backup(&args[1..]),
|
||||||
Some("help") | Some("--help") | None => {
|
Some("help") | Some("--help") | None => {
|
||||||
print!("{HILFE}");
|
print!("{HILFE}");
|
||||||
ExitCode::SUCCESS
|
ExitCode::SUCCESS
|
||||||
@ -96,3 +98,33 @@ fn config_path(args: &[String]) -> Result<Option<PathBuf>, String> {
|
|||||||
_ => Err("Aufruf: nexus selftest [--config PFAD]".to_string()),
|
_ => Err("Aufruf: nexus selftest [--config PFAD]".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `nexus backup`: Kopie der Datenbank, Standard `data_dir/backups/nexus-<zeit>.db`.
|
||||||
|
fn backup(args: &[String]) -> ExitCode {
|
||||||
|
let run = || -> Result<PathBuf, String> {
|
||||||
|
let flags = cli::Flags::parse(args, &["out", "config"], &[])?;
|
||||||
|
let config = cli::load_config(&flags)?;
|
||||||
|
let db = config.db_path();
|
||||||
|
if !db.is_file() {
|
||||||
|
return Err(format!("Es gibt keine Datenbank unter {}.", db.display()));
|
||||||
|
}
|
||||||
|
let out = flags.get("out").map(PathBuf::from).unwrap_or_else(|| {
|
||||||
|
config.data_dir.join("backups").join(format!(
|
||||||
|
"nexus-{}.db",
|
||||||
|
nexus_core::now_iso8601().replace([':', '-'], "")
|
||||||
|
))
|
||||||
|
});
|
||||||
|
nexus_store::backup_file(&db, &out).map_err(|e| e.to_string())?;
|
||||||
|
Ok(out)
|
||||||
|
};
|
||||||
|
match run() {
|
||||||
|
Ok(out) => {
|
||||||
|
println!("Sicherung geschrieben: {}", out.display());
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
||||||
|
Err(message) => {
|
||||||
|
eprintln!("{message}");
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -3,10 +3,12 @@
|
|||||||
#
|
#
|
||||||
# Was es tut (und nichts anderes):
|
# Was es tut (und nichts anderes):
|
||||||
# 1. legt /srv/apps/nexus/{releases/<version>,data} an (Besitzer svc-scotty)
|
# 1. legt /srv/apps/nexus/{releases/<version>,data} an (Besitzer svc-scotty)
|
||||||
# 2. kopiert das Binary nach releases/<version>/nexus und schaltet den
|
# 2. sichert die vorhandene Datenbank nach data/backups/ (ab 0.1.4; die neue
|
||||||
|
# Version kann das Schema anheben, ältere öffnen es danach nicht mehr)
|
||||||
|
# 3. kopiert das Binary nach releases/<version>/nexus und schaltet den
|
||||||
# Symlink /srv/apps/nexus/current darauf um (die alte Version bleibt)
|
# Symlink /srv/apps/nexus/current darauf um (die alte Version bleibt)
|
||||||
# 3. legt /srv/apps/nexus/config.toml an, NUR wenn es sie noch nicht gibt
|
# 4. legt /srv/apps/nexus/config.toml an, NUR wenn es sie noch nicht gibt
|
||||||
# 4. schreibt /etc/systemd/system/nexus.service und lädt systemd neu
|
# 5. schreibt /etc/systemd/system/nexus.service und lädt systemd neu
|
||||||
# Es startet und aktiviert den Dienst NICHT, außer mit --start bzw. --enable.
|
# Es startet und aktiviert den Dienst NICHT, außer mit --start bzw. --enable.
|
||||||
# Es ändert keine bestehende Engine-Installation und keine Modelle.
|
# Es ändert keine bestehende Engine-Installation und keine Modelle.
|
||||||
#
|
#
|
||||||
@ -33,6 +35,13 @@ if [ "${1:-}" = "--rollback" ]; then
|
|||||||
ln -sfn "$BASE/releases/$previous" "$BASE/current.new"
|
ln -sfn "$BASE/releases/$previous" "$BASE/current.new"
|
||||||
mv -T "$BASE/current.new" "$BASE/current"
|
mv -T "$BASE/current.new" "$BASE/current"
|
||||||
echo "Rückfall: current zeigt jetzt auf $previous (vorher $current)."
|
echo "Rückfall: current zeigt jetzt auf $previous (vorher $current)."
|
||||||
|
backup="$(ls -1t "$BASE/data/backups"/nexus-vor-"$current"-*.db 2>/dev/null | head -n 1 || true)"
|
||||||
|
if [ -n "$backup" ]; then
|
||||||
|
echo "Hat $current das Datenbank-Schema angehoben, öffnet $previous die Datenbank nicht mehr."
|
||||||
|
echo "Die Sicherung von vor $current liegt unter: $backup"
|
||||||
|
echo "Zurückspielen (Daten seit dem Update gehen verloren):"
|
||||||
|
echo " sudo systemctl stop nexus && sudo -u $USER_NAME cp '$backup' '$BASE/data/nexus.db' && sudo systemctl start nexus"
|
||||||
|
fi
|
||||||
if systemctl is-active --quiet nexus; then systemctl restart nexus && echo "Dienst neu gestartet."; fi
|
if systemctl is-active --quiet nexus; then systemctl restart nexus && echo "Dienst neu gestartet."; fi
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
@ -44,6 +53,11 @@ VERSION="${VERSION:?VERSION fehlt (zum Beispiel VERSION=0.1.3)}"
|
|||||||
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$BASE" "$BASE/releases" "$BASE/releases/$VERSION"
|
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$BASE" "$BASE/releases" "$BASE/releases/$VERSION"
|
||||||
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0750 "$BASE/data"
|
install -d -o "$USER_NAME" -g "$USER_NAME" -m 0750 "$BASE/data"
|
||||||
install -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$HERE/nexus" "$BASE/releases/$VERSION/nexus"
|
install -o "$USER_NAME" -g "$USER_NAME" -m 0755 "$HERE/nexus" "$BASE/releases/$VERSION/nexus"
|
||||||
|
if [ -f "$BASE/data/nexus.db" ] && [ -f "$BASE/config.toml" ]; then
|
||||||
|
# Mit dem neuen Binary, das die Datenbank dabei nur liest (keine Migration).
|
||||||
|
backup="$BASE/data/backups/nexus-vor-$VERSION-$(date -u +%Y%m%dT%H%M%SZ).db"
|
||||||
|
as_user "$BASE/releases/$VERSION/nexus" backup --out "$backup" --config "$BASE/config.toml"
|
||||||
|
fi
|
||||||
ln -sfn "$BASE/releases/$VERSION" "$BASE/current.new"
|
ln -sfn "$BASE/releases/$VERSION" "$BASE/current.new"
|
||||||
mv -T "$BASE/current.new" "$BASE/current"
|
mv -T "$BASE/current.new" "$BASE/current"
|
||||||
echo "Version $VERSION installiert, current zeigt darauf."
|
echo "Version $VERSION installiert, current zeigt darauf."
|
||||||
|
|||||||
Reference in New Issue
Block a user