use crate::manifest::{BackupManifest, FileEntry};
use powdb_storage::catalog::Catalog;
use std::io;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn full_backup(catalog: &mut Catalog, dest: &Path) -> io::Result<BackupManifest> {
catalog.checkpoint()?;
let source_lsn = catalog.max_lsn();
let src = catalog.data_dir().to_path_buf();
std::fs::create_dir_all(dest)?;
let mut files = Vec::new();
for entry in std::fs::read_dir(&src)? {
let entry = entry?;
let name = entry.file_name().to_string_lossy().to_string();
let is_durable = name == "catalog.bin" || name.ends_with(".heap") || name.ends_with(".idx");
if !is_durable {
continue;
}
let bytes = std::fs::read(entry.path())?;
let hash = blake3::hash(&bytes).to_hex().to_string();
std::fs::write(dest.join(&name), &bytes)?;
files.push(FileEntry {
name,
len: bytes.len() as u64,
blake3_hex: hash,
});
}
files.sort_by(|a, b| a.name.cmp(&b.name));
let manifest = BackupManifest {
format_version: BackupManifest::FORMAT_VERSION,
created_unix_secs: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
source_lsn,
files,
};
manifest.write(dest)?;
Ok(manifest)
}