Skip to content

Commit

Permalink
Added sqlite backup
Browse files Browse the repository at this point in the history
  • Loading branch information
mfep committed Jun 10, 2023
1 parent c4e3928 commit 35d2063
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ directories-next = "2.0"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"
dialoguer = "0.9"
rusqlite = { version = "0.28.0", features = ["bundled"] }
rusqlite = { version = "0.28.0", features = ["bundled", "backup"] }
16 changes: 14 additions & 2 deletions src/datafile/sqlite_datafile.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Handling SQLite habit databases.
use anyhow::{bail, Context, Result};
use chrono::{NaiveDate, NaiveDateTime};
use std::path::Path;
use std::{ffi::OsString, path::Path};

use super::DiaryDataConnection;
use rusqlite::{params, Connection};
use rusqlite::{backup, params, Connection};

struct DiaryDataSqlite {
connection: Connection,
Expand Down Expand Up @@ -47,6 +47,18 @@ pub fn open_sqlite_datafile(path: &Path) -> Result<Box<dyn DiaryDataConnection>>
let data = DiaryDataSqlite {
connection: Connection::open(path).context("Could not open SQLite database")?,
};
{
let mut backup_ext = OsString::from(path.extension().unwrap_or_default());
backup_ext.push(".bak");
let backup_path = path.with_extension(backup_ext);
let mut backup_connection =
Connection::open(backup_path).context("Could not open SQLite database for backup")?;
let backup = backup::Backup::new(&data.connection, &mut backup_connection)
.context("Could not initiate database backup")?;
backup
.run_to_completion(10, std::time::Duration::default(), None)
.context("Could not perform backup")?;
}
Ok(Box::new(data))
}

Expand Down

0 comments on commit 35d2063

Please sign in to comment.