Skip to content

Commit

Permalink
add parralel vault syncing #2
Browse files Browse the repository at this point in the history
  • Loading branch information
DovieW committed Dec 26, 2024
1 parent 2d5340f commit c3d88f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
1 change: 0 additions & 1 deletion bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export SCRIPTS_REPO_PATH="$REPOS_PATH/$SCRIPTS_REPO"
export OBSIDIAN_DIR="Obsidian"
export OBSIDIAN_DIR_PATH="$REPOS_PATH/$OBSIDIAN_DIR"
export NOTIFICATION_PATH="$STORAGE_PATH/sync-error-notification"
export LAST_SYNC_PATH="$HOME/last_sync.log"

alias sync="$HOME/sync-vaults.sh --skip-pause"
alias bashrc="nano /data/data/com.termux/files/usr/etc/bash.bashrc"
Expand Down
3 changes: 1 addition & 2 deletions setup
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fi

if [ ! -f ~/.ssh/id_rsa ]; then
echo -e "${BLUE}Creating an SSH key...${RESET}"
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa > /dev/null
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa > /dev/null # #TODO: use better crypto?

if cat ~/.ssh/id_rsa.pub | termux-clipboard-set; then
echo -e "${BLUE}SSH public key has been copied to your clipboard.${RESET}"
Expand Down Expand Up @@ -145,4 +145,3 @@ touch $NOTIFICATION_PATH
cd $OBSIDIAN_DIR_PATH

echo -e "\n${GREEN}Setup complete${RESET}\n"

50 changes: 40 additions & 10 deletions sync-vaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ skip_pause_val="--skip-pause"

source "$HOME/log_helper.sh"
log_file="$HOME/sync.log"
setup_logging $log_file
setup_logging "$log_file"

# Create directory to store individual sync logs
temp_dir="${HOME}/obsidian_sync_$$"
mkdir -p "$temp_dir"

cmd () {
printf "\n\033[0;34m%s\033[0m\n" "$(basename "$PWD")"
$HOME/git-sync -ns 2>&1 | tee $LAST_SYNC_PATH

# We run git-sync and capture output. In a parallel scenario,
# we redirect to a file, so we won't "live watch" it in this function.
$HOME/git-sync -ns 2>&1
if [ $? -ne 0 ]; then
cat $LAST_SYNC_PATH >> $NOTIFICATION_PATH # Send notifcation
# If there was an error, append the output to NOTIFICATION_PATH
cat "$1" >> "$NOTIFICATION_PATH"
fi
}

Expand All @@ -58,7 +64,7 @@ for dir in "$OBSIDIAN_DIR_PATH"/*; do
fi
done

msg="You can try running 'setup' to see if it helps".
msg="You can try running 'setup' to see if it helps."

# Exit if no Git repos are found
if [ ${#git_repos[@]} -eq 0 ]; then
Expand All @@ -68,23 +74,47 @@ fi

if [[ -n "$1" && "$1" != "$skip_pause_val" ]]; then # Sync a single repo
if [[ " ${git_repos[@]} " =~ " $OBSIDIAN_DIR_PATH/$1 " ]]; then
(cd "$OBSIDIAN_DIR_PATH/$1" && cmd)
repo="$OBSIDIAN_DIR_PATH/$1"
repo_name="$(basename "$repo")"
tmp_log="$temp_dir/${repo_name}.log"
(cd "$repo" && cmd "$tmp_log" > "$tmp_log" 2>&1)
cat "$tmp_log" >> "$log_file"
else
echo -e "${RED}Specified directory doesn't exist or is not a Git repository.\n${msg}${RESET}"
rm -f "$LOCK_FILE"
exit 1
fi
else # Sync all Git repos
else
# Sync all Git repos in parallel
pids=()
for repo in "${git_repos[@]}"; do
(cd "$repo" && cmd)
repo_name="$(basename "$repo")"
tmp_log="$temp_dir/${repo_name}.log"

(
cd "$repo" && cmd "$tmp_log" > "$tmp_log" 2>&1
) &
pids+=($!)
done

# Wait for all background syncs to complete
for pid in "${pids[@]}"; do
wait "$pid"
done

# Append all temp logs to the main log file
cat "$temp_dir"/*.log >> "$log_file"
fi

log_cleanup $log_file
# Cleanup temp logs
rm -rf "$temp_dir"

log_cleanup "$log_file"

# Pause if no repo was specified and skip_pause was not used
if [[ -z "$1" ]]; then
bypass_log "echo -e '\n\033[44;97mPress enter to finish...\033[0m' && read none"
fi


rm -f "$LOCK_FILE"
exit 0

0 comments on commit c3d88f9

Please sign in to comment.