in below/src/main.rs [1317:1381]
fn snapshot(
logger: slog::Logger,
below_config: &BelowConfig,
begin: String,
end: String,
host: Option<String>,
port: Option<u16>,
) -> Result<()> {
let (time_begin, time_end) = cliutil::system_time_range_from_date_and_adjuster(
begin.as_str(),
Some(end.as_str()),
/* days_adjuster */ None,
)?;
let (timestamp_begin, timestamp_end) = (
common::util::get_unix_timestamp(time_begin),
common::util::get_unix_timestamp(time_end),
);
// Create a directory for the output files
// Format: snapshot_<timestamp_begin>_<timestamp_end>
let temp_folder = TempDir::new(&format!(
"snapshot_{:011}_{:011}",
timestamp_begin, timestamp_end
))
.context("Failed to create temporary folder for snapshot")?;
let snapshot_store_path = temp_folder.into_path();
// Build compression options to ensure snapshot is compressed before tarball
let compress_opts = CompressOpts {
compress: true,
dict_compress_chunk_size: Some(16),
};
convert_store(
logger,
below_config,
begin,
end,
None,
snapshot_store_path.clone(),
host,
port,
&compress_opts,
)
.context("Failed to convert store for snapshot")?;
// The temp dir path will be something like "/tmp/snapshot_<timestamp_begin>_<timestamp_end>.XXXX".
// We will use the dir name as name of the tarball.
let tarball_name = snapshot_store_path
.as_path()
.file_name()
.unwrap()
.to_str()
.unwrap();
let file = fs::File::create(&tarball_name)
.with_context(|| format!("Failed to create snapshot file {:?}", &tarball_name))?;
// Create a new tarball with the snapshot dir name
let mut tar = TarBuilder::new(file);
tar.append_dir_all(tarball_name, snapshot_store_path.as_path())
.context("Failed to add snapshot store to tar builder")?;
tar.finish()
.context("Failed to build compressed snapshot file.")?;
println!("Snapshot has been created at {}", tarball_name);
Ok(())
}