|
|
|
@ -385,7 +385,7 @@ pub mod models; |
|
|
|
/// Creates a back-up of the sqlite database
|
|
|
|
/// MySQL/MariaDB and PostgreSQL are not supported.
|
|
|
|
#[cfg(sqlite)] |
|
|
|
pub fn backup_sqlite() -> Result<String, Error> { |
|
|
|
pub fn backup_sqlite(output_dir: Option<String>) -> Result<String, Error> { |
|
|
|
use diesel::Connection; |
|
|
|
use std::{fs::File, io::Write}; |
|
|
|
|
|
|
|
@ -395,8 +395,18 @@ pub fn backup_sqlite() -> Result<String, Error> { |
|
|
|
// This way we can set a readonly flag on the opening mode without issues.
|
|
|
|
let mut conn = diesel::sqlite::SqliteConnection::establish(&format!("sqlite://{db_url}?mode=ro"))?; |
|
|
|
|
|
|
|
let db_path = std::path::Path::new(&db_url).parent().unwrap(); |
|
|
|
let backup_file = db_path |
|
|
|
let backup_dir = match &output_dir { |
|
|
|
Some(dir) => { |
|
|
|
let output_path = std::path::Path::new(dir); |
|
|
|
if !output_path.exists() || !output_path.is_dir() { |
|
|
|
err!(format!("Backup directory does not exist or is not a directory: {}", dir)); |
|
|
|
} |
|
|
|
output_path |
|
|
|
} |
|
|
|
None => std::path::Path::new(&db_url).parent().unwrap(), |
|
|
|
}; |
|
|
|
|
|
|
|
let backup_file = backup_dir |
|
|
|
.join(format!("db_{}.sqlite3", chrono::Utc::now().format("%Y%m%d_%H%M%S"))) |
|
|
|
.to_string_lossy() |
|
|
|
.into_owned(); |
|
|
|
@ -417,7 +427,7 @@ pub fn backup_sqlite() -> Result<String, Error> { |
|
|
|
} |
|
|
|
|
|
|
|
#[cfg(not(sqlite))] |
|
|
|
pub fn backup_sqlite() -> Result<String, Error> { |
|
|
|
pub fn backup_sqlite(_output_dir: Option<String>) -> Result<String, Error> { |
|
|
|
err_silent!("The database type is not SQLite. Backups only works for SQLite databases") |
|
|
|
} |
|
|
|
|
|
|
|
|