summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-11-22 15:00:46 +0000
committerLars Wirzenius <liw@liw.fi>2020-11-22 15:00:46 +0000
commit7194cdfb105b1b835dc2a4ff3bbfc1823f170243 (patch)
tree0484972dd085963e157de00f878d5f0e6d98a52a
parent0158bd9c06acae4245897e5e04681b4a1637bee9 (diff)
parent68e88efced88f05664ae9050f6888453cfe9cd30 (diff)
downloadobnam2-7194cdfb105b1b835dc2a4ff3bbfc1823f170243.tar.gz
Merge branch 'templite' into 'main'
feat! use temporary files for SQLite databases Closes #6 See merge request larswirzenius/obnam!20
-rw-r--r--obnam.md3
-rw-r--r--src/bin/obnam.rs10
-rw-r--r--src/client.rs1
-rw-r--r--src/cmd/backup.rs22
-rw-r--r--src/cmd/restore.rs16
-rw-r--r--subplot/client.py5
-rw-r--r--subplot/client.yaml2
7 files changed, 40 insertions, 19 deletions
diff --git a/obnam.md b/obnam.md
index e93a79f..3beb67d 100644
--- a/obnam.md
+++ b/obnam.md
@@ -504,13 +504,12 @@ when I run obnam backup smoke.yaml
then backup generation is GEN
when I run obnam list smoke.yaml
then generation list contains <GEN>
-when I invoke obnam restore smoke.yaml <GEN> restore.db rest
+when I invoke obnam restore smoke.yaml <GEN> rest
then data in live and rest match
~~~
~~~{#smoke.yaml .file .yaml .numberLines}
root: live
-dbname: tmp.db
~~~
diff --git a/src/bin/obnam.rs b/src/bin/obnam.rs
index d0cf271..1394b6c 100644
--- a/src/bin/obnam.rs
+++ b/src/bin/obnam.rs
@@ -15,12 +15,7 @@ fn main() -> anyhow::Result<()> {
match opt {
Opt::Backup { config } => backup(&config, BUFFER_SIZE)?,
Opt::List { config } => list(&config)?,
- Opt::Restore {
- config,
- gen_id,
- dbname,
- to,
- } => restore(&config, &gen_id, &dbname, &to)?,
+ Opt::Restore { config, gen_id, to } => restore(&config, &gen_id, &to)?,
}
Ok(())
}
@@ -44,9 +39,6 @@ enum Opt {
gen_id: String,
#[structopt(parse(from_os_str))]
- dbname: PathBuf,
-
- #[structopt(parse(from_os_str))]
to: PathBuf,
},
}
diff --git a/src/client.rs b/src/client.rs
index d7dca36..658e3ed 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -14,7 +14,6 @@ use std::path::{Path, PathBuf};
#[derive(Debug, Deserialize, Clone)]
pub struct ClientConfig {
pub server_url: String,
- pub dbname: PathBuf,
pub root: PathBuf,
}
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs
index 71f5aac..926b2b1 100644
--- a/src/cmd/backup.rs
+++ b/src/cmd/backup.rs
@@ -2,20 +2,38 @@ use crate::client::{BackupClient, ClientConfig};
use crate::fsiter::FsIterator;
use crate::generation::Generation;
use std::path::Path;
+use tempfile::NamedTempFile;
pub fn backup(config: &Path, buffer_size: usize) -> anyhow::Result<()> {
let config = ClientConfig::read_config(config)?;
let client = BackupClient::new(&config.server_url)?;
+ // Create a named temporary file. We don't meed the open file
+ // handle, so we discard that.
+ let dbname = {
+ let temp = NamedTempFile::new()?;
+ let (_, dbname) = temp.keep()?;
+ dbname
+ };
+
{
- let mut gen = Generation::create(&config.dbname)?;
+ // Create the SQLite database using the named temporary file.
+ // The fetching is in its own block so that the file handles
+ // get closed and data flushed to disk.
+ let mut gen = Generation::create(&dbname)?;
gen.insert_iter(FsIterator::new(&config.root).map(|entry| match entry {
Err(err) => Err(err),
Ok(entry) => client.upload_filesystem_entry(entry, buffer_size),
}))?;
}
- let gen_id = client.upload_generation(&config.dbname, buffer_size)?;
+
+ // Upload the SQLite file, i.e., the named temporary file, which
+ // still exists, since we persisted it above.
+ let gen_id = client.upload_generation(&dbname, buffer_size)?;
println!("gen id: {}", gen_id);
+ // Delete the temporary file.
+ std::fs::remove_file(&dbname)?;
+
Ok(())
}
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs
index 7956b5f..0051d20 100644
--- a/src/cmd/restore.rs
+++ b/src/cmd/restore.rs
@@ -7,14 +7,25 @@ use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
+use tempfile::NamedTempFile;
+
+pub fn restore(config: &Path, gen_id: &str, to: &Path) -> anyhow::Result<()> {
+ // Create a named temporary file. We don't meed the open file
+ // handle, so we discard that.
+ let dbname = {
+ let temp = NamedTempFile::new()?;
+ let (_, dbname) = temp.keep()?;
+ dbname
+ };
-pub fn restore(config: &Path, gen_id: &str, dbname: &Path, to: &Path) -> anyhow::Result<()> {
let config = ClientConfig::read_config(&config).unwrap();
let client = BackupClient::new(&config.server_url)?;
let gen_chunk = client.fetch_generation(&gen_id)?;
debug!("gen: {:?}", gen_chunk);
+
{
+ // Fetch the SQLite file, storing it in the temporary file.
let mut dbfile = File::create(&dbname)?;
for id in gen_chunk.chunk_ids() {
let chunk = client.fetch_chunk(id)?;
@@ -28,6 +39,9 @@ pub fn restore(config: &Path, gen_id: &str, dbname: &Path, to: &Path) -> anyhow:
restore_generation(&client, &gen, fileid, entry, &to)?;
}
+ // Delete the temporary file.
+ std::fs::remove_file(&dbname)?;
+
Ok(())
}
diff --git a/subplot/client.py b/subplot/client.py
index 7556986..acdc48a 100644
--- a/subplot/client.py
+++ b/subplot/client.py
@@ -24,13 +24,12 @@ def configure_client(ctx, filename=None):
yaml.safe_dump(config, stream=f)
-def run_obnam_restore(ctx, filename=None, genid=None, dbname=None, todir=None):
+def run_obnam_restore(ctx, filename=None, genid=None, todir=None):
runcmd_run = globals()["runcmd_run"]
genid = ctx["vars"][genid]
runcmd_run(
- ctx,
- ["env", "RUST_LOG=obnam", "obnam", "restore", filename, genid, dbname, todir],
+ ctx, ["env", "RUST_LOG=obnam", "obnam", "restore", filename, genid, todir]
)
diff --git a/subplot/client.yaml b/subplot/client.yaml
index 80b69f2..63033c3 100644
--- a/subplot/client.yaml
+++ b/subplot/client.yaml
@@ -4,7 +4,7 @@
- given: "a client config based on {filename}"
function: configure_client
-- when: "I invoke obnam restore {filename} <{genid}> {dbname} {todir}"
+- when: "I invoke obnam restore {filename} <{genid}> {todir}"
function: run_obnam_restore
- then: "backup generation is {varname}"