Enhance backup process with gzip compression

This commit is contained in:
Greg Burri 2025-04-06 00:21:44 +02:00
parent eae4cec872
commit 07cdadbdac
3 changed files with 62 additions and 9 deletions

View file

@ -6,13 +6,13 @@
### Launch Axum ### Launch Axum
In directory /backend type: In directory */backend* type:
```$> cargo run``` ```$> cargo run```
Then browse http://127.0.0.1:8082 (You need to compile the wasm file first, see section *Frontend*) Then browse http://127.0.0.1:8082 (You need to compile the wasm file first, see section *Frontend*)
At first launch the configuration file '/backend/conf.ron' is created. It contains the port the server will listen to and information about the SMTP server which will be used to send email when a user sign up or change its password. At first launch the configuration file `/backend/conf.ron` is created. It contains the port the server will listen to and information about the SMTP server which will be used to send email when a user sign up or change its password.
### Autoreload ### Autoreload
@ -20,7 +20,7 @@ First install cargo watch:
```$> cargo install cargo-watch``` ```$> cargo install cargo-watch```
In directory /backend type: In directory */backend* type:
```$> cargo watch -x run``` ```$> cargo watch -x run```
@ -33,11 +33,11 @@ trunk: https://trunkrs.dev
### Compilation ### Compilation
In directory /frontend type: In directory */frontend* type:
```$> trunk build``` ```$> trunk build```
It will create the wasm file in '/backend/static'. It will create the wasm file in */backend/static*.
You can now refresh your browser to reload the wasm file. You can now refresh your browser to reload the wasm file.
# How-to # How-to
@ -46,7 +46,7 @@ You can now refresh your browser to reload the wasm file.
As root: As root:
1. Copy '/doc/recipes.service' to '/lib/systemd/system/' 1. Copy */doc/recipes.service* to */lib/systemd/system/*
2. Change the values of `User`, `WorkingDirectory` and `ExecStart`. 2. Change the values of `User`, `WorkingDirectory` and `ExecStart`.
2. Enabled it: `#> systemctl enable recipes` 2. Enabled it: `#> systemctl enable recipes`
3. Launch it: `#> systemctl start recipes` 3. Launch it: `#> systemctl start recipes`

View file

@ -27,8 +27,10 @@ clap = { version = "4", features = ["derive"] }
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio", "chrono"] } sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio", "chrono"] }
async-compression = { version = "0.4", features = ["tokio", "gzip"] }
askama = "0.13" askama = "0.13"
comrak = "0.37" comrak = "0.38"
argon2 = { version = "0.5", features = ["default", "std"] } argon2 = { version = "0.5", features = ["default", "std"] }
rand_core = { version = "0.9", features = ["std"] } rand_core = { version = "0.9", features = ["std"] }

View file

@ -1,4 +1,6 @@
use async_compression::tokio::bufread::GzipEncoder;
use chrono::{NaiveTime, TimeDelta}; use chrono::{NaiveTime, TimeDelta};
use tokio::{fs::File, io::BufReader};
use tracing::{Level, event}; use tracing::{Level, event};
use super::db; use super::db;
@ -44,11 +46,60 @@ where
path.display() 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::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
);
}
}
} }
}) })
} }