Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bottomless-cli: added snapshot command #1229

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions bottomless-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ enum Commands {
#[clap(long, short)]
verbose: bool,
},
#[clap(about = "Generate and upload a snapshot for a given generation or timestamp")]
Snapshot {
#[clap(long, short)]
generation: Option<uuid::Uuid>,
},
}

async fn run() -> Result<()> {
Expand Down Expand Up @@ -273,6 +278,44 @@ async fn run() -> Result<()> {
"rm command cannot be run without parameters; see -h or --help for details"
),
},
Commands::Snapshot { generation } => {
tokio::fs::create_dir_all(&database_dir).await?;
let generation = if let Some(gen) = generation {
gen
} else if let Some(gen) = client.latest_generation_before(None).await {
gen
} else {
println!("no generation to snapshot found; nothing to do");
return Ok(());
};
// snapshots mark the state of the DB at the beginning of the generation, therefore
// snapshot at generation N is a final state of database at generation N-1. This can
// be later used for fast restore: restore from generation N = snapshot + all WAL frames
// from that generation.
let parent = if let Some(parent) = client.get_dependency(&generation).await? {
parent
} else {
println!("cannot create a snapshot at the beginning of the generation {}: parent generation not found", generation);
return Ok(());
};
client.restore(Some(parent.clone()), None).await?;
println!(
"restored database at the start of generation {}: preparing snapshot...",
generation
);
let db_path = PathBuf::from(&database);
if let Err(e) = verify_db(&db_path) {
println!("Verification failed: {e}");
std::process::exit(1)
} else {
println!("verification succeeded");
client.set_generation(generation.clone());
client.snapshot_main_db_file(true).await?;
client.wait_until_snapshotted().await?;
println!("snapshot uploaded for generation: {}", generation);
tokio::fs::remove_dir_all(&database_dir).await?;
}
}
};
Ok(())
}
Expand Down
Loading