mirror of
https://github.com/SinTan1729/chhoto-url.git
synced 2025-12-05 21:10:23 -08:00
new: Create database backups before opening
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user