Enhance backup process with gzip compression
This commit is contained in:
parent
eae4cec872
commit
07cdadbdac
3 changed files with 62 additions and 9 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use async_compression::tokio::bufread::GzipEncoder;
|
||||
use chrono::{NaiveTime, TimeDelta};
|
||||
use tokio::{fs::File, io::BufReader};
|
||||
use tracing::{Level, event};
|
||||
|
||||
use super::db;
|
||||
|
|
@ -44,11 +46,60 @@ where
|
|||
path.display()
|
||||
);
|
||||
|
||||
if let Err(error) = db_connection.backup(path).await {
|
||||
if let Err(error) = db_connection.backup(&path).await {
|
||||
event!(Level::ERROR, "Error when backing up database: {}", error);
|
||||
}
|
||||
|
||||
event!(Level::INFO, "Backup done");
|
||||
// Compress the backup file.
|
||||
match File::open(&path).await {
|
||||
Ok(file_input) => {
|
||||
let buf_reader = BufReader::new(file_input);
|
||||
let mut encoder = GzipEncoder::new(buf_reader);
|
||||
let path_compressed = path.with_extension("sqlite.gz");
|
||||
match File::create(&path_compressed).await {
|
||||
Ok(mut file_output) => {
|
||||
if let Err(error) =
|
||||
tokio::io::copy(&mut encoder, &mut file_output).await
|
||||
{
|
||||
event!(
|
||||
Level::ERROR,
|
||||
"Error when compressing backup file: {}",
|
||||
error
|
||||
);
|
||||
} else {
|
||||
match std::fs::remove_file(&path) {
|
||||
Ok(()) => event!(
|
||||
Level::INFO,
|
||||
"Backup done: {}",
|
||||
path_compressed.display()
|
||||
),
|
||||
Err(error) => {
|
||||
event!(
|
||||
Level::ERROR,
|
||||
"Error when removing uncompressed backup file: {}",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
event!(
|
||||
Level::ERROR,
|
||||
"Error when creating compressed backup file: {}",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
event!(
|
||||
Level::ERROR,
|
||||
"Error when opening backup file for compression: {}",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue