Skip to content

Commit be9bc86

Browse files
committed
backup subfolders
1 parent 50a798b commit be9bc86

4 files changed

Lines changed: 140 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 121 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ serde_json = "1.0.143"
1111
tokio = { version = "1.47.1", features = ["full"] }
1212
zip = "4.5.0"
1313
toml = "0.9.5"
14+
chrono = "0.4.42"

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct Config {
2121

2222
pub backup_path: PathBuf,
2323

24+
pub backup_subfolders: bool,
25+
2426
#[serde(skip)]
2527
path: PathBuf,
2628
}
@@ -34,6 +36,7 @@ impl Default for Config {
3436
path: "./settings.toml".into(),
3537
backup_mods: true,
3638
backup_path: Path::new(".backup").into(),
39+
backup_subfolders: true,
3740
}
3841
}
3942
}

src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod config;
33

44
use crate::api_calls::{get_api_project_result, get_api_search_result, get_api_version_result};
55
use crate::config::Config;
6+
use chrono::{DateTime, Local};
67
use futures_util::StreamExt;
78
use reqwest::{get, Client};
89
use serde_json::Value;
@@ -162,15 +163,27 @@ async fn download_files(url: &str, path: &str) -> Result<(), Box<dyn std::error:
162163
Ok(())
163164
}
164165

165-
fn backup_mods(config: Config) -> Result<(), Box<dyn std::error::Error>> { //ToDo make it make subfolders with the date and time ran to have multiple backups.
166+
fn backup_mods(config: Config) -> Result<(), Box<dyn std::error::Error>> {
166167
if !config.backup_mods {
167168
return Ok(());
168169
} else {
169170
let from = config.target_path;
170-
let to = config.backup_path;
171+
let mut to = config.backup_path;
171172
if !to.exists() {
172173
std::fs::create_dir_all(&to)?;
173174
}
175+
if config.backup_subfolders {
176+
let system_time = std::time::SystemTime::now();
177+
let datetime: DateTime<Local> = system_time.into();
178+
let date = format!("{}", datetime.format("%m_%d_%y"));
179+
let time = format!("{}", datetime.format("%H:%M:%S"));
180+
to = to.join(&date);
181+
if !to.exists() {
182+
std::fs::create_dir_all(&to)?;
183+
}
184+
to = to.join(&time);
185+
std::fs::create_dir_all(&to)?;
186+
}
174187
copy_dir_all(from, to)?;
175188
}
176189

0 commit comments

Comments
 (0)