aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
authorMathijs van Veluw <[email protected]>2024-09-01 15:52:29 +0200
committerGitHub <[email protected]>2024-09-01 15:52:29 +0200
commite9acd8bd3c3142cccb021ab5759a8410ef335aaa (patch)
tree0395c5f9ba57ab7368211d0d200ff4dea2107196 /src/db
parent544b7229e8cc49436d3872ba2022e09231552fa7 (diff)
downloadvaultwarden-e9acd8bd3c3142cccb021ab5759a8410ef335aaa.tar.gz
vaultwarden-e9acd8bd3c3142cccb021ab5759a8410ef335aaa.zip
Add a CLI feature to backup the SQLite DB (#4906)
* Add a CLI feature to backup the SQLite DB Many users request to add the sqlite3 binary to the container image. This isn't really ideal as that might bring in other dependencies and will only bloat the image. There main reason is to create a backup of the database. While there already was a feature within the admin interface to do so (or by using the admin API call), this might not be easy. This PR adds several ways to generate a backup. 1. By calling the Vaultwarden binary with the `backup` command like: - `/vaultwarden backup` - `docker exec -it vaultwarden /vaultwarden backup` 2. By sending the USR1 signal to the running process like: - `kill -s USR1 $(pidof vaultwarden) - `killall -s USR1 vaultwarden) This should help users to more easily create backups of there SQLite database. Also added the Web-Vault version number when using `-v/--version` to the output. Signed-off-by: BlackDex <[email protected]> * Spelling and small adjustments Signed-off-by: BlackDex <[email protected]> --------- Signed-off-by: BlackDex <[email protected]>
Diffstat (limited to 'src/db')
-rw-r--r--src/db/mod.rs22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 824b3c71..51ffba9c 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -368,23 +368,31 @@ pub mod models;
/// Creates a back-up of the sqlite database
/// MySQL/MariaDB and PostgreSQL are not supported.
-pub async fn backup_database(conn: &mut DbConn) -> Result<(), Error> {
+pub async fn backup_database(conn: &mut DbConn) -> Result<String, Error> {
db_run! {@raw conn:
postgresql, mysql {
let _ = conn;
err!("PostgreSQL and MySQL/MariaDB do not support this backup feature");
}
sqlite {
- use std::path::Path;
- let db_url = CONFIG.database_url();
- let db_path = Path::new(&db_url).parent().unwrap().to_string_lossy();
- let file_date = chrono::Utc::now().format("%Y%m%d_%H%M%S").to_string();
- diesel::sql_query(format!("VACUUM INTO '{db_path}/db_{file_date}.sqlite3'")).execute(conn)?;
- Ok(())
+ backup_sqlite_database(conn)
}
}
}
+#[cfg(sqlite)]
+pub fn backup_sqlite_database(conn: &mut diesel::sqlite::SqliteConnection) -> Result<String, Error> {
+ use diesel::RunQueryDsl;
+ let db_url = CONFIG.database_url();
+ let db_path = std::path::Path::new(&db_url).parent().unwrap();
+ let backup_file = db_path
+ .join(format!("db_{}.sqlite3", chrono::Utc::now().format("%Y%m%d_%H%M%S")))
+ .to_string_lossy()
+ .into_owned();
+ diesel::sql_query(format!("VACUUM INTO '{backup_file}'")).execute(conn)?;
+ Ok(backup_file)
+}
+
/// Get the SQL Server version
pub async fn get_sql_server_version(conn: &mut DbConn) -> String {
db_run! {@raw conn: