db-snapshot: Add error handling to metadata snapshot creation
All checks were successful
ci/woodpecker/push/debug Pipeline was successful
ci/woodpecker/pr/debug Pipeline was successful

Fix #920
This commit is contained in:
Baptiste Jonglez 2025-01-22 13:31:05 +01:00
parent 294cb99409
commit e5e054187e

View file

@ -43,13 +43,32 @@ pub fn snapshot_metadata(garage: &Garage) -> Result<(), Error> {
let mut snapshots_dir = garage.config.metadata_dir.clone(); let mut snapshots_dir = garage.config.metadata_dir.clone();
snapshots_dir.push("snapshots"); snapshots_dir.push("snapshots");
fs::create_dir_all(&snapshots_dir)?; if let Err(e) = fs::create_dir_all(&snapshots_dir) {
drop(lock);
error!(
"Failed to create snapshots directory '{}': {}",
snapshots_dir.display(),
e
);
return Err(Error::Message(format!(
"Failed to create snapshots directory '{}': {}",
snapshots_dir.display(),
e
)));
}
let mut new_path = snapshots_dir.clone(); let mut new_path = snapshots_dir.clone();
new_path.push(chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)); new_path.push(chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true));
info!("Snapshotting metadata db to {}", new_path.display()); info!("Snapshotting metadata db to {}", new_path.display());
garage.db.snapshot(&new_path)?; if let Err(e) = garage.db.snapshot(&new_path) {
drop(lock);
error!("Failed to snapshot metadata db: {}", e);
return Err(Error::Message(format!(
"Failed to snapshot metadata db: {}",
e
)));
}
info!("Metadata db snapshot finished"); info!("Metadata db snapshot finished");
if let Err(e) = cleanup_snapshots(&snapshots_dir) { if let Err(e) = cleanup_snapshots(&snapshots_dir) {