sqlite-backup-playground/src/main.rs

37 lines
1003 B
Rust

use rusqlite::backup::{Backup, Progress};
use rusqlite::Connection;
use std::env;
use std::fs::copy;
use std::path::Path;
use std::time::Duration;
use tempfile::NamedTempFile;
fn progress(progress: Progress) {
println!("{:?}", progress)
}
fn main() {
let args = env::args().nth(1).expect("Requires 1 argument");
let src_file = Path::new(Path::new(&args));
let tmpfile = NamedTempFile::new().unwrap();
let tmpfile_path = tmpfile.path();
println!("{}", src_file.metadata().unwrap().len() / 1000);
println!("{}", tmpfile_path.display());
let src_db = Connection::open(src_file).unwrap();
let mut dest_db = Connection::open(tmpfile_path).unwrap();
{
let backup = Backup::new(&src_db, &mut dest_db).unwrap();
backup
.run_to_completion(100, Duration::from_millis(100), Some(progress))
.unwrap();
}
src_db.close().unwrap();
dest_db.close().unwrap();
copy(tmpfile_path, Path::new("./backup.db")).unwrap();
}