Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
raincity committed Nov 24, 2023
2 parents 4edaddc + 96b2c68 commit 0a77e43
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .test-replica/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ files = [
"big-file.pdf",
]
excludes = []
journaled = []
dbfile = ".replica/data/files.json"
compress = false
encrypt = false
dryrun = false
verbose = false
1 change: 1 addition & 0 deletions .test-replica/config/plaza-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ source_folders = ["photos", "Music", ".local/bin" ]
targets = ["/Volumes/plaza/plaza", "[email protected]:backup/plaza", "[email protected]:.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
3 changes: 3 additions & 0 deletions .test-replica/config/run-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ files = [
"tests/changed-file.txt",
]
excludes = []
journaled = []
dbfile = ".test-replica/data/run2-file.json"
compress = false
encrypt = false
dryrun = false
verbose = false
1 change: 1 addition & 0 deletions .test-replica/config/run1-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ files = [
"tests/changed-file.txt",
]
excludes = []
journaled = []
dbfile = ".test-replica/data/run1-file.json"
compress = false
encrypt = false
1 change: 1 addition & 0 deletions .test-replica/config/tiburon-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ source_folders = ["photos", "Music", ".local/bin"]
targets = ["[email protected]:/Volumes/plaza/tiburon", "[email protected]:backup/tiburon", "[email protected]:.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
3 changes: 3 additions & 0 deletions .test-replica/config/walk-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ excludes = [
".config/configstore",
".config/dconf",
]
journaled = []
dbfile = ".replica/data/files.json"
compress = false
encrypt = false
dryrun = false
verbose = false
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "replica"
version = "0.4.4"
version = "0.4.7"
edition = "2021"
authors = ["[email protected]"]
rust-version = "1.70"
Expand Down
33 changes: 33 additions & 0 deletions src/backup_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ impl BackupProcess {

Ok(())
}

/// retrun the UTC time in seconds (unix timestamp.) decode with date -r <seconds>
pub fn timestamp(&self) -> u64 {
Utc::now().timestamp() as u64
}
}

#[cfg(test)]
Expand Down Expand Up @@ -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()
}
}
89 changes: 44 additions & 45 deletions src/bin/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand All @@ -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);
Expand All @@ -88,16 +98,32 @@ 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)]
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();
Expand All @@ -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);
Expand All @@ -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());
}
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ pub struct Config {
pub targets: Vec<String>,
pub files: Vec<String>,
pub excludes: Vec<String>,
pub journaled: Vec<String>,
pub dbfile: String,
pub compress: bool,
pub encrypt: bool,
pub dryrun: bool,
pub verbose: bool,
}

impl Config {
Expand Down Expand Up @@ -50,9 +53,12 @@ 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,
dryrun: false,
verbose: false,
}
}

Expand Down

0 comments on commit 0a77e43

Please sign in to comment.