From f1e025e059400a2570a6b7323c2b05d7b5922521 Mon Sep 17 00:00:00 2001 From: Robin Gottschalk Date: Wed, 5 Feb 2025 21:55:52 +0100 Subject: [PATCH] Make more options constants and protect git files by prefix --- README.md | 14 +++++++++++++- src/filesync.rs | 15 +++++++-------- src/main.rs | 17 ++++++++--------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 155809c..a52af7e 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,16 @@ This script automates the process of backing up your Obsidian vault to a Git rep ## Installation - Create target repo folder and initialize an empty git repository -- Link repository to remote and set upstream branch (git push working) +- Set up git lfs for attachments + - `$ git lfs install` + - `.gitattributes` + ``` + *.pdf filter=lfs diff=lfs merge=lfs -text + *.png filter=lfs diff=lfs merge=lfs -text + *.jpg filter=lfs diff=lfs merge=lfs -text + *.jpeg filter=lfs diff=lfs merge=lfs -text + *.webp filter=lfs diff=lfs merge=lfs -text + *.avif filter=lfs diff=lfs merge=lfs -text + *.svg filter=lfs diff=lfs merge=lfs -text + ``` +- Link repository to origin as remote diff --git a/src/filesync.rs b/src/filesync.rs index 8f33e48..093b4fc 100644 --- a/src/filesync.rs +++ b/src/filesync.rs @@ -4,20 +4,19 @@ use std::path::{Path, PathBuf}; pub fn sync_directory( source: &Path, target: &Path, - protect: Option<&str>, // Will not get touched in target (file or directory at top level) + protect_prefix: Option<&str>, // Will not get touched in target (file or directory at top level) ) -> Result<(), Box> { // Ensure the target directory exists fs::create_dir_all(target)?; // Gather all paths in the source and target directories let filter = |entry: Result| { - entry.ok().and_then(|entry| { - if entry.file_name().to_str() != protect { - Some(entry.path()) - } else { - None - } - }) + entry + .ok() + .and_then(|entry| match (entry.file_name().to_str(), protect_prefix) { + (Some(name), Some(prefix)) if name.starts_with(prefix) => None, + _ => Some(entry.path()), + }) }; let mut source_entries: Vec = fs::read_dir(source)?.filter_map(filter).collect(); let mut target_entries: Vec = fs::read_dir(target)?.filter_map(filter).collect(); diff --git a/src/main.rs b/src/main.rs index 008cde6..7f791b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,11 @@ use winit::{ mod filesync; mod git; -const SOURCE_VAULT: &str = "D:\\test\\obsidian-vault-source"; // syncthing vault folder -const REPO_VAULT: &str = "D:\\test\\obsidian-vault-test"; // git repo folder +const PROTECT_PREFIX: &str = ".git"; // only at first directory level +const SOURCE_VAULT: &str = "D:\\Cloud\\Syncthing\\obsidian-vault"; // syncthing vault folder +const REPO_VAULT: &str = + "C:\\Users\\robin\\AppData\\Roaming\\obsidian-git-backup\\obsidian-vault-clone"; // git repo folder +const GIT_COMMIT_DELAY_AFTER_FILE_CHANGE: Duration = Duration::from_secs(5 * 60); // 5 min #[derive(Debug)] enum UserEvent { @@ -31,7 +34,6 @@ struct App { menu_backup_now: Option, menu_exit: Option, last_change_time: Option, - duration_threshold: Duration, current_change_count: usize, } @@ -74,7 +76,7 @@ impl App { self.last_change_time = Some(Instant::now()); let source = Path::new(SOURCE_VAULT); let target = Path::new(REPO_VAULT); - filesync::sync_directory(source, target, Some(".git")) + filesync::sync_directory(source, target, Some(PROTECT_PREFIX)) .expect("Directories could not be synced"); self.current_change_count = git::current_change_count(target).expect("Changes could not get counted using git"); @@ -92,10 +94,7 @@ impl App { impl ApplicationHandler for App { fn resumed(&mut self, _event_loop: &ActiveEventLoop) { self.create_tray_icon_menu(); - - // Set the duration threshold (e.g., 5 minutes) self.last_change_time = None; - self.duration_threshold = Duration::from_secs(15); } fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { @@ -127,7 +126,7 @@ impl ApplicationHandler for App { // Backup changes if last file change time exceeded threshold if let Some(last_change_time) = self.last_change_time { - if now.duration_since(last_change_time) > self.duration_threshold { + if now.duration_since(last_change_time) > GIT_COMMIT_DELAY_AFTER_FILE_CHANGE { println!("---> Backup triggered by file change"); self.start_backup(); } @@ -150,7 +149,7 @@ fn main() { // Sync folder and check for changes to back up let source_dir = Path::new(SOURCE_VAULT); let repo_dir = Path::new(REPO_VAULT); - filesync::sync_directory(source_dir, repo_dir, Some(".git")) + filesync::sync_directory(source_dir, repo_dir, Some(PROTECT_PREFIX)) .expect("Directories could not be synced"); git::backup_changes(repo_dir).expect("Changes could not be pushed");