use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Sender, channel};
use std::time::{Duration, Instant};
use anyhow::{Context, Result, anyhow, bail, ensure};
use serde::Serialize;
use second_brain_core::cold;
use second_brain_core::dir_copy::{
copy_dir_stable, copy_file_synced, copy_symlink, is_not_found, sync_dir,
};
use second_brain_core::index::raw;
use second_brain_core::keys::{
self, DeviceKeys, WRAPPED_STORE_KEY_FILE, WrapRefresh, every_held_epoch, left_behind,
unwrap_store_key_with_epochs,
};
use second_brain_core::private_files::create_private_dir;
use second_brain_core::sqlite_store::encrypt_migration::key_opens_file;
use second_brain_core::sqlite_store::{AtRestPurpose, SqliteStore};
use second_brain_core::transcript_archive::TranscriptArchive;
use crate::maintenance::{DrainRequest, PauseCommand};
use crate::write_gate::WriteGate;
const DB_FILE: &str = "brain.db";
const TRANSCRIPTS_DIR: &str = "transcripts";
const MODEL_CACHE_DIR: &str = "fastembed";
const STALE_MARKER: &str = ".stale-";
const COLD_RETRY: Duration = Duration::from_secs(5);
const COLD_WAIT: Duration = Duration::from_secs(30 * 60);
pub struct Backup<'a> {
pub store: &'a SqliteStore,
pub data_dir: &'a Path,
pub gate: &'a WriteGate,
pub drain: Option<&'a Sender<DrainRequest>>,
pub archive: Option<&'a TranscriptArchive>,
}
#[derive(Debug, Serialize)]
pub struct BackupSummary {
pub path: PathBuf,
pub memory_count: u64,
pub stored_total: u64,
pub bytes: u64,
pub outbox_settled: bool,
pub indexes: &'static str,
pub raw_index: &'static str,
}
#[derive(Debug)]
pub enum BackupError {
BadDestination(String),
Refused(String),
Failed(anyhow::Error),
}
impl std::fmt::Display for BackupError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BadDestination(m) | Self::Refused(m) => f.write_str(m),
Self::Failed(e) => write!(f, "{e:#}"),
}
}
}
impl From<anyhow::Error> for BackupError {
fn from(e: anyhow::Error) -> Self {
Self::Failed(e)
}
}
struct Live {
db: PathBuf,
indexes: PathBuf,
raw_index: PathBuf,
cold: PathBuf,
}
impl Live {
fn of(db: PathBuf) -> Self {
Self {
indexes: db.with_extension("indexes"),
raw_index: raw::dir_for_db(&db),
cold: cold::dir_for_db(&db),
db,
}
}
fn handled(&self, name: &str) -> bool {
let db = self
.db
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(DB_FILE);
[&self.indexes, &self.raw_index, &self.cold]
.iter()
.any(|dir| dir.file_name().and_then(|n| n.to_str()) == Some(name))
|| name == db
|| name == format!("{db}-wal")
|| name == TRANSCRIPTS_DIR
|| name == WRAPPED_STORE_KEY_FILE
}
}
impl Backup<'_> {
pub fn write(&self, dest: &Path) -> Result<BackupSummary, BackupError> {
let _maintenance = self.gate.hold_maintenance();
let live = Live::of(self.db_path()?);
let (parent, name) = self.check_destination(dest)?;
let dest = parent.join(&name);
remove_stale_partials(&parent, &name);
let partial = parent.join(format!(".{name}.partial-{}", uuid::Uuid::new_v4().simple()));
create_private_dir(&partial)?;
let published = self.fill(&partial, &live).and_then(|summary| {
sync_dir(&partial)?;
fs::rename(&partial, &dest)
.with_context(|| format!("renaming {} to {}", partial.display(), dest.display()))?;
Ok(summary)
});
match published {
Ok(summary) => {
if let Err(e) = sync_dir(&parent) {
tracing::warn!("the backup is in place but not yet flushed to disk: {e:#}");
}
Ok(BackupSummary {
path: dest,
..summary
})
}
Err(e) => {
if let Err(cleanup) = fs::remove_dir_all(&partial) {
tracing::warn!(
"cannot remove the partial backup {}: {cleanup}",
partial.display()
);
}
Err(e)
}
}
}
fn db_path(&self) -> Result<PathBuf, BackupError> {
let db = self.store.file_path()?.ok_or_else(|| {
BackupError::Refused("an in-memory store has no file to back up".into())
})?;
let dir = db.parent().map(Path::to_path_buf).unwrap_or_default();
if fs::canonicalize(&dir).ok() != fs::canonicalize(self.data_dir).ok() {
return Err(BackupError::Refused(format!(
"the store {} is outside the data dir {}, so restore would not find it",
db.display(),
self.data_dir.display()
)));
}
Ok(db)
}
fn check_destination(&self, dest: &Path) -> Result<(PathBuf, String), BackupError> {
let bad = |m: String| Err(BackupError::BadDestination(m));
if !dest.is_absolute() {
return bad(format!("{} is not an absolute path", dest.display()));
}
let (Some(parent), Some(name)) = (dest.parent(), dest.file_name()) else {
return bad(format!("{} names no directory", dest.display()));
};
let parent = fs::canonicalize(parent)
.map_err(|e| BackupError::BadDestination(format!("{}: {e}", parent.display())))?;
let data_dir = fs::canonicalize(self.data_dir).context("reading the data dir")?;
if parent.starts_with(&data_dir) {
return bad(format!(
"{} is inside the data dir {}",
dest.display(),
data_dir.display()
));
}
if fs::symlink_metadata(parent.join(name)).is_ok() {
return Err(BackupError::Refused(format!(
"{} already exists; a backup never writes over one",
dest.display()
)));
}
Ok((parent, name.to_string_lossy().into_owned()))
}
fn fill(&self, partial: &Path, live: &Live) -> Result<BackupSummary, BackupError> {
let device = self.fix_keys(partial)?;
let copy_db = partial.join(DB_FILE);
let indexes = self.snapshot(©_db, live)?;
let checked = self.store.check_backup_copy(©_db)?;
if let Some(device) = &device {
self.check_wrapped_key(device, partial, ©_db)?;
}
let raw_index = if live.raw_index.is_dir()
&& copy_dir_stable(&live.raw_index, &raw::dir_for_db(©_db))?
{
"copied"
} else {
"rebuilt at first start"
};
self.copy_transcripts(&partial.join(TRANSCRIPTS_DIR))?;
self.copy_rest(partial, live)?;
Ok(BackupSummary {
path: partial.to_path_buf(),
memory_count: checked.counts.live,
stored_total: checked.counts.stored_total,
bytes: tree_bytes(partial)?,
outbox_settled: checked.outbox_settled,
indexes,
raw_index,
})
}
fn fix_keys(&self, partial: &Path) -> Result<Option<DeviceKeys>, BackupError> {
if !self.store.is_encrypted() {
return Ok(None);
}
let device = keys::load_device_keys(self.data_dir)?.ok_or_else(|| {
BackupError::Refused("this device has no device key to wrap the store key with".into())
})?;
if let WrapRefresh::NoIdentity =
keys::refresh_store_key_wrap(self.store, self.data_dir, &device)?
{
return Err(BackupError::Refused(
"this device has no sync identity, so no key could open its backup; \
pair it with `sb pair` first"
.into(),
));
}
for purpose in [
AtRestPurpose::Indexes,
AtRestPurpose::RawIndex,
AtRestPurpose::TranscriptArchive,
] {
self.store.at_rest_keys(purpose)?;
}
copy_file_synced(
&self.data_dir.join(WRAPPED_STORE_KEY_FILE),
&partial.join(WRAPPED_STORE_KEY_FILE),
)?;
Ok(Some(device))
}
fn snapshot(&self, copy_db: &Path, live: &Live) -> Result<&'static str> {
let deadline = Instant::now() + COLD_WAIT;
loop {
let paused = self.drain.map(Paused::start).transpose()?;
let guard = match self.store.cold_tier() {
None => None,
Some(tier) => match tier.try_hold_passes() {
Some(guard) => Some(guard),
None => {
drop(paused);
ensure!(
Instant::now() < deadline,
"a cold pass held the cold tier for 30 minutes; run the backup again"
);
std::thread::sleep(COLD_RETRY);
continue;
}
},
};
if let Some(paused) = &paused {
paused.settle()?;
}
self.store.vacuum_into(copy_db)?;
let indexes = match &paused {
None => "unavailable",
Some(_) if !live.indexes.is_dir() => "unavailable",
Some(_) => {
if copy_dir_stable(&live.indexes, ©_db.with_extension("indexes"))? {
"copied"
} else {
"rebuilt at first start"
}
}
};
drop(paused);
if live.cold.is_dir() {
ensure!(
copy_dir_stable(&live.cold, &cold::dir_for_db(copy_db))?,
"the cold segments at {} changed while the cold tier was held",
live.cold.display()
);
}
drop(guard);
return Ok(indexes);
}
}
fn check_wrapped_key(&self, device: &DeviceKeys, partial: &Path, copy_db: &Path) -> Result<()> {
let held = every_held_epoch(self.store, device)?;
let key = unwrap_store_key_with_epochs(partial, &held)?
.ok_or_else(|| anyhow!("the backup holds no {WRAPPED_STORE_KEY_FILE}"))?;
ensure!(
key_opens_file(copy_db, &key),
"the copied {WRAPPED_STORE_KEY_FILE} does not open the copied store"
);
Ok(())
}
fn copy_transcripts(&self, dest: &Path) -> Result<()> {
match self.archive {
Some(archive) => archive.copy_into(dest),
None => {
let live = self.data_dir.join(TRANSCRIPTS_DIR);
if live.is_dir() && !copy_dir_stable(&live, dest)? {
bail!("the transcript archive at {} kept changing", live.display());
}
Ok(())
}
}
}
fn copy_rest(&self, partial: &Path, live: &Live) -> Result<()> {
for entry in fs::read_dir(self.data_dir)
.with_context(|| format!("reading {}", self.data_dir.display()))?
{
let entry = entry?;
let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
continue;
};
if left_behind(&name)
|| live.handled(&name)
|| name == MODEL_CACHE_DIR
|| name.contains(STALE_MARKER)
{
continue;
}
let (source, target) = (entry.path(), partial.join(&name));
let kind = match entry.file_type() {
Ok(kind) => kind,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => return Err(e).with_context(|| format!("reading {}", source.display())),
};
if kind.is_dir() {
ensure!(
copy_dir_stable(&source, &target)? || !source.exists(),
"{} kept changing",
source.display()
);
} else {
let copied = if kind.is_symlink() {
copy_symlink(&source, &target)
} else {
copy_file_synced(&source, &target)
};
match copied {
Err(e) if is_not_found(&e) => {}
other => other?,
}
}
}
Ok(())
}
}
struct Paused(Sender<PauseCommand>);
impl Paused {
fn start(drain: &Sender<DrainRequest>) -> Result<Self> {
let (commands, held) = channel();
drain
.send(DrainRequest::Pause(held))
.map_err(|_| anyhow!("the index drain thread is not running"))?;
let paused = Self(commands);
paused.settle()?;
Ok(paused)
}
fn settle(&self) -> Result<()> {
let (reply, done) = channel();
self.0
.send(PauseCommand::Settle(reply))
.map_err(|_| anyhow!("the index drain thread stopped"))?;
done.recv()
.map_err(|_| anyhow!("the index drain thread stopped"))?
}
}
fn remove_stale_partials(parent: &Path, name: &str) {
let prefix = format!(".{name}.partial-");
let entries = match fs::read_dir(parent) {
Ok(entries) => entries,
Err(e) => {
tracing::warn!(
"cannot look for partial backups in {}: {e}",
parent.display()
);
return;
}
};
for entry in entries {
let entry = match entry {
Ok(entry) => entry,
Err(e) => {
tracing::warn!(
"cannot look for partial backups in {}: {e}",
parent.display()
);
continue;
}
};
if entry.file_name().to_string_lossy().starts_with(&prefix)
&& let Err(e) = fs::remove_dir_all(entry.path())
{
tracing::warn!(
"cannot remove the partial backup {}: {e}",
entry.path().display()
);
}
}
}
fn tree_bytes(dir: &Path) -> Result<u64> {
let mut total = 0;
for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
let entry = entry?;
let kind = entry.file_type()?;
if kind.is_dir() {
total += tree_bytes(&entry.path())?;
} else if kind.is_file() {
total += entry.metadata()?.len();
}
}
Ok(total)
}