new: Create database backups before opening

This commit is contained in:
SinTan1729
2025-10-09 14:22:08 -05:00
parent 64082b3bb5
commit 043c6efab6

View File

@@ -11,6 +11,7 @@ use actix_web::{
};
use log::info;
use rusqlite::Connection;
use std::fs;
pub(crate) use std::io::Result;
use tokio::{spawn, time};
@@ -79,6 +80,22 @@ async fn main() -> Result<()> {
// Do periodic cleanup
let db_location_clone = conf.db_location.clone();
// Create backups if WAL mode is being used
if conf.use_wal_mode {
info!("Creating database backups.");
if fs::exists(format!("{db_location_clone}.bak1")).ok() == Some(true) {
fs::rename(
format!("{db_location_clone}.bak1"),
format!("{db_location_clone}.bak2"),
)
.expect("Error creating backups.");
}
if fs::exists(&db_location_clone).ok() == Some(true) {
fs::copy(&db_location_clone, format!("{db_location_clone}.bak1"))
.expect("Error creating backups.");
}
}
info!("Starting cleanup service, will run once every hour.");
spawn(async move {
let db = database::open_db(db_location_clone, conf.use_wal_mode);