From 7eb4bef136ed102a6f5f5f3eacbe501762b7c04a Mon Sep 17 00:00:00 2001 From: darryl west Date: Tue, 21 Nov 2023 17:08:41 -0800 Subject: [PATCH 1/5] cargo updates --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/backup_process.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f2c0a9..2bd30cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -885,7 +885,7 @@ dependencies = [ [[package]] name = "replica" -version = "0.4.4" +version = "0.4.5" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 65de1db..de2585c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "replica" -version = "0.4.4" +version = "0.4.5" edition = "2021" authors = ["darryl.west@raincitysoftware.com"] rust-version = "1.70" diff --git a/src/backup_process.rs b/src/backup_process.rs index e45062e..f47481d 100644 --- a/src/backup_process.rs +++ b/src/backup_process.rs @@ -149,6 +149,11 @@ impl BackupProcess { Ok(()) } + + /// retrun the UTC time in seconds (unix timestamp.) decode with date -r + pub fn timestamp(&self) -> u64 { + Utc::now().timestamp() as u64 + } } #[cfg(test)] @@ -250,4 +255,32 @@ mod tests { println!("{:?}", response); assert!(response.is_some()); } + + #[test] + fn timestamp() { + let path = "tests/"; + let files = create_filelist(); + + let backup = BackupProcess::new(path, files, true); + + let ts = backup.timestamp(); + + println!("timestamp: {} = {}", ts, get_now_seconds()); + assert!(ts > 1_700_604_600); + let tss = from_timestamp(ts); + println!("{}->{}", ts, tss); + } + + fn get_now_seconds() -> u64 { + use std::time::SystemTime; + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() + } + + fn from_timestamp(ts: u64) -> String { + use chrono::TimeZone; + Utc.timestamp_opt(ts as i64, 0).unwrap().to_rfc3339() + } } From 4a4d42d64781e65295625485b0e07df1b2b86f44 Mon Sep 17 00:00:00 2001 From: darryl west Date: Thu, 23 Nov 2023 09:25:57 -0800 Subject: [PATCH 2/5] refactored to pull cli vars into config --- .test-replica/config/config.toml | 2 + .test-replica/config/run-config.toml | 2 + .test-replica/config/walk-config.toml | 2 + Cargo.lock | 2 +- Cargo.toml | 2 +- src/bin/replica.rs | 89 +++++++++++++-------------- src/config.rs | 4 ++ 7 files changed, 56 insertions(+), 47 deletions(-) diff --git a/.test-replica/config/config.toml b/.test-replica/config/config.toml index 6a4539f..7766b33 100644 --- a/.test-replica/config/config.toml +++ b/.test-replica/config/config.toml @@ -13,3 +13,5 @@ excludes = [] dbfile = ".replica/data/files.json" compress = false encrypt = false +dryrun = false +verbose = false diff --git a/.test-replica/config/run-config.toml b/.test-replica/config/run-config.toml index 821bf3a..fba28df 100644 --- a/.test-replica/config/run-config.toml +++ b/.test-replica/config/run-config.toml @@ -14,3 +14,5 @@ excludes = [] dbfile = ".test-replica/data/run2-file.json" compress = false encrypt = false +dryrun = false +verbose = false diff --git a/.test-replica/config/walk-config.toml b/.test-replica/config/walk-config.toml index 03b1cc2..54561da 100644 --- a/.test-replica/config/walk-config.toml +++ b/.test-replica/config/walk-config.toml @@ -18,3 +18,5 @@ excludes = [ dbfile = ".replica/data/files.json" compress = false encrypt = false +dryrun = false +verbose = false diff --git a/Cargo.lock b/Cargo.lock index 2bd30cd..77d5ed8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -885,7 +885,7 @@ dependencies = [ [[package]] name = "replica" -version = "0.4.5" +version = "0.4.6" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index de2585c..71b64e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "replica" -version = "0.4.5" +version = "0.4.6" edition = "2021" authors = ["darryl.west@raincitysoftware.com"] rust-version = "1.70" diff --git a/src/bin/replica.rs b/src/bin/replica.rs index 2488d0b..37dc182 100644 --- a/src/bin/replica.rs +++ b/src/bin/replica.rs @@ -35,22 +35,32 @@ fn cd_app_home(app_home: &str) { env::set_current_dir(app_home).unwrap_or_else(|_| panic!("{}", msg)); } -fn startup(cli: &Cli) -> Config { - let config = Config::read_config(cli.config.as_str()).expect("config should initialize"); +/// read the cli to override config dryrun and verbose if false +fn startup(cli: Cli) -> Config { + // println!("cli: {:?}", cli); + let mut config = Config::read_config(cli.config.as_str()).expect("config should initialize"); config.start_logger().expect("logger should start."); + if !config.dryrun { + config.dryrun = cli.dryrun; + } + + if !config.verbose { + config.verbose = cli.verbose; + } + info!("replica config: {:?}", config); config.to_owned() } /// the primary process -fn run(cli: Cli, config: Config) -> Result<()> { +fn run(config: Config) -> Result<()> { let start_time = Instant::now(); cd_app_home(config.home.as_str()); - if cli.dryrun { + if config.dryrun { warn!("THIS IS A DRY RUN!"); } @@ -62,7 +72,7 @@ fn run(cli: Cli, config: Config) -> Result<()> { info!("file count: {}", files.len()); let target_dir = &config.targets[0]; - let backup = BackupProcess::new(target_dir.as_str(), files.clone(), cli.dryrun); + let backup = BackupProcess::new(target_dir.as_str(), files.clone(), config.dryrun); let results = backup.process(db.clone()); if results.is_err() { error!("backup failed: {:?}", results); @@ -88,9 +98,8 @@ fn main() -> Result<()> { let home = env::var("HOME").expect("The user should have a home folder."); cd_app_home(home.as_str()); - let cli = Cli::parse(); - let config = startup(&cli); - run(cli, config) + let config = startup(Cli::parse()); + run(config) } #[cfg(test)] @@ -98,6 +107,23 @@ mod tests { use super::*; use std::{fs::File, io::Write}; + // returns the default cli struct + fn get_conf_path() -> String { + let test_home = env::current_dir().expect("should get the current working directory"); + format!( + "{}/.test-replica/config/run-config.toml", + test_home.to_str().unwrap() + ) + } + + fn dflt_cli() -> Cli { + Cli { + config: get_conf_path(), + verbose: false, + dryrun: false, + } + } + fn change_file() { let filename = "tests/changed-file.txt"; let mut buf = File::create(filename).unwrap(); @@ -107,20 +133,9 @@ mod tests { #[test] fn startup_test() { - let test_home = env::current_dir().expect("should get the current working directory"); - let conf_path = format!( - "{}/.test-replica/config/run-config.toml", - test_home.to_str().unwrap() - ); - - let cli = Cli { - config: conf_path, - verbose: false, - dryrun: false, - }; + let cli = dflt_cli(); - let config = startup(&cli); - println!("cli: {:?}", cli); + let config = startup(cli); println!("ctx: {:?}", config); assert!(true); @@ -129,40 +144,24 @@ mod tests { #[test] fn run_test() { change_file(); - let test_home = env::current_dir().expect("should get the current working directory"); - let conf_path = format!( - "{}/.test-replica/config/run-config.toml", - test_home.to_str().unwrap() - ); + let conf_path = get_conf_path(); let config = Config::read_config(conf_path.as_str()).unwrap(); println!("conf path : {:?}", conf_path); - let cli = Cli { - config: conf_path, - verbose: false, - dryrun: false, - }; + let cli = dflt_cli(); println!("{:?}", cli); - let results = run(cli, config); + let results = run(config); assert!(results.is_ok()); } #[test] fn run_test_dryrun() { - let test_home = env::current_dir().expect("should get the current working directory"); - let conf_path = format!( - "{}/.test-replica/config/run-config.toml", - test_home.to_str().unwrap() - ); - let config = Config::read_config(conf_path.as_str()).unwrap(); + let conf_path = get_conf_path(); + let mut config = Config::read_config(conf_path.as_str()).unwrap(); + config.verbose = true; + config.dryrun = true; println!("conf path : {:?}", conf_path); - let cli = Cli { - config: conf_path, - verbose: true, - dryrun: true, - }; - println!("{:?}", cli); - let results = run(cli, config); + let results = run(config); println!("{:?}", results); assert!(results.is_ok()); } diff --git a/src/config.rs b/src/config.rs index 52197cf..18e4c7a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,6 +22,8 @@ pub struct Config { pub dbfile: String, pub compress: bool, pub encrypt: bool, + pub dryrun: bool, + pub verbose: bool, } impl Config { @@ -53,6 +55,8 @@ impl Config { dbfile: self.dbfile.clone(), compress: self.compress, encrypt: self.encrypt, + dryrun: false, + verbose: false, } } From 103e09f807584323da7d35c8fffb111d4c594287 Mon Sep 17 00:00:00 2001 From: darryl west Date: Thu, 23 Nov 2023 11:45:29 -0800 Subject: [PATCH 3/5] added journaled entry to config files --- .test-replica/config/config.toml | 1 + .test-replica/config/plaza-config.toml | 1 + .test-replica/config/run-config.toml | 1 + .test-replica/config/run1-config.toml | 1 + .test-replica/config/tiburon-config.toml | 1 + .test-replica/config/walk-config.toml | 1 + 6 files changed, 6 insertions(+) diff --git a/.test-replica/config/config.toml b/.test-replica/config/config.toml index 7766b33..e4a8264 100644 --- a/.test-replica/config/config.toml +++ b/.test-replica/config/config.toml @@ -10,6 +10,7 @@ files = [ "big-file.pdf", ] excludes = [] +journaled = [] dbfile = ".replica/data/files.json" compress = false encrypt = false diff --git a/.test-replica/config/plaza-config.toml b/.test-replica/config/plaza-config.toml index e35ef4f..7125e0a 100644 --- a/.test-replica/config/plaza-config.toml +++ b/.test-replica/config/plaza-config.toml @@ -7,5 +7,6 @@ source_folders = ["photos", "Music", ".local/bin" ] targets = ["/Volumes/plaza/plaza", "dpw@piedmont.local:backup/plaza", "dpw@orinda.local:.backup/plaza"] files = [ ".age/decrypt.sh", ".age/encrypt.sh", ".age/key.enc", ".alias", ".config", ".motd", ".profile", ".ssh/authorized_keys", ".ssh/plaza.pub", ".zprofile", ".zshenv", ".zshrc" ] excludes = [] +journaled = [] compress = false encrypt = false diff --git a/.test-replica/config/run-config.toml b/.test-replica/config/run-config.toml index fba28df..a90e043 100644 --- a/.test-replica/config/run-config.toml +++ b/.test-replica/config/run-config.toml @@ -11,6 +11,7 @@ files = [ "tests/changed-file.txt", ] excludes = [] +journaled = [] dbfile = ".test-replica/data/run2-file.json" compress = false encrypt = false diff --git a/.test-replica/config/run1-config.toml b/.test-replica/config/run1-config.toml index ed66408..e7384a3 100644 --- a/.test-replica/config/run1-config.toml +++ b/.test-replica/config/run1-config.toml @@ -11,6 +11,7 @@ files = [ "tests/changed-file.txt", ] excludes = [] +journaled = [] dbfile = ".test-replica/data/run1-file.json" compress = false encrypt = false diff --git a/.test-replica/config/tiburon-config.toml b/.test-replica/config/tiburon-config.toml index cc16ff9..da602c1 100644 --- a/.test-replica/config/tiburon-config.toml +++ b/.test-replica/config/tiburon-config.toml @@ -7,5 +7,6 @@ source_folders = ["photos", "Music", ".local/bin"] targets = ["dpw@plaza.local:/Volumes/plaza/tiburon", "dpw@piedmont.local:backup/tiburon", "dpw@orinda.local:.backup/tiburon"] files = [ ".alias", ".config", ".motd", ".profile", ".ssh/authorized_keys", ".ssh/id_rsa.pub", ".wget-hsts", ".zprofile", ".zshenv", ".zshrc" ] excludes = [] +journaled = [] compress = false encrypt = false diff --git a/.test-replica/config/walk-config.toml b/.test-replica/config/walk-config.toml index 54561da..4dc38b7 100644 --- a/.test-replica/config/walk-config.toml +++ b/.test-replica/config/walk-config.toml @@ -15,6 +15,7 @@ excludes = [ ".config/configstore", ".config/dconf", ] +journaled = [] dbfile = ".replica/data/files.json" compress = false encrypt = false From 8e09696d9c075506695b704628b971419c56f8b2 Mon Sep 17 00:00:00 2001 From: darryl west Date: Fri, 24 Nov 2023 07:43:18 -0800 Subject: [PATCH 4/5] added journaled vector to config --- src/config.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/config.rs b/src/config.rs index 18e4c7a..0fc0ec2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,6 +19,7 @@ pub struct Config { pub targets: Vec, pub files: Vec, pub excludes: Vec, + pub journaled: Vec, pub dbfile: String, pub compress: bool, pub encrypt: bool, @@ -52,6 +53,7 @@ impl Config { targets: self.targets.clone(), files: self.files.clone(), excludes: self.excludes.clone(), + journaled: self.journaled.clone(), dbfile: self.dbfile.clone(), compress: self.compress, encrypt: self.encrypt, From 96b2c68ef7b0af3cc26c410890a9f81f5fcffdb7 Mon Sep 17 00:00:00 2001 From: darryl west Date: Fri, 24 Nov 2023 07:50:45 -0800 Subject: [PATCH 5/5] version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77d5ed8..270903e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -885,7 +885,7 @@ dependencies = [ [[package]] name = "replica" -version = "0.4.6" +version = "0.4.7" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 71b64e8..ef3bbde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "replica" -version = "0.4.6" +version = "0.4.7" edition = "2021" authors = ["darryl.west@raincitysoftware.com"] rust-version = "1.70"